src/App/EventListener/Client/Store/StoreMoveListener.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\Client\Store;
  3. use App\Entity\Client\Store\StoreHeader;
  4. use App\Entity\Client\Store\StoreMove;
  5. use App\EventListener\GenericEvent;
  6. use App\Service\AppManager;
  7. use App\Service\Client\Store\StoreHeaderManager;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class StoreMoveListener
  11. {
  12.     private $appManager;
  13.     private $storeHeaderManager;
  14.     public function __construct(StoreHeaderManager $storeHeaderManagerAppManager $appManager)
  15.     {
  16.         $this->appManager $appManager;
  17.         $this->storeHeaderManager $storeHeaderManager;
  18.     }
  19.     public function addMove(GenericEvent $genericEvent)
  20.     {
  21.         $storeMove $genericEvent->getSubject();
  22.         if (!$storeMove instanceof StoreMove) {
  23.             return;
  24.         }
  25.         $storeHeader $storeMove->getHeader();
  26.         if (!$storeHeader instanceof StoreHeader) {
  27.             return;
  28.         }
  29.         if (!$storeHeader->isTemporary()){
  30.             $genericEvent->setResponse(new JsonResponse(json_encode(['message' => 'Header is locked''errorCode' => '1001']), 403, [], true));
  31.             $genericEvent->stopPropagation();
  32.             return;
  33.         }
  34.         $allowedTypes = [StoreHeader::STORE_HEADER_WAREHOUSE_RECEIPTStoreHeader::STORE_HEADER_WAREHOUSE_RECEIPT_RETURNStoreHeader::STORE_HEADER_STORE_TRANSFER];
  35.         if (!in_array($storeHeader->getType(), $allowedTypes)) {
  36.             return;
  37.         }
  38.         $haveProductInMoves $storeHeader->getMoves()->filter(function (StoreMove $existStoreMove) use ($storeMove) {
  39.             return $existStoreMove->getProduct()->getId() === $storeMove->getProduct()->getId() && $existStoreMove->getId() !== $storeMove->getId();
  40.         });
  41.         foreach ($haveProductInMoves as $haveProductInMove) {
  42.             $storeHeader->removeMove($haveProductInMove);
  43.         }
  44.         $this->appManager->eventPersist($storeHeaderAppManager::UPDATE'move_add');
  45.         $genericEvent->setResponse(new Response($storeMove->getId(), 201));
  46.         $genericEvent->stopPropagation();
  47.     }
  48.     public function removeMove(GenericEvent $genericEvent)
  49.     {
  50.         $storeMove $genericEvent->getSubject();
  51.         if (!$storeMove instanceof StoreMove) {
  52.             return;
  53.         }
  54.         $storeHeader $storeMove->getHeader();
  55.         if (!$storeHeader instanceof StoreHeader) {
  56.             return;
  57.         }
  58.         $this->isLocked($storeHeader$genericEvent);
  59.         if ($genericEvent->isPropagationStopped()){
  60.             return;
  61.         }
  62.         if(!StoreHeaderManager::isWarehouseType($storeHeader->getType())){
  63.             return;
  64.         }
  65.         $haveProductInMoves $storeHeader->getMoves()->filter(function (StoreMove $existStoreMove) use ($storeMove) {
  66.             return $existStoreMove->getProduct()->getId() === $storeMove->getProduct()->getId() && $existStoreMove->getId() !== $storeMove->getId();
  67.         });
  68.         foreach ($haveProductInMoves as $haveProductInMove) {
  69.             $storeHeader->removeMove($haveProductInMove);
  70.         }
  71.         $this->appManager->eventPersist($storeHeaderAppManager::UPDATE'move_delete');
  72.         $genericEvent->setResponse(new Response(null204));
  73.         $genericEvent->stopPropagation();
  74.     }
  75.     private function isLocked(StoreHeader $storeHeaderGenericEvent $genericEvent)
  76.     {
  77.         if (!$storeHeader->isTemporary()){
  78.             $genericEvent->setResponse(new JsonResponse(json_encode(['message' => 'Header is locked''errorCode' => '1001']), 403, [], true));
  79.             $genericEvent->stopPropagation();
  80.         }
  81.     }
  82. }