src/EventListener/ClinicSubscriber.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Aws\SESManager;
  4. use App\Entity\Clinic\Clinic;
  5. use App\Entity\Person\Admin;
  6. use App\Entity\Person\SuperClinicAdmin;
  7. use App\Entity\Person\User;
  8. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Cookie;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  15. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  16. use Symfony\Component\HttpKernel\Event\ViewEvent;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  19. class ClinicSubscriber implements EventSubscriberInterface
  20. {
  21.     const EMAIL_MESSAGE 'Please verify your clinic email address. An automated email from Amazon Web Services (SES) has been sent to that address. Clicking on the link within that email will verify your clinic email address.';
  22.     const ALLOWED_ROUTES = [
  23.         'clinics_delete',
  24.         'clinics_onboarding_index',
  25.         'patients_import',
  26.         'clinics_product_import',
  27.         'clinic_users_import',
  28.         'clinics_treatment_category_import',
  29.         'clinics_price_list_import',
  30.         'patients_note_import',
  31.         'invoices_import',
  32.         'clinic_register_price_tiers',
  33.         'clinic_register_profile',
  34.         'clinic_register_customer',
  35.         'clinics_customer_new',
  36.         'clinic_register_team',
  37.         'clinic_register_products',
  38.         'clinic_register_treatments',
  39.         'clinic_register_payment',
  40.         'admin_clinics_price_tiers',
  41.         'clinic_register_form_files',
  42.         'clinics_onboarding_finish'
  43.     ];
  44.     /**
  45.      * @var SESManager
  46.      */
  47.     private $ses;
  48.     /**
  49.      * @var Router
  50.      */
  51.     private $router;
  52.     /**
  53.      * @var Session
  54.      */
  55.     private $session;
  56.     /**
  57.      * @var TokenStorageInterface
  58.      */
  59.     private $tokenStorage;
  60.     public function __construct(SESManager $sesRouter $routerSession $sessionTokenStorageInterface $tokenStorage)
  61.     {
  62.         $this->ses $ses;
  63.         $this->router $router;
  64.         $this->session $session;
  65.         $this->tokenStorage $tokenStorage;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public static function getSubscribedEvents()
  71.     {
  72.         return [
  73.             'kernel.controller_arguments' => 'onKernelController',
  74.             'kernel.view' => 'onKernelView',
  75.             'kernel.response' => 'onKernelResponse'
  76.         ];
  77.     }
  78.     public function onKernelView(ViewEvent $event)
  79.     {
  80.         $request $event->getRequest();
  81.         /** @var Clinic $clinic */
  82.         $clinic $request->attributes->get('clinic');
  83.         if ($clinic) {
  84.             $controllerName $request->attributes->get('_controller');
  85.             $routeName $request->attributes->get('_route');
  86.  
  87.              
  88.             $trialEndDate$clinic->getTrialEndDate();
  89.             
  90.             $token $this->tokenStorage->getToken();
  91.             $user $token instanceof TokenInterface $token->getUser() : null;
  92.             
  93.             $isAdmin false;
  94.             if ($user instanceof Admin) {
  95.               $isAdmin true;
  96.             }
  97.             
  98.             if(count($clinic->getLicenses()) == 0) {
  99.               if ($trialEndDate && $trialEndDate <= date('Ymd') && $request->attributes->get('_route')) {
  100.                   
  101.                   if(in_array($request->attributes->get('_route'), ['clinics_customer_new''clinics_license_new''clinic_register_price_tiers'])) {
  102.                     ;
  103.                   } else if(!$isAdmin && 'clinic_register_customer' !== $request->attributes->get('_route')){
  104.                      
  105.                     $needLicense false;
  106.                     if($clinic->getOrganisation()) {
  107.                       $org $clinic->getOrganisation();
  108.                       if($org->isEnableLicensing()) {
  109.                         $needLicense true;
  110.                       }
  111.                     }
  112.                     
  113.                     if($needLicense) {
  114.                       $link $this->router->generate('clinic_register_customer', ['id' => $clinic->getId()]);
  115.                       $event->setResponse(new RedirectResponse($link));
  116.                       return;
  117.                     }
  118.                   }
  119.               }
  120.             }
  121.             
  122.             if (false === strpos($controllerName'App\Controller\Admin') || ($routeName && in_array(
  123.                         $routeName,
  124.                         self::ALLOWED_ROUTES,
  125.                         true
  126.                     ))) {
  127.                 return;
  128.             }
  129.             if (!$isAdmin && count($messages $this->checkClinic($clinic))) {
  130.                 if ($request->attributes->get('_route') && 'clinics_edit' !== $request->attributes->get('_route')) {
  131.                     $token $this->tokenStorage->getToken();
  132.                     $user $token instanceof TokenInterface $token->getUser() : null;
  133.                     if ($user instanceof Admin || $user instanceof SuperClinicAdmin || User::ROLE_CLINIC_ADMIN === $user->getRole()) {
  134.                         $link $this->router->generate('clinics_edit', ['id' => $clinic->getId()]);
  135.                     } else {
  136.                         $link $this->router->generate('dashboard_index');
  137.                     }
  138.                     $event->setResponse(new RedirectResponse($link));
  139.                 }
  140.             }
  141.         }
  142.     }
  143.     private function checkClinic(Clinic $clinic)
  144.     {
  145.         $messages = [];
  146.         if (!$clinic->isVerifiedEmail()) {
  147.             $this->ses->verifyEmail($clinic);
  148.             if (!$clinic->isVerifiedEmail()) {
  149.                 $messages[] = self::EMAIL_MESSAGE;
  150.             }
  151.         }
  152.         return $messages;
  153.     }
  154.     public function onKernelController(ControllerArgumentsEvent $event)
  155.     {
  156.         $request $event->getRequest();
  157.         /** @var Clinic $clinic */
  158.         $clinic $request->attributes->get('clinic');
  159.         if ($clinic) {
  160.             $controllerName $request->attributes->get('_controller');
  161.             $routeName $request->attributes->get('_route');
  162.             if (!$routeName) {
  163.                 return;
  164.             }
  165.             if (false === strpos($controllerName'App\Controller\Admin') || in_array(
  166.                     $routeName,
  167.                     self::ALLOWED_ROUTES,
  168.                     true
  169.                 )) {
  170.                 return;
  171.             }
  172.             if (count($messages $this->checkClinic($clinic))) {
  173.                 $this->session->getFlashBag()->set('warning'$messages);
  174.             }
  175.         }
  176.     }
  177.     public function onKernelResponse(ResponseEvent $event)
  178.     {
  179.         if (!($event->getResponse() instanceof JsonResponse)) {
  180.             $token $this->tokenStorage->getToken();
  181.             $response $event->getResponse();
  182.             $request $event->getRequest();
  183.             $controllerName $request->attributes->get('_controller');
  184.             if ($token) {
  185.                 $user $token->getUser();
  186.                 if ($user && is_object($user)) {
  187.                     if ('App\Controller\Admin\HelpController::index' === $controllerName) {
  188.                         $cookie = new Cookie(
  189.                             'wpl',
  190.                             md5((new \DateTime())->format('d-m-Y')),
  191.                             (new \DateTime())->setTime(235959),
  192.                             '/',
  193.                             '.consentz.com',
  194.                             false,
  195.                             true,
  196.                             false
  197.                         );
  198.                         $response->headers->setCookie($cookie);
  199.                     }
  200.                 } else {
  201.                     $cookie = new Cookie(
  202.                         'wpl',
  203.                         md5((new \DateTime())->format('d-m-Y')),
  204.                         (new \DateTime())->setTime(000),
  205.                         '/',
  206.                         '.consentz.com',
  207.                         false,
  208.                         true,
  209.                         false
  210.                     );
  211.                     $response->headers->setCookie($cookie);
  212.                 }
  213.             }
  214.         }
  215.     }
  216. }