headless_cms-1.0.3/modules/headless_cms_notify/src/HeadlessNotifyService.php

modules/headless_cms_notify/src/HeadlessNotifyService.php
<?php

declare(strict_types=1);

namespace Drupal\headless_cms_notify;

use Drupal\Core\Entity\EntityInterface;
use Drupal\headless_cms_notify\Event\BeforeHeadlessNotifyEvent;
use Drupal\headless_cms_notify\NotifyMessage\HeadlessNotifyCacheRebuildMessage;
use Drupal\headless_cms_notify\NotifyMessage\HeadlessNotifyEntityOperationMessage;
use Drupal\headless_cms_notify\NotifyMessage\HeadlessNotifyMessageInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Provides a service for sending notifications.
 */
class HeadlessNotifyService {

  public function __construct(
    protected readonly ConsumerHeadlessNotifyManager $consumerManager,
    protected readonly HeadlessNotifyTransportService $transportService,
    #[Autowire(service: 'event_dispatcher')]
    protected readonly EventDispatcherInterface $eventDispatcher,
  ) {}

  /**
   * Send a notification for an entity operation.
   *
   * Checks if the notification type and entity type are enabled
   * for the negotiated consumer. Sends the notification via the
   * configured transport.
   */
  public function sendEntityOperationNotification(HeadlessNotificationEntityOperation $operation, EntityInterface $entity) {
    $applicableConsumers = $this->consumerManager->getApplicableConsumers(HeadlessNotifyEntityOperationMessage::getMessageTypeId(), $entity->getEntityTypeId());
    if (empty($applicableConsumers)) {
      return;
    }

    $message = new HeadlessNotifyEntityOperationMessage($entity, $operation);
    $this->sendNotifyMessage($message);
  }

  /**
   * Send a notification for cache rebuild.
   *
   * Checks if the notification type is enabled for the negotiated
   * consumer. Sends the notification via the configured transport.
   */
  public function sendCacheRebuildNotification() {
    $message = new HeadlessNotifyCacheRebuildMessage();
    $this->sendNotifyMessage($message);
  }

  /**
   * Send a notification message.
   */
  public function sendNotifyMessage(HeadlessNotifyMessageInterface $message) {
    $event = new BeforeHeadlessNotifyEvent($message);
    $this->eventDispatcher->dispatch($event, BeforeHeadlessNotifyEvent::EVENT_NAME);

    if ($event->isAborted()) {
      return;
    }

    $applicableConsumers = $this->consumerManager->getApplicableConsumers($message::getMessageTypeId());
    if (empty($applicableConsumers)) {
      return;
    }

    foreach ($applicableConsumers as $consumer) {
      $transport = $this->consumerManager->getNotifyTransport($consumer);
      $this->transportService->send($message, $transport);
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc