custom/plugins/SwagPayPal/src/Pos/MessageQueue/Handler/CloneVisibilityHandler.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Pos\MessageQueue\Handler;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityCollection;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Swag\PayPal\Pos\MessageQueue\Handler\Sync\AbstractSyncHandler;
  14. use Swag\PayPal\Pos\MessageQueue\Message\AbstractSyncMessage;
  15. use Swag\PayPal\Pos\MessageQueue\Message\CloneVisibilityMessage;
  16. use Swag\PayPal\Pos\Run\RunService;
  17. class CloneVisibilityHandler extends AbstractSyncHandler
  18. {
  19.     private EntityRepositoryInterface $productVisibilityRepository;
  20.     public function __construct(
  21.         RunService $runService,
  22.         LoggerInterface $logger,
  23.         EntityRepositoryInterface $productVisibilityRepository
  24.     ) {
  25.         parent::__construct($runService$logger);
  26.         $this->productVisibilityRepository $productVisibilityRepository;
  27.     }
  28.     /**
  29.      * @param CloneVisibilityMessage $message
  30.      */
  31.     public function sync(AbstractSyncMessage $message): void
  32.     {
  33.         $context $message->getContext();
  34.         $criteria = new Criteria();
  35.         $criteria->addFilter(new EqualsFilter('salesChannelId'$message->getFromSalesChannelId()));
  36.         $criteria->setLimit($message->getLimit());
  37.         $criteria->setOffset($message->getOffset());
  38.         /** @var ProductVisibilityCollection $existingVisibilities */
  39.         $existingVisibilities $this->productVisibilityRepository->search($criteria$context)->getEntities();
  40.         $updates = [];
  41.         foreach ($existingVisibilities as $existingVisibility) {
  42.             $updates[] = [
  43.                 'productId' => $existingVisibility->getProductId(),
  44.                 'salesChannelId' => $message->getToSalesChannelId(),
  45.                 'visibility' => $existingVisibility->getVisibility(),
  46.             ];
  47.         }
  48.         $this->productVisibilityRepository->upsert($updates$context);
  49.     }
  50.     public static function getHandledMessages(): iterable
  51.     {
  52.         return [
  53.             CloneVisibilityMessage::class,
  54.         ];
  55.     }
  56. }