src/Module/Cms/Subscriber/DetailSlotSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Module\Cms\Subscriber;
  4. use App\Module\Cms\Provider\CmsContentProvider;
  5. use Denios\Data\Product\Product;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. /**
  10. * Class DetailSlotSubscriber
  11. *
  12. * @author André Varelmann <andre.varelmann@bestit-online.de>
  13. * @package App\Module\Cms\Subscriber
  14. */
  15. class DetailSlotSubscriber implements EventSubscriberInterface
  16. {
  17. private CmsContentProvider $provider;
  18. /**
  19. * DetailSlotSubscriber constructor.
  20. *
  21. * @param CmsContentProvider $provider
  22. */
  23. public function __construct(
  24. CmsContentProvider $provider
  25. ) {
  26. $this->provider = $provider;
  27. }
  28. /**
  29. * @return array
  30. */
  31. public static function getSubscribedEvents(): array
  32. {
  33. return [KernelEvents::VIEW => 'onKernelResponse'];
  34. }
  35. /**
  36. * @param ViewEvent $event
  37. *
  38. * @return void
  39. */
  40. public function onKernelResponse(ViewEvent $event): void
  41. {
  42. $controllerResult = $event->getControllerResult();
  43. if (!is_array($controllerResult)
  44. || !isset($controllerResult['product'])
  45. || !$controllerResult['product'] instanceof Product
  46. || count($controllerResult['product']->categories) === 0
  47. ) {
  48. return;
  49. }
  50. $slots = $this->provider->getDetailContent($controllerResult['product']);
  51. if ($slots !== null && is_array($slots)) {
  52. foreach ($slots as $slotName => $slotContent) {
  53. if ($slotContent !== null) {
  54. $controllerResult[$slotName] = $slotContent;
  55. }
  56. }
  57. $event->setControllerResult($controllerResult);
  58. }
  59. }
  60. }