Example #1
0
 /**
  * @param string $clientId
  * @param string $clientSecret
  * @param string $username
  * @param string $password
  * @param string $authenticationDomain
  *
  * @return Api
  */
 public static function initWithAuthentication($clientId, $clientSecret, $username, $password, $authenticationDomain = null)
 {
     if (!$authenticationDomain) {
         $authenticationDomain = self::DEFAULT_AUTHENTICATION_DOMAIN;
     }
     $httpClient = new Client();
     $response = $httpClient->request('POST', $authenticationDomain . '/services/oauth2/token', ['form_params' => ['client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'password', 'username' => $username, 'password' => $password]]);
     if (200 == $response->getStatusCode()) {
         $data = json_decode($response->getBody()->getContents(), true);
         $session = new Session($data['access_token'], $data['instance_url']);
         $session->setId($data['id']);
         $session->setIssuedAt($data['issued_at']);
         $session->setSignature($data['signature']);
         $session->setTokenType($data['token_type']);
         $api = new static($httpClient, $session);
         static::setInstance($api);
         return $api;
     }
     return $response->getStatusCode();
 }