PutUserAction.php (2518B)
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\Exceptions\UserProtectedException; 12 use PartKeepr\AuthBundle\Services\UserService; 13 use Symfony\Component\HttpFoundation\Request; 14 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 15 use Symfony\Component\Serializer\SerializerInterface; 16 17 class PutUserAction 18 { 19 use ActionUtilTrait; 20 21 /** 22 * @var DataProviderInterface 23 */ 24 private $dataProvider; 25 26 /** 27 * @var SerializerInterface 28 */ 29 private $serializer; 30 31 /** 32 * @var UserService 33 */ 34 private $userService; 35 36 public function __construct( 37 DataProviderInterface $dataProvider, 38 SerializerInterface $serializer, 39 UserService $userService 40 ) { 41 $this->dataProvider = $dataProvider; 42 $this->serializer = $serializer; 43 $this->userService = $userService; 44 } 45 46 /** 47 * Create a new item. 48 * 49 * @param Request $request 50 * @param string|int $id 51 * 52 * @throws NotFoundHttpException 53 * @throws RuntimeException 54 * @throws UserProtectedException 55 * @throws UserLimitReachedException 56 * 57 * @return mixed 58 */ 59 public function __invoke(Request $request, $id) 60 { 61 /** 62 * @var ResourceInterface 63 */ 64 list($resourceType, $format) = $this->extractAttributes($request); 65 66 /** 67 * @var User 68 */ 69 $data = $this->getItem($this->dataProvider, $resourceType, $id); 70 71 $context = $resourceType->getDenormalizationContext(); 72 $context['object_to_populate'] = $data; 73 74 if ($data->isProtected()) { 75 throw new UserProtectedException(); 76 } 77 78 $data = $this->serializer->deserialize( 79 $request->getContent(), 80 $resourceType->getEntityClass(), 81 $format, 82 $context 83 ); 84 85 if ($data->isActive()) { 86 if ($this->userService->checkUserLimit()) { 87 throw new UserLimitReachedException(); 88 } 89 } 90 91 $this->userService->syncData($data); 92 $data->setNewPassword(''); 93 $data->setPassword(''); 94 $data->setLegacy(false); 95 96 return $data; 97 } 98 }