vendor/store.shopware.com/swagb2bplatform/components/OrderNumber/BridgePlatform/SalesChannelProductRepositoryDecorator.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\OrderNumber\BridgePlatform;
  3. use Shopware\B2B\Shop\BridgePlatform\SalesChannelProductExtension;
  4. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AntiJoinFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\Filter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  16. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use function end;
  19. use function explode;
  20. use function get_class;
  21. use function in_array;
  22. class SalesChannelProductRepositoryDecorator implements SalesChannelRepositoryInterface
  23. {
  24.     private const ORDER_NUMBER_CONTEXT_OWNER_ID_FIELD 'contextOwnerId';
  25.     private const ORDER_NUMBER_CUSTOM_ORDER_NUMBER_FIELD 'customOrderNumber';
  26.     private const PRODUCT_DEFINITION_PRODUCT_NUMBER_FIELD 'productNumber';
  27.     private const PRODUCT_ORDER_NUMBER_ID_FIELD 'product.b2bOrderNumber.id';
  28.     private const PRODUCT_ORDER_NUMBER_CUSTOM_ORDER_NUMBER_FIELD 'product.b2bOrderNumber.customOrderNumber';
  29.     private const PRODUCT_ORDER_NUMBER_CONTEXT_OWNER_ID_FIELD 'product.b2bOrderNumber.contextOwnerId';
  30.     private AuthenticationService $authenticationService;
  31.     private SalesChannelRepositoryInterface $decorated;
  32.     public function __construct(
  33.         AuthenticationService $authenticationService,
  34.         SalesChannelRepositoryInterface $decorated
  35.     ) {
  36.         $this->authenticationService $authenticationService;
  37.         $this->decorated $decorated;
  38.     }
  39.     public function search(Criteria $criteriaSalesChannelContext $salesChannelContext): EntitySearchResult
  40.     {
  41.         if (!$this->authenticationService->isB2b()) {
  42.             return $this->decorated->search($criteria$salesChannelContext);
  43.         }
  44.         $this->prepareCriteriaForB2bOrderNumber($criteria);
  45.         return $this->decorated->search($criteria$salesChannelContext);
  46.     }
  47.     public function aggregate(Criteria $criteriaSalesChannelContext $salesChannelContext): AggregationResultCollection
  48.     {
  49.         return $this->decorated->aggregate($criteria$salesChannelContext);
  50.     }
  51.     public function searchIds(Criteria $criteriaSalesChannelContext $salesChannelContext): IdSearchResult
  52.     {
  53.         if (!$this->authenticationService->isB2b()) {
  54.             return $this->decorated->searchIds($criteria$salesChannelContext);
  55.         }
  56.         $this->prepareCriteriaForB2bOrderNumber($criteria);
  57.         return $this->decorated->searchIds($criteria$salesChannelContext);
  58.     }
  59.     /**
  60.      * @internal
  61.      */
  62.     protected function prepareCriteriaForB2bOrderNumber(Criteria $criteria): void
  63.     {
  64.         $filters $criteria->getFilters();
  65.         $criteria->addAssociation(SalesChannelProductExtension::B2B_ORDER_NUMBER_EXTENSION_NAME);
  66.         $associationCriteria $criteria->getAssociation(SalesChannelProductExtension::B2B_ORDER_NUMBER_EXTENSION_NAME);
  67.         $associationCriteria->addFilter(new EqualsFilter(
  68.             self::ORDER_NUMBER_CONTEXT_OWNER_ID_FIELD,
  69.             $this->authenticationService->getIdentity()->getContextAuthId()->getValue()
  70.         ));
  71.         $criteria->resetFilters();
  72.         foreach ($filters as $filter) {
  73.             $criteria->addFilter($this->mapFilter($filter));
  74.         }
  75.     }
  76.     /**
  77.      * @internal
  78.      */
  79.     protected function mapFilter(Filter $filter): Filter
  80.     {
  81.         // ignore inherited MultiFilter
  82.         if (in_array(
  83.             $class get_class($filter),
  84.             [MultiFilter::class, NotFilter::class, AntiJoinFilter::class],
  85.             true
  86.         )) {
  87.             $queries = [];
  88.             foreach ($filter->getQueries() as $query) {
  89.                 $queries[] = $this->mapFilter($query);
  90.             }
  91.             return new $class($filter->getOperator(), $queries);
  92.         }
  93.         if (!$this->isOrderNumberFilter($filter)) {
  94.             return $filter;
  95.         }
  96.         return $this->buildOrderNumberFilter($filter);
  97.     }
  98.     /**
  99.      * @internal
  100.      */
  101.     protected function isOrderNumberFilter(Filter $filter): bool
  102.     {
  103.         return ($filter instanceof EqualsFilter || $filter instanceof EqualsAnyFilter || $filter instanceof ContainsFilter)
  104.             && $this->getNecessaryFieldValue($filter->getField()) === self::PRODUCT_DEFINITION_PRODUCT_NUMBER_FIELD;
  105.     }
  106.     /**
  107.      * @internal
  108.      */
  109.     protected function buildOrderNumberFilter(Filter $filter): Filter
  110.     {
  111.         return new MultiFilter(
  112.             MultiFilter::CONNECTION_OR,
  113.             [
  114.                 $filter,
  115.                 new MultiFilter(MultiFilter::CONNECTION_AND, [
  116.                     $this->mapOrderNumberFilter(self::PRODUCT_ORDER_NUMBER_CUSTOM_ORDER_NUMBER_FIELD$filter),
  117.                     new NotFilter(NotFilter::CONNECTION_AND, [new EqualsFilter(self::PRODUCT_ORDER_NUMBER_ID_FIELDnull)]),
  118.                     new EqualsFilter(self::PRODUCT_ORDER_NUMBER_CONTEXT_OWNER_ID_FIELD$this->authenticationService->getIdentity()->getContextAuthId()->getValue()),
  119.                 ]),
  120.             ]
  121.         );
  122.     }
  123.     /**
  124.      * @internal
  125.      */
  126.     protected function mapOrderNumberFilter(string $fieldNameFilter $filter): Filter
  127.     {
  128.         if ($filter instanceof EqualsFilter) {
  129.             return new EqualsFilter($fieldName$filter->getValue());
  130.         }
  131.         if ($filter instanceof EqualsAnyFilter) {
  132.             return new EqualsAnyFilter($fieldName$filter->getValue());
  133.         }
  134.         if ($filter instanceof ContainsFilter) {
  135.             return new ContainsFilter($fieldName$filter->getValue());
  136.         }
  137.         return $filter;
  138.     }
  139.     /**
  140.      * @internal
  141.      */
  142.     protected function getNecessaryFieldValue(string $field): string
  143.     {
  144.         $explodedField explode('.'$field);
  145.         return end($explodedField);
  146.     }
  147. }