src/EventSubscriber/Scorm/ScormSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Scorm;
  3. use App\Entity\Scholar\Lesson\Lesson;
  4. use App\Entity\Scholar\Module\Module;
  5. use App\Entity\Scholar\Training\Training;
  6. use App\Enum\Scorm\ScormVersion;
  7. use App\Service\Scorm\Exception\ScormApiException;
  8. use App\Service\Scorm\Exception\ScormGeneratorException;
  9. use App\Service\Scorm\ScormService;
  10. use App\Service\Scorm\ScormTerminateTracker;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Contracts\EventDispatcher\Event;
  15. class ScormSubscriber implements EventSubscriberInterface
  16. {
  17.     public function __construct(
  18.         private ScormService          $scormService,
  19.         private ScormTerminateTracker $scormTerminateTracker,
  20.         private LoggerInterface       $logger,
  21.     )
  22.     {
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             KernelEvents::TERMINATE => 'terminate',
  28.         ];
  29.     }
  30.     public function terminate(Event $event): void
  31.     {
  32.         $queued $this->scormTerminateTracker->extractQueue();
  33.         if (empty($queued) || !$this->scormService->isEnabledAutoProcess()) {
  34.             return;
  35.         }
  36.         foreach ($queued as $queuedObject) {
  37.             try {
  38.                 if ($queuedObject instanceof Lesson) {
  39.                     $this->scormService->ensureScorm($queuedObjectScormVersion::V12);
  40.                     $this->scormService->ensureScorm($queuedObjectScormVersion::V2004);
  41.                 } else if ($queuedObject instanceof Module) {
  42.                     $this->scormService->ensureModuleProduct($queuedObjectScormVersion::V12);
  43.                     $this->scormService->ensureModuleProduct($queuedObjectScormVersion::V2004);
  44.                 } else if ($queuedObject instanceof Training) {
  45.                     $this->scormService->ensureTrainingProduct($queuedObjectScormVersion::V12);
  46.                     $this->scormService->ensureTrainingProduct($queuedObjectScormVersion::V2004);
  47.                 }
  48.             } catch (ScormGeneratorException|ScormApiException|\Exception $exception) {
  49.                 $this->logger->error($exception);
  50.             }
  51.         }
  52.     }
  53. }