src/EventSubscriber/DashboardWidgetSubscriber.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\DashboardWidget;
  5. use App\Manager\DashboardWidgetManager;
  6. use App\Manager\MissionManager;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\String\UnicodeString;
  12. class DashboardWidgetSubscriber implements EventSubscriberInterface
  13. {
  14.     private DashboardWidgetManager $dashboardWidgetManager;
  15.     private MissionManager $missionManager;
  16.     public function __construct(DashboardWidgetManager $dashboardWidgetManagerMissionManager $missionManager)
  17.     {
  18.         $this->dashboardWidgetManager $dashboardWidgetManager;
  19.         $this->missionManager $missionManager;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             KernelEvents::VIEW => [
  25.                 ['apiPreCreate'EventPriorities::PRE_WRITE],
  26.                 ['apiPreUpdate'EventPriorities::PRE_WRITE],
  27.                 ['apiPreDelete'EventPriorities::PRE_WRITE],
  28.             ],
  29.         ];
  30.     }
  31.     public function apiPreCreate(ViewEvent $event)
  32.     {
  33.         $dashboardWidget $event->getControllerResult();
  34.         $method $event->getRequest()->getMethod();
  35.         $operation $event->getRequest()->attributes->get('_api_item_operation_name');
  36.         if (!$this->isDashboardWidget($dashboardWidget) || $method !== Request::METHOD_POST || $operation === 'image_upload_put') {
  37.             return;
  38.         }
  39.         
  40.         $dashboardWidget $this->dashboardWidgetManager->checkTitle($dashboardWidget);
  41.         $this->preCreate($dashboardWidget);
  42.     }
  43.     public function preCreate(DashboardWidget $dashboardWidget)
  44.     {
  45.         $dashboardWidget->setPosition($this->dashboardWidgetManager->calculateNextPosition($dashboardWidget->getDashboard()));
  46.         $this->dashboardWidgetManager->checkAccess($dashboardWidget'write');
  47.         $settings =  $dashboardWidget->getSettings();
  48.         if (isset($settings['missionInstructions'])){
  49.             $dataSource $dashboardWidget->getDatasource();
  50.            
  51.             $mission =  $this->missionManager->getMissionByIdOrUuid($dataSource['missions'][0]);
  52.             
  53.             $settings['missionInstructions'] = html_entity_decode(strip_tags($mission->getInstructions()));
  54.             $dashboardWidget->setSettings($settings);
  55.         }
  56.        
  57.     }
  58.     public function apiPreUpdate(ViewEvent $event)
  59.     {
  60.         $dashboardWidget $event->getControllerResult();
  61.         $method $event->getRequest()->getMethod();
  62.         if (!$this->isDashboardWidget($dashboardWidget) || $method !== Request::METHOD_PUT) {
  63.             return;
  64.         }
  65.         $this->preUpdate($dashboardWidget);
  66.     }
  67.     public function preUpdate(DashboardWidget $dashboardWidget)
  68.     {
  69.         $this->dashboardWidgetManager->checkAccess($dashboardWidget'write');
  70.     }
  71.     public function apiPreDelete(ViewEvent $event)
  72.     {
  73.         $dashboardWidget $event->getControllerResult();
  74.         $method $event->getRequest()->getMethod();
  75.         if (!$this->isDashboardWidget($dashboardWidget) || $method !== Request::METHOD_DELETE) {
  76.             return;
  77.         }
  78.         $this->preUpdate($dashboardWidget);
  79.     }
  80.     private function isDashboardWidget($dashboardWidget): bool
  81.     {
  82.         return $dashboardWidget instanceof DashboardWidget;
  83.     }
  84. }