You are here

class Template in GatherContent 8.3

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

Class Template.

@package GatherContent

Hierarchy

  • class \Drupal\gathercontent\DAO\Template

Expanded class hierarchy of Template

4 files declare their use of Template
gathercontent.module in ./gathercontent.module
Main module file for GatherContent module.
MappingEditForm.php in src/Form/MappingEditForm.php
MappingImportForm.php in src/Form/MappingImportForm.php
MappingListBuilder.php in src/MappingListBuilder.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.", []);
    }
    $this->client = new Client([
      'base_uri' => 'https://api.gathercontent.com',
      'auth' => [
        $username,
        $api_key,
      ],
      'headers' => [
        'Accept' => 'application/vnd.gathercontent.v0.5+json',
      ],
    ]);
  }

  /**
   * Fetch projects from GatherContent.
   *
   * @return array
   *   Associative array of projects.
   */
  public function getTemplates($project_id) {
    $templates = [];
    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(), []);
    }
    return $templates;
  }

  /**
   * Fetch projects from GatherContent.
   *
   * @return array
   *   Associative array of projects.
   */
  public function getTemplatesObject($project_id) {
    $templates = [];
    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(), []);
    }
    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(), []);
    }
    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.