PartKeepr\PartBundle\Action\SetStockAction::__invoke PHP Method

__invoke() public method

Retrieves a collection of resources.
public __invoke ( Request $request, integer $id ) : array | Dunglas\ApiBundle\Model\PaginatorInterface | Traversable
$request Symfony\Component\HttpFoundation\Request The request
$id integer The ID of the part
return array | Dunglas\ApiBundle\Model\PaginatorInterface | Traversable
    public function __invoke(Request $request, $id)
    {
        list($resourceType) = $this->extractAttributes($request);
        $part = $this->getItem($this->dataProvider, $resourceType, $id);
        /*
         * @var $part Part
         */
        $quantity = $request->request->get('quantity');
        $user = $this->userService->getUser();
        $oldQuantity = $part->getStockLevel();
        $correctionQuantity = $quantity - $oldQuantity;
        if ($correctionQuantity != 0) {
            $stock = new StockEntry();
            $stock->setStockLevel($correctionQuantity);
            $stock->setUser($user);
            if ($request->request->has('comment') && $request->request->get('comment') !== null) {
                $stock->setComment($request->request->get('comment'));
            }
            $part->addStockLevel($stock);
            $this->registry->getManager()->persist($stock);
            $this->registry->getManager()->flush();
        }
        return $part;
    }