PatchinfoDrupalorgService.php in PatchInfo 8.2
File
modules/patchinfo_drupalorg/src/PatchinfoDrupalorgService.php
View source
<?php
namespace Drupal\patchinfo_drupalorg;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Http\ClientFactory;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\GuzzleException;
class PatchinfoDrupalorgService {
protected $cache;
protected $httpClientFactory;
protected $time;
public function __construct(CacheBackendInterface $cache, ClientFactory $http_client_factory, TimeInterface $time) {
$this->cache = $cache;
$this->httpClientFactory = $http_client_factory;
$this->time = $time;
}
public function getIssue(string $issue_number) {
$cid = 'drupalorgLookupIssue:' . $issue_number;
$cache = $this->cache
->get($cid);
if ($cache) {
return (array) $cache->data;
}
$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 [];
}
}
}