Skip to content
Snippets Groups Projects

Issue #3221390: Add better fencing to the getA11yProjectChecklist function in the service

3 files
+ 80
29
Compare changes
  • Side-by-side
  • Inline

Files

+ 65
17
@@ -3,12 +3,59 @@
namespace Drupal\a11yproject_checklist\Service;
use Drupal\Component\Serialization\Json;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use GuzzleHttp\ClientInterface;
/**
* The A11YProjectChecklist service.
*/
class A11yProjectChecklist {
/**
* An http client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Logger Factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* Messenger.
*
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
/**
* {@inheritdoc}
*/
public function __construct(ClientInterface $httpClient, Messenger $messenger, LoggerChannelFactoryInterface $logger_factory) {
$this->httpClient = $httpClient;
$this->messenger = $messenger;
$this->loggerFactory = $logger_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('http_client'),
$container->get('logger.factory'),
$container->get('messenger')
);
}
/**
* Gets the A11y Project Checklist from JSON and converts to PHP.
*
@@ -17,7 +64,6 @@ class A11yProjectChecklist {
*/
public function getA11yProjectChecklist() {
// Where the checklist lives.
$endpoint = [];
$endpoint = 'https://raw.githubusercontent.com/a11yproject/a11yproject.com/main/src/_data/checklists.json';
// Set some request options.
@@ -26,28 +72,30 @@ class A11yProjectChecklist {
'connect_timeout' => 30,
];
// Make the request for the checklist.
try {
$client = [];
$client = \Drupal::httpClient();
$request = [];
$client = $this->httpClient;
$request = $client->request('GET', $endpoint, $options);
if ($request->getStatusCode() == 200) {
// Get the JSON.
$checklist_json = [];
$checklist_json = $request->getBody()->getContents();
// Convert it to PHP.
$a11yproject_checklist = [];
$a11yproject_checklist = Json::decode($checklist_json);
return $a11yproject_checklist;
}
}
catch (ConnectException $e) {
// Connection exception.
$this->loggerFactory->get('a11yproject_checklist')->error($e->getMessage());
$this->messenger->addError($e->getMessage());
}
catch (RequestException $e) {
// Log the error if it fails.
watchdog_exception('a11yproject_checklist', $e);
// Request exception.
$this->loggerFactory->get('a11yproject_checklist')->error($e->getMessage());
$this->messenger->addError($e->getMessage());
}
// Get the JSON.
$checklist_json = [];
$checklist_json = $request->getBody()->getContents();
// Convert it to PHP.
$a11yproject_checklist = [];
$a11yproject_checklist = Json::decode($checklist_json);
return $a11yproject_checklist;
}
}
Loading