You are here

public function User::getUserProxy in Rocket.Chat 8.2

Retrieve the Proxy, create the User if needed.

Parameters

\Drupal\rocket_chat_api\RocketChat\ApiClient $apiClient:

Return value

array|null

Throws

Exception

File

modules/rocket_chat_api/src/RocketChat/Element/User.php, line 145

Class

User

Namespace

Drupal\rocket_chat_api\RocketChat\Element

Code

public function getUserProxy(ApiClient $apiClient) {
  if (empty($this->User)) {
    $users = [];
    if ($this
      ->isEmpty()) {

      //TODO report this fail state!
      return NULL;
    }
    $usersState = new Users($this->state, $apiClient);
    $users = $usersState
      ->getCache();
    if (!$this
      ->isInList($users)) {
      $options = [];
      $options['json'] = [];
      $options['json']['email'] = $this->email;
      $options['json']['name'] = $this->name;
      $options['json']['password'] = bin2hex(random_bytes(512));
      $options['json']['username'] = $this->username;
      $options['json']['verified'] = TRUE;

      /**
       * @see https://rocket.chat/docs/developer-guides/rest-api/users/create/
       */
      $ret = $apiClient
        ->postToRocketChat("users.create", $options);
    }
    else {
      $found = 0;
      $userCache = $this
        ->getFromList($users);
      if (empty($userCache)) {
        $ret = $apiClient
          ->getFromRocketChat("users.info", [
          "query" => [
            "username" => $this->username,
          ],
        ]);
      }
      else {
        $ret = [];
        $ret['body'] = [];
        $ret['body']['user'] = $userCache;
      }
    }
    $this->User = $ret['body']['user'];
    return $this->User;
  }
  else {

    //Refresh User
    return null;
  }
}