custom/plugins/PickwareErpStarter/src/Warehouse/Model/Subscriber/WarehouseEntitySubscriber.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\PickwareErpStarter\Warehouse\Model\Subscriber;
  11. use Pickware\PickwareErpStarter\Config\Config;
  12. use Pickware\PickwareErpStarter\Warehouse\Model\WarehouseDefinition;
  13. use Pickware\PickwareErpStarter\Warehouse\Model\WarehouseEntity;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class WarehouseEntitySubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var Config
  20.      */
  21.     private $config;
  22.     public function __construct(Config $config)
  23.     {
  24.         $this->config $config;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             WarehouseDefinition::EVENT_LOADED => 'onWarehouseLoaded',
  30.         ];
  31.     }
  32.     public function onWarehouseLoaded(EntityLoadedEvent $event): void
  33.     {
  34.         /** @var WarehouseEntity $warehouse */
  35.         foreach ($event->getEntities() as $warehouse) {
  36.             if ($this->config->getDefaultWarehouseId() === $warehouse->getId()) {
  37.                 $warehouse->assign([
  38.                     'isDefault' => true,
  39.                 ]);
  40.             } else {
  41.                 $warehouse->assign([
  42.                     'isDefault' => false,
  43.                 ]);
  44.             }
  45.         }
  46.     }
  47. }