|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\EventSubscriber; |
| 6 | + |
| 7 | +use Ibexa\Contracts\Core\Repository\NotificationService; |
| 8 | +use Ibexa\Contracts\IntegratedHelp\Event\RenderProductTourScenarioEvent; |
| 9 | +use Ibexa\IntegratedHelp\ProductTour\Block\LinkBlock; |
| 10 | +use Ibexa\IntegratedHelp\ProductTour\Block\TextBlock; |
| 11 | +use Ibexa\IntegratedHelp\ProductTour\ProductTourStep; |
| 12 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 13 | + |
| 14 | +final readonly class NotificationScenarioSubscriber implements EventSubscriberInterface |
| 15 | +{ |
| 16 | + public function __construct(private NotificationService $notificationService) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + public static function getSubscribedEvents(): array |
| 21 | + { |
| 22 | + return [ |
| 23 | + RenderProductTourScenarioEvent::class => ['onRenderScenario'], |
| 24 | + ]; |
| 25 | + } |
| 26 | + |
| 27 | + public function onRenderScenario(RenderProductTourScenarioEvent $event): void |
| 28 | + { |
| 29 | + $scenario = $event->getScenario(); |
| 30 | + $steps = $scenario->getSteps(); |
| 31 | + |
| 32 | + if ($scenario->getIdentifier() !== 'notifications') { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + foreach ($steps as $step) { |
| 37 | + $scenario->removeStep($step); |
| 38 | + } |
| 39 | + |
| 40 | + if (!$this->hasUnreadNotifications()) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $customStep = new ProductTourStep(); |
| 45 | + $customStep->setIdentifier('custom_step_identifier'); |
| 46 | + $customStep->setInteractionMode('clickable'); |
| 47 | + $customStep->setTarget('.ibexa-header-user-menu__notifications-toggler'); |
| 48 | + $customStep->setTitle('You have unread notifications'); |
| 49 | + $customStep->addBlock(new TextBlock('Click here to preview your unread notifications.')); |
| 50 | + $customStep->addBlock(new LinkBlock( |
| 51 | + 'https://doc.ibexa.co/projects/userguide/en/latest/getting_started/notifications/', |
| 52 | + 'Learn more about notifications' |
| 53 | + )); |
| 54 | + |
| 55 | + $scenario->addStep($customStep); |
| 56 | + } |
| 57 | + |
| 58 | + private function hasUnreadNotifications(): bool |
| 59 | + { |
| 60 | + return $this->notificationService->getPendingNotificationCount() > 0; |
| 61 | + } |
| 62 | +} |
0 commit comments