vendor/ibexa/activity-log/src/bundle/Event/SiteFactoryEventsListener.php line 37

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\ActivityLog\Event;
  8. use Ibexa\Contracts\ActivityLog\ActivityLogServiceInterface;
  9. use Ibexa\Contracts\SiteFactory\Events\CreateSiteEvent;
  10. use Ibexa\Contracts\SiteFactory\Events\DeleteSiteEvent;
  11. use Ibexa\Contracts\SiteFactory\Events\UpdateSiteEvent;
  12. use Ibexa\Contracts\SiteFactory\Values\Site\Site;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. final class SiteFactoryEventsListener implements EventSubscriberInterface
  15. {
  16.     private ActivityLogServiceInterface $activityLogService;
  17.     public function __construct(
  18.         ActivityLogServiceInterface $activityLogService
  19.     ) {
  20.         $this->activityLogService $activityLogService;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CreateSiteEvent::class => ['onCreate'],
  26.             DeleteSiteEvent::class => ['onDelete'],
  27.             UpdateSiteEvent::class => ['onUpdate'],
  28.         ];
  29.     }
  30.     public function onCreate(CreateSiteEvent $event): void
  31.     {
  32.         $site $event->getSite();
  33.         $this->saveActivityLog('create'$site->id);
  34.     }
  35.     public function onDelete(DeleteSiteEvent $event): void
  36.     {
  37.         $site $event->getSite();
  38.         $this->saveActivityLog('delete'$site->id);
  39.     }
  40.     public function onUpdate(UpdateSiteEvent $event): void
  41.     {
  42.         $site $event->getSite();
  43.         $this->saveActivityLog('update'$site->id);
  44.     }
  45.     private function saveActivityLog(string $actionint $id): void
  46.     {
  47.         $activityLog $this->activityLogService->build(Site::class, (string)$id$action);
  48.         $this->activityLogService->save($activityLog);
  49.     }
  50. }