DeleteUserAction.php (1916B)
1 <?php 2 3 namespace PartKeepr\AuthBundle\Action; 4 5 use Dunglas\ApiBundle\Action\ActionUtilTrait; 6 use Dunglas\ApiBundle\Exception\RuntimeException; 7 use Dunglas\ApiBundle\Model\DataProviderInterface; 8 use PartKeepr\AuthBundle\Entity\User; 9 use PartKeepr\AuthBundle\Exceptions\UserProtectedException; 10 use PartKeepr\AuthBundle\Services\UserPreferenceService; 11 use PartKeepr\AuthBundle\Services\UserService; 12 use Symfony\Component\HttpFoundation\Request; 13 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 14 15 /** 16 * Custom API action deleting an user. 17 */ 18 class DeleteUserAction 19 { 20 use ActionUtilTrait; 21 22 /** 23 * @var DataProviderInterface 24 */ 25 private $dataProvider; 26 27 /** 28 * @var UserService 29 */ 30 private $userService; 31 32 /** 33 * @var UserPreferenceService 34 */ 35 private $userPreferenceService; 36 37 public function __construct( 38 DataProviderInterface $dataProvider, 39 UserService $userService, 40 UserPreferenceService $userPreferenceService 41 ) { 42 $this->dataProvider = $dataProvider; 43 $this->userService = $userService; 44 $this->userPreferenceService = $userPreferenceService; 45 } 46 47 /** 48 * Returns an item to delete. 49 * 50 * @param Request $request 51 * @param string|int $id 52 * 53 * @throws NotFoundHttpException 54 * @throws RuntimeException 55 * @throws UserProtectedException 56 * 57 * @return mixed 58 */ 59 public function __invoke(Request $request, $id) 60 { 61 list($resourceType) = $this->extractAttributes($request); 62 63 /** 64 * @var User 65 */ 66 $item = $this->getItem($this->dataProvider, $resourceType, $id); 67 68 if ($item->isProtected()) { 69 throw new UserProtectedException(); 70 } 71 72 $this->userService->deleteFOSUser($item); 73 $this->userPreferenceService->deletePreferences($item); 74 75 return $item; 76 } 77 }