You are here

class Template in GatherContent 8

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

Class Template.

@package GatherContent

Hierarchy

  • class \Drupal\gathercontent\DAO\Template

Expanded class hierarchy of Template

5 files declare their use of Template
gathercontent.module in ./gathercontent.module
Main module file for GatherContent module.
GathercontentContentImportConfirmForm.php in src/Form/GathercontentContentImportConfirmForm.php
GathercontentMappingEditForm.php in src/Form/GathercontentMappingEditForm.php
GathercontentMappingImportForm.php in src/Form/GathercontentMappingImportForm.php
GathercontentMappingListBuilder.php in src/GathercontentMappingListBuilder.php
1 string reference to 'Template'
gathercontent_mapping.schema.yml in config/schema/gathercontent_mapping.schema.yml
config/schema/gathercontent_mapping.schema.yml

File

src/DAO/Template.php, line 12

Namespace

Drupal\gathercontent\DAO
View source
class Template {
  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',
      ),
    ));
  }

  /**
   * Fetch projects from GatherContent.
   *
   * @return array
   *   Associative array of projects.
   */
  public function getTemplates($project_id) {
    $templates = array();
    try {
      $response = $this->client
        ->get('/templates?project_id=' . $project_id);
      if ($response
        ->getStatusCode() === 200) {
        $data = json_decode($response
          ->getBody());
        foreach ($data->data as $template) {
          $templates[$template->id] = $template->name;
        }
      }
    } catch (\Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
      \Drupal::logger('gathercontent')
        ->error($e
        ->getMessage(), array());
    }
    return $templates;
  }

  /**
   * Fetch projects from GatherContent.
   *
   * @return array
   *   Associative array of projects.
   */
  public function getTemplatesObject($project_id) {
    $templates = array();
    try {
      $response = $this->client
        ->get('/templates?project_id=' . $project_id);
      if ($response
        ->getStatusCode() === 200) {
        $data = json_decode($response
          ->getBody());
        foreach ($data->data as $template) {
          $templates[$template->id] = $template;
        }
      }
    } catch (\Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
      \Drupal::logger('gathercontent')
        ->error($e
        ->getMessage(), array());
    }
    return $templates;
  }

  /**
   * TBD.
   *
   * @param int $template_id
   *   ID of template.
   *
   * @return object
   *   Object of template
   */
  public function getTemplate($template_id) {
    $template = NULL;
    try {
      $response = $this->client
        ->get('/templates/' . $template_id);
      if ($response
        ->getStatusCode() === 200) {
        $data = json_decode($response
          ->getBody());
        $template = $data->data;
      }
    } catch (\Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
      \Drupal::logger('gathercontent')
        ->error($e
        ->getMessage(), array());
    }
    return $template;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Template::$client private property
Template::getTemplate public function TBD.
Template::getTemplates public function Fetch projects from GatherContent.
Template::getTemplatesObject public function Fetch projects from GatherContent.
Template::__construct public function Account constructor.