src/Subscriber/BreadcrumbSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Subscriber;
  4. use App\Module\Catalog\Provider\CategoryTreeProvider;
  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. use function array_reverse;
  10. /**
  11. * Class BreadcrumbSubscriber
  12. *
  13. * @author André Varelmann <andre.varelmann@bestit-online.de>
  14. * @package App\Subscriber
  15. */
  16. class BreadcrumbSubscriber implements EventSubscriberInterface
  17. {
  18. /**
  19. * @var PAGE_TYPE_CMS_CONTENT Const for the name of $controllerResult type for cms results
  20. */
  21. private const PAGE_TYPE_CMS_CONTENT = 'cmsContent';
  22. /**
  23. * @var PAGE_TYPE_PRODUCT Const for the name of $controllerResult type for product results
  24. */
  25. private const PAGE_TYPE_PRODUCT = 'product';
  26. /**
  27. * @var PAGE_TYPE_CATEGORY_CONTENT Const for the name of $controllerResult type for category results
  28. */
  29. private const PAGE_TYPE_CATEGORY_CONTENT = 'categoryContent';
  30. /**
  31. * @var CategoryTreeProvider
  32. */
  33. private $provider;
  34. /**
  35. * BreadcrumbSubscriber constructor.
  36. *
  37. * @param CategoryTreeProvider $provider
  38. */
  39. public function __construct(CategoryTreeProvider $provider)
  40. {
  41. $this->provider = $provider;
  42. }
  43. /**
  44. * Returns an array of event names this subscriber wants to listen to.
  45. *
  46. * @return array The event names to listen to
  47. */
  48. public static function getSubscribedEvents(): array
  49. {
  50. return [KernelEvents::VIEW => 'onKernelResponse'];
  51. }
  52. /**
  53. * @param ViewEvent $event
  54. *
  55. * @return void
  56. */
  57. public function onKernelResponse(ViewEvent $event): void
  58. {
  59. $controllerResult = $event->getControllerResult();
  60. if (is_array($controllerResult)) {
  61. if (isset($controllerResult[self::PAGE_TYPE_CMS_CONTENT])) {
  62. $controllerResult['breadcrumbs'] = $this->getBreadcrumbs($controllerResult[self::PAGE_TYPE_CMS_CONTENT]->uuid);
  63. }
  64. if (isset($controllerResult[self::PAGE_TYPE_CATEGORY_CONTENT])) {
  65. $controllerResult['breadcrumbs'] = $this->getBreadcrumbs($controllerResult[self::PAGE_TYPE_CATEGORY_CONTENT]['id']);
  66. }
  67. if (isset($controllerResult[self::PAGE_TYPE_PRODUCT]) && $controllerResult[self::PAGE_TYPE_PRODUCT] instanceof Product) {
  68. $firstCategoryId = current($controllerResult[self::PAGE_TYPE_PRODUCT]->categories);
  69. $controllerResult['breadcrumbs'] = $this->getBreadcrumbs($firstCategoryId);
  70. }
  71. }
  72. $event->setControllerResult($controllerResult);
  73. }
  74. /**
  75. *
  76. * @return array
  77. */
  78. private function getBreadcrumbs($categoryId): array
  79. {
  80. $categories = $this->provider->getFlat();
  81. $breadcrumbs = [];
  82. while (isset($categories[$categoryId])) {
  83. $category = $categories[$categoryId];
  84. $breadcrumb = [
  85. 'name' => $category['name']
  86. ];
  87. if (($category['isActive'] ?? true ) and ($category['inNavigation'] ?? true )) {
  88. $breadcrumb['link'] = $category['slug'];
  89. $breadcrumb['branch'] = $category['branch'] ?? 'cp';
  90. }
  91. $breadcrumbs[] = $breadcrumb;
  92. $categoryId = $category['parent'];
  93. }
  94. return array_reverse($breadcrumbs);
  95. }
  96. }