src/EventSubscriber/Scorm/ScormSubscriber.php line 34

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\Service\Scorm\Exception\ScormApiException;
  7. use App\Service\Scorm\Exception\ScormGeneratorException;
  8. use App\Service\Scorm\ScormService;
  9. use App\Service\Scorm\ScormTerminateTracker;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Contracts\EventDispatcher\Event;
  14. class ScormSubscriber implements EventSubscriberInterface
  15. {
  16. public function __construct(
  17. private ScormService $scormService,
  18. private ScormTerminateTracker $scormTerminateTracker,
  19. private LoggerInterface $logger,
  20. )
  21. {
  22. }
  23. public static function getSubscribedEvents(): array
  24. {
  25. return [
  26. KernelEvents::TERMINATE => 'terminate',
  27. ];
  28. }
  29. public function terminate(Event $event): void
  30. {
  31. $queued = $this->scormTerminateTracker->extractQueue();
  32. if (empty($queued)) {
  33. return;
  34. }
  35. // Response is already sent: SCORM generation of a whole training can exceed max_execution_time,
  36. // and a timeout between a ScormProxy call and the flush leaves orphans on ScormProxy
  37. set_time_limit(0);
  38. ignore_user_abort(true);
  39. foreach ($queued as $queuedObjectAndVersion) {
  40. $scormVersion = $queuedObjectAndVersion['scormVersion'];
  41. $entity = $queuedObjectAndVersion['entity'];
  42. try {
  43. if ($entity instanceof Lesson) {
  44. $this->scormService->ensureScorm($entity, $scormVersion);
  45. } else if ($entity instanceof Module) {
  46. $this->scormService->ensureModuleProduct($entity, $scormVersion);
  47. } else if ($entity instanceof Training) {
  48. $this->scormService->ensureTrainingProduct($entity, $scormVersion);
  49. }
  50. } catch (ScormGeneratorException|ScormApiException|\Exception $exception) {
  51. $this->logger->error($exception);
  52. }
  53. }
  54. }
  55. }