You are here

class DrupalGatherContentClient in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 src/DrupalGatherContentClient.php \Drupal\gathercontent\DrupalGatherContentClient

Extends the GatherContentClient class with Drupal specific functionality.

Hierarchy

Expanded class hierarchy of DrupalGatherContentClient

4 files declare their use of DrupalGatherContentClient
gathercontent.drush.inc in ./gathercontent.drush.inc
Drush command to cli config import.
MappingImportForm.php in gathercontent_ui/src/Form/MappingImportForm.php
MappingListBuilder.php in gathercontent_ui/src/MappingListBuilder.php
MockDrupalGatherContentClient.php in tests/modules/gathercontent_test/src/MockDrupalGatherContentClient.php
1 string reference to 'DrupalGatherContentClient'
gathercontent.services.yml in ./gathercontent.services.yml
gathercontent.services.yml
1 service uses DrupalGatherContentClient
gathercontent.client in ./gathercontent.services.yml
Drupal\gathercontent\DrupalGatherContentClient

File

src/DrupalGatherContentClient.php, line 12

Namespace

Drupal\gathercontent
View source
class DrupalGatherContentClient extends GatherContentClient {

  /**
   * {@inheritdoc}
   */
  public function __construct(ClientInterface $client) {
    parent::__construct($client);
    $this
      ->setCredentials();
  }

  /**
   * Put the authentication config into client.
   */
  public function setCredentials() {
    $config = \Drupal::config('gathercontent.settings');
    $this
      ->setEmail($config
      ->get('gathercontent_username') ?: '');
    $this
      ->setApiKey($config
      ->get('gathercontent_api_key') ?: '');
  }

  /**
   * Retrieve the account id of the given account.
   *
   * If none given, retrieve the first account by default.
   */
  public static function getAccountId($account_name = NULL) {
    $account = \Drupal::config('gathercontent.settings')
      ->get('gathercontent_account');
    $account = unserialize($account);
    if (!is_array($account)) {
      return NULL;
    }
    if (!$account_name) {
      if (reset($account)) {
        return key($account);
      }
    }
    foreach ($account as $id => $name) {
      if ($name === $account_name) {
        return $id;
      }
    }
    return NULL;
  }

  /**
   * Retrieve all the active projects.
   */
  public function getActiveProjects($account_id) {
    $projects = $this
      ->projectsGet($account_id);
    foreach ($projects as $id => $project) {
      if (!$project->active) {
        unset($projects[$id]);
      }
    }
    return $projects;
  }

  /**
   * Returns a formatted array with the template ID's as a key.
   *
   * @param int $project_id
   *   Project ID.
   *
   * @return array
   *   Return array.
   */
  public function getTemplatesOptionArray($project_id) {
    $formatted = [];
    $templates = $this
      ->templatesGet($project_id);
    foreach ($templates as $id => $template) {
      $formatted[$id] = $template->name;
    }
    return $formatted;
  }

  /**
   * Returns the response body.
   *
   * @param bool $json_decoded
   *   If TRUE the method will return the body json_decoded.
   *
   * @return \Psr\Http\Message\StreamInterface
   *   Response body.
   */
  public function getBody($json_decoded = FALSE) {
    $body = $this
      ->getResponse()
      ->getBody();
    if ($json_decoded) {
      return \GuzzleHttp\json_decode($body);
    }
    return $body;
  }

  /**
   * Downloads all files asynchronously.
   *
   * @param array $files
   *   Files object array.
   * @param string $directory
   *   Destination directory.
   * @param string $language
   *   Language string.
   *
   * @return array
   *   Imported files array.
   */
  public function downloadFiles(array $files, $directory, $language) {

    /** @var \GuzzleHttp\Client $httpClient */
    $httpClient = $this->client;
    $options = [
      'auth' => $this
        ->getRequestAuth(),
      'headers' => [],
    ];
    $options['headers'] += $this
      ->getRequestHeaders();
    $files = array_values($files);
    $importedFiles = [];
    $requests = function () use ($httpClient, $files, $options) {
      foreach ($files as $file) {
        $url = $this
          ->getUri("files/{$file->id}/download");
        (yield function () use ($httpClient, $url, $options) {
          return $httpClient
            ->getAsync($url, $options);
        });
      }
    };
    $pool = new Pool($httpClient, $requests(), [
      'fulfilled' => function ($response, $index) use ($files, $directory, $language, &$importedFiles) {
        if ($response
          ->getStatusCode() === 200) {
          $path = $directory . '/' . $files[$index]->fileName;
          $importedFile = file_save_data($response
            ->getBody(), $path);
          if ($importedFile) {
            $importedFile
              ->set('gc_id', $files[$index]->id)
              ->set('langcode', $language)
              ->set('filesize', $files[$index]->size)
              ->save();
            $importedFiles[$index] = $importedFile
              ->id();
          }
        }
      },
    ]);
    $promise = $pool
      ->promise();
    $promise
      ->wait();
    ksort($importedFiles);
    return $importedFiles;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalGatherContentClient::downloadFiles public function Downloads all files asynchronously. 1
DrupalGatherContentClient::getAccountId public static function Retrieve the account id of the given account.
DrupalGatherContentClient::getActiveProjects public function Retrieve all the active projects.
DrupalGatherContentClient::getBody public function Returns the response body.
DrupalGatherContentClient::getTemplatesOptionArray public function Returns a formatted array with the template ID's as a key.
DrupalGatherContentClient::setCredentials public function Put the authentication config into client.
DrupalGatherContentClient::__construct public function