src/EventSubscriber/Onboarding/LeadPostValidateSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SEBASTIAN TOVAR
  5.  * Date: 21/06/2019
  6.  * Time: 12:34 PM
  7.  */
  8. namespace App\EventSubscriber\Onboarding;
  9. use ApiPlatform\Core\EventListener\EventPriorities;
  10. use App\Entity\App\User;
  11. use App\Entity\Onboarding\Lead;
  12. use App\Exception\SubmittedOnboardingException;
  13. use App\Repository\App\UserRepository;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpKernel\Event\ViewEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use App\Exception\InvalidArgumentException;
  20. use App\Services\UtilsService;
  21. class LeadPostValidateSubscriber implements EventSubscriberInterface
  22. {
  23.     private $translator;
  24.     private $request;
  25.     private $utilsService;
  26.     private $userRepository;
  27.     public function __construct(
  28.         TranslatorInterface $translator,
  29.         UtilsService $utilsService,
  30.         UserRepository $userRepository
  31.     ){
  32.         $this->translator $translator;
  33.         $this->utilsService $utilsService;
  34.         $this->userRepository $userRepository;
  35.     }
  36.     public function onKernelView(ViewEvent $event)
  37.     {
  38.         if ($this->utilsService->isAPublicRequest($event)) {
  39.             return;
  40.         }
  41.         $lead $event->getControllerResult();
  42.         $this->request $event->getRequest();
  43.         $method $this->request->getMethod();
  44.         if (!($lead instanceof Lead) || (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)) {
  45.             return;
  46.         }
  47.         if ($lead->getIsCompleted()) {
  48.             throw new SubmittedOnboardingException(
  49.                 $this->translator->trans('Onboarding already submitted')
  50.             );
  51.         }
  52.         $version $lead->getVersion();
  53.         switch ($version) {
  54.             case 1:
  55.                 $this->validateFieldsV1($lead);
  56.                 break;
  57.             case 2:
  58.                 $this->validateFieldsV2($lead);
  59.                 break;
  60.             default:
  61.                 throw new InvalidArgumentException(
  62.                     $this->translator->trans('Onboarding wrong version')
  63.                 );
  64.                 break;
  65.         }
  66.         return;
  67.     }
  68.     /**
  69.      * @param Lead $lead
  70.      * @throws InvalidArgumentException
  71.      */
  72.     protected function validateFieldsV2(Lead $lead)
  73.     {
  74.         $step $lead->getStep();
  75.         switch ($step) {
  76.             case Lead::V2_USER_STEP:
  77.                 $this->stepOneValidationV2($lead);
  78.                 break;
  79.             case Lead::V2_COMPANY_STEP:
  80.                 break;
  81.             case Lead::V2_TASK_STEP:
  82.                 break;
  83.             default:
  84.                 throw new InvalidArgumentException(
  85.                     $this->translator->trans('Onboarding wrong step',
  86.                         ['%step%' => $lead->getStep()]
  87.                     )
  88.                 );
  89.                 break;
  90.         }
  91.     }
  92.     protected function stepOneValidationV2(Lead $lead) {
  93.         //$content = $this->request->getContent();
  94.         //$params = json_decode($content, true);
  95.         $email $lead->getEmail() ?? null;
  96.         $mobileNumber $lead->getMobileNumber() ?? null;
  97.         $qiipUserEmail null;
  98.         $qiipUserMobile null;
  99.         if (is_null($email) && is_null($mobileNumber)) {
  100.             throw new InvalidArgumentException(
  101.                 $this->translator->trans('Onboarding field null',
  102.                     ['%fieldName%' => 'Email / Mobile number']
  103.                 )
  104.             );
  105.         }
  106.         if (!is_null($mobileNumber)) {
  107.             $mobileArray explode('~',$mobileNumber);
  108.             if (count($mobileArray) != 3) {
  109.                 throw new InvalidArgumentException(
  110.                     $this->translator->trans('Onboarding mobile number error',
  111.                         ['%mobileNumber%' => $lead->getMobileNumber()]
  112.                     )
  113.                 );
  114.             }
  115.             $criteria $mobileArray[1] . $mobileArray[2];
  116.             $qiipUserMobile $this->userRepository->loadUserByUsername($criteria);
  117.             if ($qiipUserMobile instanceof  User) {
  118.                 //TODO Enviar SMS
  119.                 throw new InvalidArgumentException(
  120.                     $this->translator->trans('Onboarding existing user')
  121.                 );
  122.             }
  123.         }
  124.         if (!is_null($email)) {
  125.             $criteria $email;
  126.             $qiipUserEmail $this->userRepository->loadUserByUsername($criteria);
  127.             if ($qiipUserEmail instanceof  User) {
  128.                 //TODO Enviar mail
  129.                 throw new InvalidArgumentException(
  130.                     $this->translator->trans('Onboarding existing user')
  131.                 );
  132.             }
  133.         }
  134.     }
  135.     protected function stepTwoValidationV2(Lead $lead) {
  136.         if (is_null($lead->getVendor() ?? null)) {
  137.             throw new InvalidArgumentException(
  138.                 $this->translator->trans('Onboarding field null',
  139.                     ['%fieldName%' => 'Company']
  140.                 )
  141.             );
  142.         }
  143.     }
  144.     /**
  145.      * @param Lead $lead
  146.      * @throws InvalidArgumentException
  147.      */
  148.     protected function validateFieldsV1(Lead $lead)
  149.     {
  150.         $step $lead->getStep();
  151.         switch ($step) {
  152.             case Lead::GENERAL_INFO_STEP:
  153.                 $this->stepOneValidation($lead);
  154.                 break;
  155.             case Lead::USERS_STEP:
  156.                 $this->validateEmailsAndMobileNumbers($lead->getUsers(), "User contact info");
  157.                 break;
  158.             case Lead::OPERATORS_STEP:
  159.                 $this->validateEmailsAndMobileNumbers($lead->getOperators(), "Operator contact info");
  160.                 break;
  161.             case Lead::TASKS_STEP:
  162.                 break;
  163.             default:
  164.                 throw new InvalidArgumentException(
  165.                     $this->translator->trans('Onboarding wrong step',
  166.                         ['%step%' => $lead->getStep()]
  167.                     )
  168.                 );
  169.                 break;
  170.         }
  171.     }
  172.     protected function stepOneValidationV1(Lead $lead) {
  173.         $content $this->request->getContent();
  174.         $params json_decode($contenttrue);
  175.         $email $params['email'] ?? null;
  176.         $mobileNumber $params['mobileNumber'] ?? null;
  177.         if (is_null($email) && is_null($mobileNumber)) {
  178.             throw new InvalidArgumentException(
  179.                 $this->translator->trans('Onboarding field null',
  180.                     ['%fieldName%' => 'Email / Mobile number']
  181.                 )
  182.             );
  183.         }
  184.         if (!is_null($mobileNumber)) {
  185.             $lead->setEmail(null);
  186.             if (!preg_match('/^\+[\d -]+$/'$lead->getMobileNumber())) {
  187.                 throw new InvalidArgumentException(
  188.                     $this->translator->trans('Onboarding mobile number error',
  189.                         ['%mobileNumber%' => $lead->getMobileNumber()]
  190.                     )
  191.                 );
  192.             }
  193.             $criteria $mobileNumber;
  194.         }
  195.         else {
  196.             $lead->setMobileNumber(null);
  197.             $criteria $email;
  198.         }
  199.         $qiipUser $this->userRepository->loadUserByUsername($criteria);
  200.         if (!is_null($qiipUser)) {
  201.             throw new InvalidArgumentException(
  202.                 $this->translator->trans('Onboarding existing user')
  203.             );
  204.         }
  205.     }
  206.     protected function validateEmailsAndMobileNumbers($contactsField$fieldName) {
  207.         if (!is_null($contactsField) && !empty($contactsField)) {
  208.             $contactArray explode(",",preg_replace('/\s+/','',$contactsField));
  209.             $validData = array();
  210.             $noValidData = array();
  211.             foreach ($contactArray as $key => $contact) {
  212.                 if(!empty($contact)) {
  213.                     if (filter_var($contactFILTER_VALIDATE_EMAIL)) {
  214.                         $validData[] = $contact;
  215.                     }
  216.                     else {
  217.                         if (preg_match('/^\+[\d -]+$/'$contact)) {
  218.                             $validData[] = $contact;
  219.                         }
  220.                         else {
  221.                             $noValidData[] = $contact;
  222.                         }
  223.                     }
  224.                 }
  225.             }
  226.             if (!empty($noValidData)) {
  227.                 $wrongLeads implode(", "$noValidData);
  228.                 throw new InvalidArgumentException(
  229.                     $this->translator->trans('Onboarding number/email error',
  230.                         ['%wrongLeads%' => $wrongLeads]
  231.                     )
  232.                 );
  233.             }
  234.         }
  235.     }
  236.     public static function getSubscribedEvents()
  237.     {
  238.         return [
  239.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_VALIDATE]
  240.         ];
  241.     }
  242. }