src/EventSubscriber/User/UserPostWriteSubscriber.php line 79

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 28/03/2019
  6.  * Time: 06:10 PM
  7.  */
  8. namespace App\EventSubscriber\User;
  9. use App\Exception\GeneralException;
  10. use App\Exception\NotFoundException;
  11. use App\MessageHandler\Message;
  12. use App\Repository\App\TimeSlot\TimeSlotRepository;
  13. use Doctrine\ORM\EntityManager;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\ViewEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use ApiPlatform\Core\EventListener\EventPriorities;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use App\Repository\App\VendorRepository;
  21. use App\Repository\App\CompanyRepository;
  22. use App\Services\VendorService;
  23. use App\Services\UserService;
  24. use App\Entity\App\User;
  25. use App\Entity\App\Vendor;
  26. use App\Entity\App\Role;
  27. use Symfony\Component\Messenger\MessageBusInterface;
  28. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  29. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  30. use Symfony\Contracts\Translation\TranslatorInterface;
  31. class UserPostWriteSubscriber implements EventSubscriberInterface
  32. {
  33.     private $vendorRepository;
  34.     private $userService;
  35.     private $vendorService;
  36.     private $entityManager;
  37.     private $companyRepository;
  38.     private $tokenStorage;
  39.     private $translator;
  40.     private $authorizationChecker;
  41.     private $messageBus;
  42.     private $timeSlotRepository;
  43.     public function __construct(
  44.         EntityManagerInterface $entityManager,
  45.         CompanyRepository $companyRepository,
  46.         VendorRepository $vendorRepository,
  47.         VendorService $vendorService,
  48.         UserService $userService,
  49.         TokenStorageInterface $tokenStorage,
  50.         TranslatorInterface $translator,
  51.         AuthorizationCheckerInterface $checker,
  52.         MessageBusInterface $messageBus,
  53.     TimeSlotRepository  $timeSlotRepository
  54.     ) {
  55.         $this->entityManager $entityManager;
  56.         $this->vendorRepository $vendorRepository;
  57.         $this->userService $userService;
  58.         $this->vendorService $vendorService;
  59.         $this->companyRepository $companyRepository;
  60.         $this->tokenStorage $tokenStorage;
  61.         $this->translator $translator;
  62.         $this->authorizationChecker $checker;
  63.         $this->messageBus $messageBus;
  64.         $this->timeSlotRepository $timeSlotRepository;
  65.     }
  66.     /**
  67.      * @param ViewEvent $event
  68.      * @throws GeneralException
  69.      * @throws NotFoundException
  70.      * @throws \Doctrine\Common\Persistence\Mapping\MappingException
  71.      * @throws \Doctrine\DBAL\ConnectionException
  72.      * @throws \Doctrine\ORM\ORMException
  73.      */
  74.     public function onKernelView(ViewEvent $event)
  75.     {
  76.         $user $event->getControllerResult();
  77.         $request $event->getRequest();
  78.         $route $request->attributes->get('_route');
  79.         if (!($user instanceof User)) {
  80.             return;
  81.         }
  82.         if ($this->tokenStorage->getToken()) {
  83.             $userCurrent $this->tokenStorage->getToken()->getUser();
  84.             if (!($userCurrent instanceof User)) {
  85.                 throw new NotFoundException($this->translator->trans('User current not found'));
  86.             }
  87.             if (!$this->entityManager->isOpen()) {
  88.                 $this->entityManager EntityManager::create(
  89.                     $this->entityManager->getConnection(),
  90.                     $this->entityManager->getConfiguration()
  91.                 );
  92.             }
  93.             if (false == $this->entityManager->getConnection()->ping()) {
  94.                 $this->entityManager->getConnection()->close();
  95.                 $this->entityManager->getConnection()->connect();
  96.             }
  97.             if ('api_users_post_collection' == $route || 'api_users_put_item' == $route) {
  98.                 $dataMessage = [];
  99.                 $this->entityManager->getConnection()->beginTransaction();
  100.                 try {
  101.                     $plainPassword $user->getPlainPassword();
  102.                     if (!is_null($plainPassword)) {
  103.                         $this->userService->encodePassword($userfalsefalse);
  104.                     }
  105.                     $mediaContent $user->getMediaContent();
  106.                     if ($mediaContent !== null && $mediaContent !== 0) {
  107.                         $this->userService->addMediaContent(
  108.                             $user,
  109.                             $mediaContent,
  110.                             false,
  111.                             true,
  112.                             false);
  113.                     }
  114.                     if (is_null($user->getChannelName())) {
  115.                         $user->setChannelName(md5(uniqid()));
  116.                     }
  117.                     if (!is_null($user->getEmail())) {
  118.                         $user->setUsername($user->getEmail());
  119.                     } elseif (!is_null($user->getPhonePrefixAndPhone())) {
  120.                         $user->setUsername($user->getPhonePrefixAndPhone());
  121.                     } elseif (!is_null($user->getPhoneNumber())) {
  122.                         $user->setUsername($user->getPhoneNumber());
  123.                     }
  124.                     $this->entityManager->persist($user);
  125.                     $data json_decode($request->getContent(), true);
  126.                     $vendor null;
  127.                     if (isset($data['vendor'])) {
  128.                         $vendor $this->vendorRepository->find($data['vendor']);
  129.                     }
  130.                     if ($vendor instanceof Vendor) {
  131.                         if ('api_users_put_item' == $route) {
  132.                             if ($this->vendorService->isUserRoleInToVendor(
  133.                                 $vendor,
  134.                                 $userCurrent,
  135.                                 [Role::ROLE_ADMIN]
  136.                             )) {
  137.                                 $this->updatePassword($user);
  138.                             } else {
  139.                                 if ($userCurrent === $user || $this->authorizationChecker->isGranted(
  140.                                         'ROLE_SUPERADMIN'
  141.                                     )) {
  142.                                     $this->updatePassword($user);
  143.                                 }
  144.                             }
  145.                         }
  146.                         $vendorStaff $this->vendorService->findOrCreateVendorStaff($vendor$usertrue);
  147.                         if (isset($data['company']) && !is_null($data['company'])) {
  148.                             $company $this->companyRepository->find($data['company']);
  149.                             $vendorStaff->setCompany($company);
  150.                         } else {
  151.                             $vendorStaff->setCompany(null);
  152.                         }
  153.                         if (isset($data['costPerHour'])) {
  154.                             $vendorStaff->setCostPerHour($data['costPerHour']);
  155.                         } else {
  156.                             $vendorStaff->setCostPerHour(0);
  157.                         }
  158.                         if (isset($data['enable'])) {
  159.                             $vendorStaff->setEnable($data['enable']);
  160.                         }
  161.                         if (isset($data['timeSlot']['id']) && !is_null($vendorStaff)){
  162.                             $timeSlotRepository $this->timeSlotRepository->find($data['timeSlot']['id']);
  163.                             if(!is_null($timeSlotRepository)) {
  164.                                 $vendorStaff->setCostPerHour($timeSlotRepository->getDefaultCost());
  165.                             }
  166.                         }
  167.                         if(!is_null($vendorStaff)){
  168.                             $workerBudgets $vendorStaff->getWorkerBudgets();
  169.                             foreach ($workerBudgets as $workerBudget){
  170.                                 $workerBudget->setCostHour($vendorStaff->getCostPerHour());
  171.                             }
  172.                         }
  173.                         $this->entityManager->persist($vendorStaff);
  174.                         $messageResponse '';
  175.                         if (isset($data['roles'])) {
  176.                             $this->vendorService->addRoleStaff(
  177.                                 $vendorStaff,
  178.                                 $data['roles'],
  179.                                 true,
  180.                                 $messageResponse,
  181.                                 false,
  182.                                 true);
  183.                         }
  184.                         if (isset($data['locations'])) {
  185.                             $this->vendorService->addLocationToUser(
  186.                                 $vendorStaff,
  187.                                 $data['locations'],
  188.                                 true,
  189.                                 $messageResponse,
  190.                                 false,
  191.                                 true);
  192.                         }
  193.                         if (isset($data['categories'])) {
  194.                             $this->vendorService->addCategoryToUser(
  195.                                 $vendorStaff,
  196.                                 $data['categories'],
  197.                                 $messageResponse,
  198.                                 false,
  199.                                 true);
  200.                         }
  201.                         if ('api_users_post_collection' == $route) {
  202.                             $dataMessage['user_id'] = $user->getId();
  203.                             $dataMessage['vendor_id'] = $vendor->getId();
  204.                             if (!empty($user->getEmail())) {
  205.                                 $this->userService->sendEnterpriseWelcome($user);
  206.                             }
  207.                             if (!empty($user->getPhonePrefixAndPhone())) {
  208.                                 $this->userService->sendInvitationSMS($user);
  209.                             }
  210.                         }
  211.                     }
  212.                     $this->entityManager->flush();
  213.                     $this->entityManager->getConnection()->commit();
  214.                     //$this->entityManager->clear();
  215.                 } catch (\Exception $exception) {
  216.                     $this->entityManager->getConnection()->rollback();
  217.                     $this->entityManager->remove($user);
  218.                     //$this->entityManager->clear();
  219.                     throw new GeneralException($exception->getMessage());
  220.                 }
  221.                 if ('api_users_post_collection' == $route) {
  222.                     if (!empty($user->getCaptchaToken())) {
  223.                         if (!empty($user->getEmail())) {
  224.                             $this->userService->sendEnterpriseWelcome($user);
  225.                         }
  226.                         if (!empty($user->getPhonePrefixAndPhone())) {
  227.                             $this->userService->sendInvitationSMS($user);
  228.                         }
  229.                     }
  230.                 }
  231.                 if (count($dataMessage) > 0) {
  232.                     $this->messageBus->dispatch(new Message(Message::NEW_USER$dataMessage));
  233.                 }
  234.             }
  235.         }
  236.     }
  237.     /**
  238.      * @param User $user
  239.      * @throws \Doctrine\ORM\ORMException
  240.      */
  241.     protected function updatePassword(User $user)
  242.     {
  243.         $plainPassword $user->getPlainPassword();
  244.         if (!is_null($plainPassword))
  245.             $this->userService->encodePassword($usertruefalse);
  246.     }
  247.     public static function getSubscribedEvents()
  248.     {
  249.         return [
  250.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_WRITE]
  251.         ];
  252.     }
  253. }