src/EventSubscriber/JourneySubscriber.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\JourneyEvent;
  4. use App\Event\ModeEvent;
  5. use App\Model\Mode;
  6. use App\Normalizer\DisruptionNormalizer;
  7. use App\Normalizer\JourneyGroupNormalizer;
  8. use App\Service\Internal\DisruptionService;
  9. use App\Service\Internal\JourneyService;
  10. use App\Service\Internal\LogService;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class JourneySubscriber implements EventSubscriberInterface
  14. {
  15. /**
  16. * @var EventDispatcherInterface
  17. */
  18. protected $eventDispatcher;
  19. /**
  20. * @var JourneyService
  21. */
  22. protected $journeyService;
  23. /** @var DisruptionService */
  24. protected $disruptionService;
  25. /**
  26. * @var LogService
  27. */
  28. protected $logService;
  29. /**
  30. * @var string
  31. */
  32. protected $networkId;
  33. /**
  34. * @var JourneyGroupNormalizer
  35. */
  36. protected $journeyGroupNormalizer;
  37. public function __construct(
  38. EventDispatcherInterface $eventDispatcher,
  39. JourneyService $journeyService,
  40. DisruptionService $disruptionService,
  41. LogService $logService,
  42. string $networkId,
  43. JourneyGroupNormalizer $journeyGroupNormalizer
  44. ) {
  45. $this->eventDispatcher = $eventDispatcher;
  46. $this->journeyService = $journeyService;
  47. $this->disruptionService = $disruptionService;
  48. $this->logService = $logService;
  49. $this->networkId = $networkId;
  50. $this->journeyGroupNormalizer = $journeyGroupNormalizer;
  51. }
  52. public static function getSubscribedEvents(): array
  53. {
  54. return [
  55. JourneyEvent::NAME => [
  56. ['journeyCalculation', 0]
  57. ]
  58. ];
  59. }
  60. public function journeyCalculation(JourneyEvent $event)
  61. {
  62. $bodyRequest = $event->getBodyRequest();
  63. $userModes = [];
  64. if (isset($bodyRequest['modes'])) {
  65. $userModes = $bodyRequest['modes'];
  66. unset($bodyRequest['modes']);
  67. }
  68. $modeEvent = new ModeEvent($userModes);
  69. $this->eventDispatcher->dispatch($modeEvent, ModeEvent::NAME);
  70. $bodyRequest['excludedModes'] = $modeEvent->getExcludedModes();
  71. $totalNumberJourneys = 0;
  72. $journeys = [];
  73. $layoutModes = isset ($bodyRequest['layoutMode']) ? [$bodyRequest['layoutMode']] : $modeEvent->getLayoutModes();
  74. $isSubNetworks = '';
  75. $currentDisruptions = $this->disruptionService->getDisruptions($isSubNetworks, DisruptionNormalizer::STATUS_ONGOING);
  76. $disruptions = $currentDisruptions['disruptions'] ?? [];
  77. foreach ($layoutModes as $layout) {
  78. $layoutTotalJourneys = 0;
  79. $journeys[$layout] = $this->journeyService->getJourneysByLayoutMode($layout, $bodyRequest);
  80. if (isset($journeys[$layout]['groups'])) {
  81. $layoutTotalJourneys = $this->getTotalNumberJourneysByLayoutMode($journeys[$layout]['groups']);
  82. $totalNumberJourneys += $layoutTotalJourneys;
  83. $journeys[$layout] = $this->journeyGroupNormalizer->normalize($layout, $journeys[$layout], $disruptions);
  84. }
  85. if (Mode::TAB_TRANSPORT === $layout && 0 === $layoutTotalJourneys) {
  86. $wsUrl = preg_replace(
  87. ['/%1/', '/%2/'],
  88. [$this->networkId, $layout],
  89. JourneyService::PATH_JOURNEYS_BY_LAYOUT_MODE
  90. );
  91. $this->logService->sendLog($wsUrl, JourneyService::LOG_NO_JOURNEYS_FOUND);
  92. }
  93. }
  94. $event->setJourneyResults($journeys);
  95. $event->setTotalNumberJourneys($totalNumberJourneys);
  96. }
  97. public function getTotalNumberJourneysByLayoutMode(array $journeysGroup): int
  98. {
  99. $total = 0;
  100. foreach ($journeysGroup as $group) {
  101. if (isset($group['results']['journeycount'])) {
  102. $total += $group['results']['journeycount'];
  103. }
  104. }
  105. return $total;
  106. }
  107. }