You are here

protected function RoleExpireSetExpireTime::doExecute in Role Expire 8

Same name and namespace in other branches
  1. 2.x src/Plugin/RulesAction/RoleExpireSetExpireTime.php \Drupal\role_expire\Plugin\RulesAction\RoleExpireSetExpireTime::doExecute()

Assign expire time for user and role.

Parameters

\Drupal\user\UserInterface $account: User object.

string $roles: Array of User roles ID.

string $date: Date when the roles will expire.

Throws

\Drupal\rules\Exception\InvalidArgumentException

File

src/Plugin/RulesAction/RoleExpireSetExpireTime.php, line 45

Class

RoleExpireSetExpireTime
Provides a 'Set expire time' action.

Namespace

Drupal\role_expire\Plugin\RulesAction

Code

protected function doExecute(UserInterface $account, array $roles, $date) {
  foreach ($roles as $role) {

    // Skip adding the expire time for the role if user doesn't have it.
    if ($account
      ->hasRole($role)) {
      try {
        $time = strtotime($date);
        if (!empty($time)) {
          \Drupal::service('role_expire.api')
            ->writeRecord($account
            ->id(), $role, $time);
        }
        else {
          throw new InvalidArgumentException($this
            ->t('Invalid date format.'));
        }
      } catch (\InvalidArgumentException $e) {
        throw new InvalidArgumentException($e
          ->getMessage());
      }
    }
  }
}