You are here

class PatchinfoDrupalorgService in PatchInfo 8.2

Class PatchinfoDrupalorgService contains commonly shared utilities.

@package Drupal\patchinfo_drupalorg

Hierarchy

Expanded class hierarchy of PatchinfoDrupalorgService

1 file declares its use of PatchinfoDrupalorgService
PatchInfoDrupalorgCommands.php in modules/patchinfo_drupalorg/src/Commands/PatchInfoDrupalorgCommands.php
1 string reference to 'PatchinfoDrupalorgService'
patchinfo_drupalorg.services.yml in modules/patchinfo_drupalorg/patchinfo_drupalorg.services.yml
modules/patchinfo_drupalorg/patchinfo_drupalorg.services.yml
1 service uses PatchinfoDrupalorgService
patchinfo_drupalorg.patchinfo_drupalorg_service in modules/patchinfo_drupalorg/patchinfo_drupalorg.services.yml
Drupal\patchinfo_drupalorg\PatchinfoDrupalorgService

File

modules/patchinfo_drupalorg/src/PatchinfoDrupalorgService.php, line 16

Namespace

Drupal\patchinfo_drupalorg
View source
class PatchinfoDrupalorgService {

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * The HTTP client factory.
   *
   * @var \Drupal\Core\Http\ClientFactory
   */
  protected $httpClientFactory;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs a new PatchinfoDrupalorgService instance.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend.
   * @param \Drupal\Core\Http\ClientFactory $http_client_factory
   *   The HTTP client factory.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  public function __construct(CacheBackendInterface $cache, ClientFactory $http_client_factory, TimeInterface $time) {
    $this->cache = $cache;
    $this->httpClientFactory = $http_client_factory;
    $this->time = $time;
  }

  /**
   * Gets information about issue on drupal.org.
   *
   * @param string $issue_number
   *   Issue number on drupal.org.
   *
   * @return array
   *   Array with field information.
   */
  public function getIssue(string $issue_number) {
    $cid = 'drupalorgLookupIssue:' . $issue_number;
    $cache = $this->cache
      ->get($cid);
    if ($cache) {
      return (array) $cache->data;
    }

    // Get url content from Guzzle.
    $http_client = $this->httpClientFactory
      ->fromOptions([
      'headers' => [
        'Content-Type' => [
          'application/json',
        ],
      ],
      'timeout' => 10,
    ]);
    $service_url = 'https://www.drupal.org/api-d7/node.json?nid=' . $issue_number;
    try {
      $response = $http_client
        ->request('GET', $service_url);
      $reponse_body = $response
        ->getBody();
      $decoded = json_decode($reponse_body);
      if ($response
        ->getStatusCode() !== 200) {
        drush_print('Error retrieving issue ' . $issue_number . ' , error:' . $response
          ->getReasonPhrase());
        return [];
      }
      $composer_module_issue = reset($decoded->list);
      $this->cache
        ->set($cid, $composer_module_issue, $this->time
        ->getRequestTime() + 3700);
      return (array) $composer_module_issue;
    } catch (RequestException $e) {
      drush_print('Error retrieving issue ' . $issue_number . ' , error:' . $e
        ->getMessage());
      return [];
    } catch (GuzzleException $e) {
      drush_print('Error retrieving issue ' . $issue_number . ' , error:' . $e
        ->getMessage());
      return [];
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PatchinfoDrupalorgService::$cache protected property The cache backend.
PatchinfoDrupalorgService::$httpClientFactory protected property The HTTP client factory.
PatchinfoDrupalorgService::$time protected property The time service.
PatchinfoDrupalorgService::getIssue public function Gets information about issue on drupal.org.
PatchinfoDrupalorgService::__construct public function Constructs a new PatchinfoDrupalorgService instance.