custom/plugins/GbmedEnev/src/Service/Product.php line 134

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * gb media
  4.  * All Rights Reserved.
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  7.  * The content of this file is proprietary and confidential.
  8.  *
  9.  * @category       Shopware
  10.  * @package        Shopware_Plugins
  11.  * @subpackage     GbmedEnev
  12.  * @copyright      Copyright (c) 2019, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. namespace Gbmed\Enev\Service;
  18. use Gbmed\Enev\Core\Content\Enev\EnevCollection;
  19. use Gbmed\Enev\Core\Content\Enev\EnevEntity;
  20. use Shopware\Core\Content\Product\ProductEntity;
  21. use Shopware\Core\Framework\Api\Response\Type\Api\JsonApiType;
  22. use Shopware\Core\Framework\Context;
  23. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  24. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  25. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  26. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  27. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  28. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  29. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  30. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  31. use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder;
  32. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  33. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  34. use Symfony\Component\HttpFoundation\Request;
  35. class Product
  36. {
  37.     /** @var EntityRepositoryInterface */
  38.     private $gbmedEnevRepository;
  39.     /** @var EntityRepositoryInterface */
  40.     private $productRepository;
  41.     /** @var RequestCriteriaBuilder */
  42.     private $searchCriteriaBuilder;
  43.     private JsonApiType $responseFactory;
  44.     private Eprel $serviceEprel;
  45.     /**
  46.      * Product constructor.
  47.      *
  48.      * @param EntityRepositoryInterface $gbmedEnevRepository
  49.      * @param EntityRepositoryInterface $productRepository
  50.      * @param RequestCriteriaBuilder $searchCriteriaBuilder
  51.      * @param JsonApiType $responseFactory
  52.      * @param Eprel $serviceEprel
  53.      */
  54.     public function __construct(
  55.         EntityRepositoryInterface $gbmedEnevRepository,
  56.         EntityRepositoryInterface $productRepository,
  57.         RequestCriteriaBuilder $searchCriteriaBuilder,
  58.         JsonApiType $responseFactory,
  59.         Eprel $serviceEprel
  60.     ) {
  61.         $this->gbmedEnevRepository $gbmedEnevRepository;
  62.         $this->productRepository $productRepository;
  63.         $this->searchCriteriaBuilder $searchCriteriaBuilder;
  64.         $this->responseFactory $responseFactory;
  65.         $this->serviceEprel $serviceEprel;
  66.     }
  67.     /**
  68.      * find products ex gbmedEnev products
  69.      *
  70.      * @param Request $request
  71.      * @param Context $context
  72.      *
  73.      * @return EntitySearchResult
  74.      * @throws InconsistentCriteriaIdsException
  75.      */
  76.     public function products(Request $requestContext $context): EntitySearchResult
  77.     {
  78.         $products $this->enevProducts($context);
  79.         $criteria = new Criteria();
  80.         $criteria $this->searchCriteriaBuilder->handleRequest(
  81.             $request,
  82.             $criteria,
  83.             $this->productRepository->getDefinition(),
  84.             $context
  85.         );
  86.         if (count($products)) {
  87.             $criteria->addFilter(new NotFilter(NotFilter::CONNECTION_AND,
  88.                 [new EqualsAnyFilter('product.id'$products)]));
  89.         }
  90.         return $this->productRepository->search($criteria$context);
  91.     }
  92.     /**
  93.      * get product IDs from EnEV
  94.      *
  95.      * @param Context $context
  96.      *
  97.      * @return array
  98.      * @throws InconsistentCriteriaIdsException
  99.      */
  100.     public function enevProducts(Context $context): array
  101.     {
  102.         $enevs $this->gbmedEnevRepository->search(new Criteria(), $context);
  103.         $products = [];
  104.         /**
  105.          * @var EnevCollection $enev
  106.          */
  107.         foreach ($enevs->getElements() as $index => $enev) {
  108.             $products[] = $enev->get('product')->getId();
  109.         }
  110.         return $products;
  111.     }
  112.     /**
  113.      * find enev by product
  114.      *
  115.      * @param $productId
  116.      * @param Context $context
  117.      * @param bool $activeOnly
  118.      * @return EntitySearchResult
  119.      * @throws InconsistentCriteriaIdsException
  120.      */
  121.     public function findEnevByProductId(
  122.         $productId,
  123.         Context $context,
  124.         $activeOnly true
  125.     ): EntitySearchResult {
  126.         $criteria = new Criteria();
  127.         $criteria->addFilter(new EqualsFilter('gbmed_enev.productId'$productId))
  128.             ->addSorting(new FieldSorting('position'))
  129.             ->addAssociation('media')
  130.             ->addAssociation('datasheet')
  131.             ->addAssociation('icon');
  132.         if ($activeOnly) {
  133.             $criteria->addFilter(new EqualsFilter('gbmed_enev.active'true));
  134.         }
  135.         return $this->gbmedEnevRepository->search($criteria$context);
  136.     }
  137.     /**
  138.      * @param $productId
  139.      * @param Context $context
  140.      * @return ProductEntity|null
  141.      */
  142.     public function findProductById(
  143.         $productId,
  144.         Context $context
  145.     ) {
  146.         $criteria = new Criteria();
  147.         $criteria->addFilter(new EqualsFilter('id'$productId));
  148.         return $this->productRepository->search($criteria$context)->first();
  149.     }
  150.     /**
  151.      * @param $productNumber
  152.      * @param Context $context
  153.      * @return ProductEntity|null
  154.      */
  155.     public function findProductByNumber(
  156.         $productNumber,
  157.         Context $context
  158.     ) {
  159.         $criteria = new Criteria();
  160.         $criteria->addFilter(new EqualsFilter('productNumber'$productNumber));
  161.         return $this->productRepository->search($criteria$context)->first();
  162.     }
  163.     /**
  164.      * add EPREL data
  165.      *
  166.      * @param EntityCollection $result
  167.      * @param SalesChannelContext $salesChannelContext
  168.      * @return EntityCollection
  169.      * @throws \GuzzleHttp\Exception\GuzzleException
  170.      */
  171.     public function prepareEprel(EntityCollection $resultSalesChannelContext $salesChannelContext): EntityCollection
  172.     {
  173.         /** @var \Gbmed\Enev\Core\Content\Enev\EnevEntity $element */
  174.         foreach ($result->getElements() as $element) {
  175.             $this->setElementEprelData($element);
  176.             if (!$element->getEprelGroup() || !$element->getEprelId()) {
  177.                 continue;
  178.             }
  179.             try {
  180.                 if ($eprelData $this->serviceEprel->getEprelDataFromElement($element)) {
  181.                     $this->setElementEprelData(
  182.                         $element,
  183.                         $this->serviceEprel->getGbmedEnev($eprelData$salesChannelContext)
  184.                     );
  185.                 }
  186.             } catch (\Exception $e) {
  187.             }
  188.         }
  189.         return $result;
  190.     }
  191.     /**
  192.      * @param EnevEntity $element
  193.      * @param array|null $data
  194.      */
  195.     private function setElementEprelData(EnevEntity $element, ?array $data null)
  196.     {
  197.         if (is_callable([$element'__set'])) {
  198.             $element->__set('eprelData'$data);
  199.         } else {
  200.             $element->eprelData $data;
  201.         }
  202.     }
  203.     /**
  204.      * @return EntityRepositoryInterface
  205.      */
  206.     public function getGbmedEnevRepository(): EntityRepositoryInterface
  207.     {
  208.         return $this->gbmedEnevRepository;
  209.     }
  210.     /**
  211.      * @return EntityRepositoryInterface
  212.      */
  213.     public function getProductRepository(): EntityRepositoryInterface
  214.     {
  215.         return $this->productRepository;
  216.     }
  217.     /**
  218.      * @return RequestCriteriaBuilder
  219.      */
  220.     public function getSearchCriteriaBuilder(): RequestCriteriaBuilder
  221.     {
  222.         return $this->searchCriteriaBuilder;
  223.     }
  224. }