You are here

class QaAccountsCreateDelete in QA Accounts 8

Service that creates and deletes QA accounts for roles.

Hierarchy

Expanded class hierarchy of QaAccountsCreateDelete

1 string reference to 'QaAccountsCreateDelete'
qa_accounts.services.yml in ./qa_accounts.services.yml
qa_accounts.services.yml
1 service uses QaAccountsCreateDelete
qa_accounts.create_delete in ./qa_accounts.services.yml
\Drupal\qa_accounts\QaAccountsCreateDelete

File

src/QaAccountsCreateDelete.php, line 11

Namespace

Drupal\qa_accounts
View source
class QaAccountsCreateDelete implements QaAccountsCreateDeleteInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The logger channel for this service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * QaAccountsCreateDelete constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger factory.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactoryInterface $logger_factory) {
    $this->entityTypeManager = $entity_type_manager;
    $this->logger = $logger_factory
      ->get('qa_accounts');
  }

  /**
   * {@inheritdoc}
   */
  public function createQaAccounts() {
    $roles = $this->entityTypeManager
      ->getStorage('user_role')
      ->loadMultiple();
    foreach ($roles as $role_name => $role) {
      if ($role_name === 'anonymous') {
        continue;
      }
      $this
        ->createQaAccountForRole($role_name);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function createQaAccountForRole($role_name) {
    $username = 'qa_' . $role_name;

    // Check if qa user already exists.
    $user_storage = $this->entityTypeManager
      ->getStorage('user');
    $uids = $user_storage
      ->getQuery()
      ->condition('name', $username)
      ->accessCheck(FALSE)
      ->range(0, 1)
      ->execute();
    if ($uids) {
      $this->logger
        ->notice('User @name already exists.', [
        '@name' => $username,
      ]);
    }
    else {

      /** @var \Drupal\user\Entity\User $user */
      $user = $user_storage
        ->create();
      $user
        ->enforceIsNew();
      $user
        ->setUsername($username);
      $user
        ->setEmail($username . '@example.com');
      $user
        ->setPassword($username);
      if ($role_name !== 'authenticated') {
        $user
          ->addRole($role_name);
      }
      $user
        ->activate();
      $user
        ->save();
      $this->logger
        ->notice('Created user @name.', [
        '@name' => $username,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteQaAccounts() {
    $roles = $this->entityTypeManager
      ->getStorage('user_role')
      ->loadMultiple();
    foreach ($roles as $role_name => $role) {
      if ($role_name === 'anonymous') {
        continue;
      }
      $this
        ->deleteQaAccountForRole($role_name);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteQaAccountForRole($role_name) {
    $username = 'qa_' . $role_name;

    // Check that user for role exists.
    $users = $this->entityTypeManager
      ->getStorage('user')
      ->loadByProperties([
      'name' => $username,
    ]);
    $user = $users ? reset($users) : NULL;
    if ($user) {
      $user
        ->delete();
      $this->logger
        ->notice('Deleted user @name.', [
        '@name' => $username,
      ]);
    }
    else {
      $this->logger
        ->notice('No such user @name.', [
        '@name' => $username,
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QaAccountsCreateDelete::$entityTypeManager protected property The entity type manager.
QaAccountsCreateDelete::$logger protected property The logger channel for this service.
QaAccountsCreateDelete::createQaAccountForRole public function Creates QA account for specified role. Overrides QaAccountsCreateDeleteInterface::createQaAccountForRole
QaAccountsCreateDelete::createQaAccounts public function Creates QA accounts for all existing roles. Overrides QaAccountsCreateDeleteInterface::createQaAccounts
QaAccountsCreateDelete::deleteQaAccountForRole public function Deletes QA account for specified role. Overrides QaAccountsCreateDeleteInterface::deleteQaAccountForRole
QaAccountsCreateDelete::deleteQaAccounts public function Deletes QA accounts. Overrides QaAccountsCreateDeleteInterface::deleteQaAccounts
QaAccountsCreateDelete::__construct public function QaAccountsCreateDelete constructor.