PostUserAction.php (2161B)
1 <?php 2 3 namespace PartKeepr\AuthBundle\Action; 4 5 use Dunglas\ApiBundle\Action\ActionUtilTrait; 6 use Dunglas\ApiBundle\Api\ResourceInterface; 7 use Dunglas\ApiBundle\Exception\RuntimeException; 8 use Dunglas\ApiBundle\Model\DataProviderInterface; 9 use PartKeepr\AuthBundle\Entity\User; 10 use PartKeepr\AuthBundle\Exceptions\UserLimitReachedException; 11 use PartKeepr\AuthBundle\Services\UserService; 12 use Symfony\Component\HttpFoundation\Request; 13 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 14 use Symfony\Component\Serializer\SerializerInterface; 15 16 class PostUserAction 17 { 18 use ActionUtilTrait; 19 20 /** 21 * @var DataProviderInterface 22 */ 23 private $dataProvider; 24 25 /** 26 * @var SerializerInterface 27 */ 28 private $serializer; 29 30 /** 31 * @var UserService 32 */ 33 private $userService; 34 35 public function __construct( 36 DataProviderInterface $dataProvider, 37 SerializerInterface $serializer, 38 UserService $userService 39 ) { 40 $this->dataProvider = $dataProvider; 41 $this->serializer = $serializer; 42 $this->userService = $userService; 43 } 44 45 /** 46 * Create a new item. 47 * 48 * @param Request $request 49 * 50 * @throws NotFoundHttpException 51 * @throws RuntimeException 52 * @throws UserLimitReachedException 53 * 54 * @return mixed 55 */ 56 public function __invoke(Request $request) 57 { 58 /** 59 * @var ResourceInterface 60 */ 61 list($resourceType, $format) = $this->extractAttributes($request); 62 63 if ($this->userService->checkUserLimit() === true) { 64 throw new UserLimitReachedException(); 65 } 66 /** 67 * @var User 68 */ 69 $data = $this->serializer->deserialize( 70 $request->getContent(), 71 $resourceType->getEntityClass(), 72 $format, 73 $resourceType->getDenormalizationContext() 74 ); 75 76 $data->setProvider($this->userService->getBuiltinProvider()); 77 $data->setLegacy(false); 78 $this->userService->syncData($data); 79 80 $data->setNewPassword(''); 81 $data->setPassword(''); 82 83 return $data; 84 } 85 }