Report.php in Acquia Content Hub 8.2
File
src/EventSubscriber/HandleWebhook/Report.php
View source
<?php
namespace Drupal\acquia_contenthub\EventSubscriber\HandleWebhook;
use Acquia\Hmac\ResponseSigner;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\ContentHubCommonActions;
use Drupal\acquia_contenthub\Event\HandleWebhookEvent;
use Drupal\Core\Extension\ModuleExtensionList;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use Laminas\Diactoros\ResponseFactory;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\StreamFactory;
use Laminas\Diactoros\UploadedFileFactory;
use Psr\Http\Message\ResponseInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
class Report implements EventSubscriberInterface {
const PAYLOAD_REPORT = 'report';
protected $common;
protected $moduleList;
public function __construct(ContentHubCommonActions $common, ModuleExtensionList $module_list) {
$this->common = $common;
$this->moduleList = $module_list;
}
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::HANDLE_WEBHOOK][] = [
'onHandleWebhook',
1000,
];
return $events;
}
public function onHandleWebhook(HandleWebhookEvent $event) : void {
$payload = $event
->getPayload();
if ('successful' !== $payload['status'] || self::PAYLOAD_REPORT !== $payload['crud']) {
return;
}
$response = $this
->getResponse($event, json_encode($this
->getReport()));
$event
->setResponse($response);
$event
->stopPropagation();
}
protected function getResponse(HandleWebhookEvent $event, string $body) : ResponseInterface {
$response = new GuzzleResponse(Response::HTTP_OK, [], $body);
if (class_exists(DiactorosFactory::class)) {
$httpMessageFactory = new DiactorosFactory();
}
else {
$httpMessageFactory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
}
$psr7_request = $httpMessageFactory
->createRequest($event
->getRequest());
$signer = new ResponseSigner($event
->getKey(), $psr7_request);
$signedResponse = $signer
->signResponse($response);
return $signedResponse;
}
protected function getReport() : array {
return [
'modules' => $this->moduleList
->getAllInstalledInfo(),
'updatedb-status' => $this->common
->getUpdateDbStatus(),
];
}
}
Classes
Name |
Description |
Report |
Handles webhooks with a payload requesting site report. |