PartPostAction.php (1963B)
1 <?php 2 3 namespace PartKeepr\PartBundle\Action; 4 5 use Dunglas\ApiBundle\Action\ActionUtilTrait; 6 use Dunglas\ApiBundle\Api\ResourceInterface; 7 use Dunglas\ApiBundle\Exception\RuntimeException; 8 use PartKeepr\PartBundle\Entity\Part; 9 use PartKeepr\PartBundle\Exceptions\InternalPartNumberNotUniqueException; 10 use PartKeepr\PartBundle\Exceptions\PartLimitExceededException; 11 use PartKeepr\PartBundle\Services\PartService; 12 use Symfony\Component\HttpFoundation\Request; 13 use Symfony\Component\Serializer\SerializerInterface; 14 15 class PartPostAction 16 { 17 use ActionUtilTrait; 18 19 /** 20 * @var SerializerInterface 21 */ 22 private $serializer; 23 24 /** 25 * @var PartService 26 */ 27 private $partService; 28 29 public function __construct( 30 SerializerInterface $serializer, 31 PartService $partService 32 ) { 33 $this->serializer = $serializer; 34 $this->partService = $partService; 35 } 36 37 /** 38 * Injects the specific root node ID if "@local-tree-root" was specified. 39 * 40 * @param Request $request 41 * 42 * @throws RuntimeException 43 * @throws PartLimitExceededException 44 * @throws InternalPartNumberNotUniqueException 45 * 46 * @return mixed 47 */ 48 public function __invoke(Request $request) 49 { 50 if ($this->partService->checkPartLimit()) { 51 throw new PartLimitExceededException(); 52 } 53 54 /** 55 * @var ResourceInterface 56 */ 57 list($resourceType, $format) = $this->extractAttributes($request); 58 59 /** 60 * @var Part 61 */ 62 $part = $this->serializer->deserialize( 63 $request->getContent(), 64 $resourceType->getEntityClass(), 65 $format, 66 $resourceType->getDenormalizationContext() 67 ); 68 69 if (!$this->partService->isInternalPartNumberUnique($part->getInternalPartNumber())) { 70 throw new InternalPartNumberNotUniqueException(); 71 } 72 73 return $part; 74 } 75 }