src/FMT/Domain/EventSubscriber/BookstoreSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. namespace FMT\Domain\EventSubscriber;
  3. use Exception;
  4. use FMT\Application\Controller\Common\PublicDashboardController;
  5. use FMT\Data\Entity\User;
  6. use FMT\Data\Entity\UserSchool;
  7. use FMT\Domain\Event\BookstoreApiNotificationEvent;
  8. use FMT\Domain\Event\BookstoreEvent;
  9. use FMT\Domain\Repository\UserRepositoryInterface;
  10. use FMT\Domain\Repository\UserSchoolRepositoryInterface;
  11. use FMT\Domain\Service\BookstorePayment\CustomerManager;
  12. use FMT\Domain\Service\ShippingManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\Routing\RouterInterface;
  17. class BookstoreSubscriber implements EventSubscriberInterface
  18. {
  19.     private UserSchoolRepositoryInterface $userSchoolRepository;
  20.     private EventDispatcherInterface $dispatcher;
  21.     private UserRepositoryInterface $userRepository;
  22.     private ShippingManagerInterface $manager;
  23.     private RouterInterface $router;
  24.     private CustomerManager $customerManager;
  25.     public function __construct(
  26.         UserSchoolRepositoryInterface $userSchoolRepository,
  27.         EventDispatcherInterface $dispatcher,
  28.         UserRepositoryInterface $userRepository,
  29.         ShippingManagerInterface $manager,
  30.         RouterInterface $router,
  31.         CustomerManager $customerManager
  32.     ) {
  33.         $this->userSchoolRepository $userSchoolRepository;
  34.         $this->userRepository $userRepository;
  35.         $this->dispatcher $dispatcher;
  36.         $this->manager $manager;
  37.         $this->router $router;
  38.         $this->customerManager $customerManager;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             BookstoreEvent::BOOKSTORE_REQUEST_FAILED => 'onBookstoreRequestFailed'
  44.         ];
  45.     }
  46.     public function onBookstoreRequestFailed(BookstoreEvent $event)
  47.     {
  48.         $userSchool $this->userSchoolRepository->findOneBy(['bookstoreId' => $event->getBookstoreId()]);
  49.         if ($userSchool && $this->isApiConnectionFails($userSchool) && $userSchool->isActive()) {
  50.             $this->userRepository->beginTransaction();
  51.             $userSchool->setActive(false);
  52.             $userSchool->getUserSchoolSettings()->setIsTestSuccessful(false);
  53.             $this->userSchoolRepository->add($userSchool);
  54.             $this->userSchoolRepository->save($userSchool);
  55.             $this->dispatchInternalUsersNotification($userSchool);
  56.             $this->dispatchExternalUsersNotification($userSchool);
  57.             $this->userRepository->commit();
  58.         }
  59.         return new RedirectResponse($this->router->generate(PublicDashboardController::ROUTE_INDEX));
  60.     }
  61.     private function dispatchExternalUsersNotification(UserSchool $userSchool)
  62.     {
  63.         $involvedUsers $this->userRepository->getInvolvedUsersForSchool($userSchool);
  64.         $event = new BookstoreApiNotificationEvent($involvedUsers$userSchool->getName());
  65.         $this->dispatcher->dispatch($eventBookstoreApiNotificationEvent::USERS_NOTIFICATION);
  66.     }
  67.     private function dispatchInternalUsersNotification(UserSchool $userSchool)
  68.     {
  69.         $adminUsers $this->userRepository->getUsersByRoles([User::ROLE_SUPER_ADMIN]);
  70.         $adminEvent = new BookstoreApiNotificationEvent($adminUsers$userSchool->getName());
  71.         $this->dispatcher->dispatch($adminEventBookstoreApiNotificationEvent::ADMIN_NOTIFICATION);
  72.     }
  73.     /**
  74.      * @return bool
  75.      */
  76.     private function isApiConnectionFails(UserSchool $userSchool): bool
  77.     {
  78.         try {
  79.             $this->manager->getOptions(true$userSchool->getBookstoreId());
  80.             $this->customerManager->getFundingSourceIriAdminTest($userSchool->getUserSchoolSettings());
  81.         } catch (Exception $exception) {
  82.             return true;
  83.         }
  84.          return false;
  85.     }
  86. }