packages/sylius-google-analytics-plugin/src/Factory/Events/NavigationEventFactory.php line 50

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Jakub Lech <info@smartbyte.pl>
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. declare(strict_types=1);
  9. namespace Spinbits\SyliusGoogleAnalytics4Plugin\Factory\Events;
  10. use Spinbits\GoogleAnalytics4EventsDtoS\Events\Search;
  11. use Spinbits\GoogleAnalytics4EventsDtoS\Events\ViewItem;
  12. use Spinbits\GoogleAnalytics4EventsDtoS\Events\ViewItemList;
  13. use Spinbits\GoogleAnalytics4EventsDtoS\Events\ItemsContainerInterface;
  14. use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\ItemFactory;
  15. use Sylius\Component\Core\Model\ProductInterface;
  16. use Sylius\Component\Product\Model\ProductInterface as ProdProductInterface;
  17. class NavigationEventFactory
  18. {
  19.     private ItemFactory $itemFactory;
  20.     public function __construct(ItemFactory $itemFactory)
  21.     {
  22.         $this->itemFactory $itemFactory;
  23.     }
  24.     public function search(string $searchTerm): Search
  25.     {
  26.         return new Search($searchTerm);
  27.     }
  28.     /**
  29.      * @param array<array-key, ProductInterface> $products
  30.      * @param null|string $itemListId
  31.      * @param null|string $itemListName
  32.      * @return ViewItemList
  33.      */
  34.     public function viewItemList(
  35.         array $products = [],
  36.         ?string $itemListId null,
  37.         ?string $itemListName null
  38.     ): ViewItemList
  39.     {
  40.         $viewItemList = (new ViewItemList())
  41.             ->setItemListId($itemListId)
  42.             ->setItemListName($itemListName);
  43.         $this->addItems($viewItemList$products);
  44.         return $viewItemList;
  45.     }
  46.     public function viewItem(ProductInterface $product): ViewItem
  47.     {
  48.         $viewItem = new ViewItem();
  49.         $viewItem->addItem($this->itemFactory->fromProduct($product));
  50.         return $viewItem;
  51.     }
  52.     /**
  53.      * @param ItemsContainerInterface $event
  54.      * @param array<array-key, ProdProductInterface>|ProductInterface[] $products
  55.      * @return void
  56.      */
  57.     private function addItems(ItemsContainerInterface $event, array $products): void
  58.     {
  59.         foreach ($products as $product) {
  60.             $event->addItem(
  61.                 $this->itemFactory->fromProduct($product)
  62.             );
  63.         }
  64.     }
  65. }