src/EventSubscriber/CreateUserSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Employee;
  5. use App\Entity\User;
  6. use App\Repository\UserRepository;
  7. use App\Service\UserManipulatorService;
  8. use Doctrine\ORM\OptimisticLockException;
  9. use Doctrine\ORM\ORMException;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. class CreateUserSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var UserRepository
  19.      */
  20.     private $userRepository;
  21.     /**
  22.      * @var UserPasswordEncoderInterface
  23.      */
  24.     private $userPasswordEncoder;
  25.     /**
  26.      * @var UserManipulatorService
  27.      */
  28.     private $generateUserFromEmployeeService;
  29.     public function __construct(UserRepository $userRepositoryUserPasswordEncoderInterface $userPasswordEncoder,
  30.                                 UserManipulatorService $generateUserFromEmployeeService)
  31.     {
  32.         $this->userRepository $userRepository;
  33.         $this->userPasswordEncoder $userPasswordEncoder;
  34.         $this->generateUserFromEmployeeService $generateUserFromEmployeeService;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.           KernelEvents::VIEW => ['createUserFromEmployee'EventPriorities::POST_WRITE]
  40.         ];
  41.     }
  42.     public function createUserFromEmployee(ViewEvent $viewEvent) {
  43.         /** @var Employee $employee */
  44.         $employee $viewEvent->getControllerResult();
  45.         $method $viewEvent->getRequest()->getMethod();
  46.         if (!$employee instanceof Employee || Request::METHOD_POST !== $method) {
  47.             return;
  48.         }
  49.         $this->generateUserFromEmployeeService->generateUserFromEmployee($employee);
  50.     }
  51. }