partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

SetStockAction.php (2393B)


      1 <?php
      2 
      3 namespace PartKeepr\PartBundle\Action;
      4 
      5 use Dunglas\ApiBundle\Action\ActionUtilTrait;
      6 use Dunglas\ApiBundle\Exception\RuntimeException;
      7 use Dunglas\ApiBundle\Model\DataProviderInterface;
      8 use PartKeepr\AuthBundle\Services\UserService;
      9 use PartKeepr\CategoryBundle\Exception\RootNodeNotFoundException;
     10 use PartKeepr\PartBundle\Entity\Part;
     11 use PartKeepr\StockBundle\Entity\StockEntry;
     12 use Symfony\Bridge\Doctrine\ManagerRegistry;
     13 use Symfony\Component\HttpFoundation\Request;
     14 
     15 /**
     16  * Sets the stock for a given part.
     17  */
     18 class SetStockAction
     19 {
     20     use ActionUtilTrait;
     21 
     22     /**
     23      * @var DataProviderInterface
     24      */
     25     private $dataProvider;
     26 
     27     /**
     28      * @var ManagerRegistry
     29      */
     30     private $registry;
     31 
     32     /**
     33      * @var UserService
     34      */
     35     private $userService;
     36 
     37     public function __construct(
     38         DataProviderInterface $dataProvider,
     39         UserService $userService,
     40         ManagerRegistry $registry
     41     ) {
     42         $this->dataProvider = $dataProvider;
     43         $this->userService = $userService;
     44         $this->registry = $registry;
     45     }
     46 
     47     /**
     48      * Retrieves a collection of resources.
     49      *
     50      * @param Request $request The request
     51      * @param int     $id      The ID of the part
     52      *
     53      * @throws RuntimeException|RootNodeNotFoundException
     54      *
     55      * @return array|\Dunglas\ApiBundle\Model\PaginatorInterface|\Traversable
     56      */
     57     public function __invoke(Request $request, $id)
     58     {
     59         list($resourceType) = $this->extractAttributes($request);
     60 
     61         $part = $this->getItem($this->dataProvider, $resourceType, $id);
     62 
     63         /*
     64          * @var $part Part
     65          */
     66         $quantity = $request->request->get('quantity');
     67         $user = $this->userService->getUser();
     68 
     69         $oldQuantity = $part->getStockLevel();
     70         $correctionQuantity = $quantity - $oldQuantity;
     71 
     72         if ($correctionQuantity != 0) {
     73             $stock = new StockEntry();
     74             $stock->setStockLevel($correctionQuantity);
     75             $stock->setUser($user);
     76 
     77             if ($request->request->has('comment') && $request->request->get('comment') !== null) {
     78                 $stock->setComment($request->request->get('comment'));
     79             }
     80 
     81             $part->addStockLevel($stock);
     82             $this->registry->getManager()->persist($stock);
     83             $this->registry->getManager()->flush();
     84         }
     85 
     86         return $part;
     87     }
     88 }