You are here

class Account in GatherContent 8

Same name and namespace in other branches
  1. 8.3 src/DAO/Account.php \Drupal\gathercontent\DAO\Account

Class Account.

@package GatherContent

Hierarchy

  • class \Drupal\gathercontent\DAO\Account

Expanded class hierarchy of Account

1 file declares its use of Account
ConfigForm.php in src/Form/ConfigForm.php

File

src/DAO/Account.php, line 17
Contains GatherContent\Accont class.

Namespace

Drupal\gathercontent\DAO
View source
class Account {
  private $client;

  /**
   * Account constructor.
   *
   * @param string $username
   *   API username.
   * @param string $api_key
   *   API key.
   */
  public function __construct($username = NULL, $api_key = NULL) {
    if (is_null($username)) {
      $username = \Drupal::config('gathercontent.settings')
        ->get('gathercontent_username');
    }
    if (is_null($api_key)) {
      $api_key = \Drupal::config('gathercontent.settings')
        ->get('gathercontent_api_key');
    }
    if (empty($username || $api_key)) {
      \Drupal::logger('gathercontent')
        ->error("Trying to call API without credentials.", array());
    }
    $this->client = new Client(array(
      'base_uri' => 'https://api.gathercontent.com',
      'auth' => array(
        $username,
        $api_key,
      ),
      'headers' => array(
        'Accept' => 'application/vnd.gathercontent.v0.5+json',
      ),
    ));
  }

  /**
   * Get list of accounts.
   *
   * @return array
   *   Array with accounts.
   */
  public function getAccounts() {
    $accounts = array();
    try {
      $response = $this->client
        ->get('/accounts');
      if ($response
        ->getStatusCode() === 200) {
        $data = json_decode($response
          ->getBody());
        $accounts = $data->data;
      }
      else {
        drupal_set_message(t("User with provided credentials wasn't found."), 'error');
        $accounts = NULL;
      }
    } catch (\Exception $e) {
      \Drupal::logger('gathercontent')
        ->error($e
        ->getMessage(), array());
      drupal_set_message(t("User with provided credentials wasn't found. 1"), 'error');
      $accounts = NULL;
    }
    return $accounts;
  }

  /**
   * Test connection with current credentials.
   *
   * @return bool
   *   Return TRUE on success connection otherwise FALSE.
   */
  public function testConnection() {
    try {
      $response = $this->client
        ->get('/me');
      if ($response
        ->getStatusCode() === 200) {
        return TRUE;
      }
      else {
        return FALSE;
      }
    } catch (\Exception $e) {
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Account::$client private property
Account::getAccounts public function Get list of accounts.
Account::testConnection public function Test connection with current credentials.
Account::__construct public function Account constructor.