src/Domain/EventSubscriber/LogbookEventSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Domain\EventSubscriber;
  4. use CalendarBundle\CalendarEvents;
  5. use CalendarBundle\Entity\Event;
  6. use CalendarBundle\Event\CalendarEvent;
  7. use Domain\Repository\LogEntryRepositoryInterface;
  8. use Infrastructure\Persistence\Doctrine\Entity\LogEntry;
  9. use Infrastructure\Persistence\Doctrine\Entity\User;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. final class LogbookEventSubscriber implements EventSubscriberInterface
  16. {
  17.     private const COLOR_BLUE '#007bff';
  18.     private LogEntryRepositoryInterface $logEntryRepository;
  19.     private UrlGeneratorInterface $urlGenerator;
  20.     private TokenStorageInterface $tokenStorage;
  21.     private RequestStack $requestStack;
  22.     private LoggerInterface $logger;
  23.     public function __construct(
  24.         LogEntryRepositoryInterface $logEntryRepository,
  25.         UrlGeneratorInterface $urlGenerator,
  26.         TokenStorageInterface $tokenStorage,
  27.         RequestStack $requestStack,
  28.         LoggerInterface $logger
  29.     ) {
  30.         $this->logEntryRepository $logEntryRepository;
  31.         $this->urlGenerator $urlGenerator;
  32.         $this->tokenStorage $tokenStorage;
  33.         $this->requestStack $requestStack;
  34.         $this->logger $logger;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             CalendarEvents::SET_DATA => 'onCalendarSetData',
  40.         ];
  41.     }
  42.     public function onCalendarSetData(CalendarEvent $calendar)
  43.     {
  44.         $start $calendar->getStart();
  45.         $end $calendar->getEnd();
  46.         $filters $calendar->getFilters();
  47.         // You may want to make a custom query from your database to fill the calendar
  48.         /** @var User $currentUser */
  49.         $currentUser $this->tokenStorage->getToken()->getUser();
  50.         $logEntries $this->fetchLogEntries($currentUser$start$end$filters['companyId'] ?? null);
  51.         array_map(function (LogEntry $logEntry) use ($calendar) {
  52.             $colorOfLogEntry $this->colorizeLogEntry($logEntry);
  53.             $calendar->addEvent(new Event(
  54.                 sprintf('%s: %s'$logEntry->getUser()->getFullName(), $logEntry->getText()),
  55.                 $logEntry->getCreatedAt(),
  56.                 $logEntry->getCreatedAt(),
  57.                 [
  58.                 'backgroundColor' => $colorOfLogEntry,
  59.                 'borderColor' => $colorOfLogEntry,
  60.                 'textColor' => 'white',
  61.                 'url' => $this->urlGenerator->generate('fetch_logentry_for_company', [
  62.                     'companyId' => $logEntry->getCompany()->getId(),
  63.                     'logEntryId' => $logEntry->getId(),
  64.                 ]),
  65.             ]
  66.             ));
  67.         }, $logEntries);
  68.     }
  69.     private function colorizeLogEntry(LogEntry $logEntry): string
  70.     {
  71.         if (!$logEntry->isCommentedAndLocked() && $logEntry->getUser()->isClient()) {
  72.             return 'orange';
  73.         }
  74.         if ($logEntry->isCommentedAndLocked()) {
  75.             return 'lightgrey';
  76.         }
  77.         return self::COLOR_BLUE;
  78.     }
  79.     public function fetchLogEntries(User $currentUser\DateTimeInterface $start\DateTimeInterface $end, ?int $companyId null): array
  80.     {
  81.         if ($currentUser->isClient() || $currentUser->isCleaner()) {
  82.             return $this->logEntryRepository->byCompanyId($currentUser->getCompany()->getId());
  83.         }
  84.         if (\is_int($companyId) && $currentUser->isManager()) {
  85.             return $this->logEntryRepository->allInGivenMonthForCompany($start$end$companyId);
  86.         }
  87.         if (null === $companyId && $currentUser->isManager()) {
  88.             return $this->logEntryRepository->allInCurrentMonth();
  89.         }
  90.         return [];
  91.     }
  92. }