PurgeBase.php in Acquia Content Hub 8.2
File
src/EventSubscriber/HandleWebhook/PurgeBase.php
View source
<?php
namespace Drupal\acquia_contenthub\EventSubscriber\HandleWebhook;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\HandleWebhookEvent;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Queue\QueueFactory;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
abstract class PurgeBase implements EventSubscriberInterface {
protected const PURGE = 'purge';
protected $queue;
protected $logger;
public function __construct(QueueFactory $queue_factory, LoggerChannelInterface $logger_channel) {
$this->queue = $queue_factory
->get($this
->getQueueName());
$this->logger = $logger_channel;
}
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::HANDLE_WEBHOOK][] = 'onHandleWebhook';
return $events;
}
public function onHandleWebhook(HandleWebhookEvent $event) {
$payload = $event
->getPayload();
if (self::PURGE !== $payload['crud']) {
return;
}
if ('successful' !== $payload['status']) {
$this->logger
->error('Failed to react on @webhook webhook (@payload).', [
'@webhook' => self::PURGE,
'@payload' => print_r($payload, TRUE),
]);
return;
}
$this
->onPurgeSuccessful();
}
protected function onPurgeSuccessful() {
$this->queue
->deleteQueue();
$this->logger
->info('Queue @queue has been purged successfully.', [
'@queue' => $this
->getQueueName(),
]);
}
protected abstract function getQueueName() : string;
}