<?php
namespace FMT\Domain\EventSubscriber;
use Exception;
use FMT\Application\Controller\Common\PublicDashboardController;
use FMT\Data\Entity\User;
use FMT\Data\Entity\UserSchool;
use FMT\Domain\Event\BookstoreApiNotificationEvent;
use FMT\Domain\Event\BookstoreEvent;
use FMT\Domain\Repository\UserRepositoryInterface;
use FMT\Domain\Repository\UserSchoolRepositoryInterface;
use FMT\Domain\Service\BookstorePayment\CustomerManager;
use FMT\Domain\Service\ShippingManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class BookstoreSubscriber implements EventSubscriberInterface
{
private UserSchoolRepositoryInterface $userSchoolRepository;
private EventDispatcherInterface $dispatcher;
private UserRepositoryInterface $userRepository;
private ShippingManagerInterface $manager;
private RouterInterface $router;
private CustomerManager $customerManager;
public function __construct(
UserSchoolRepositoryInterface $userSchoolRepository,
EventDispatcherInterface $dispatcher,
UserRepositoryInterface $userRepository,
ShippingManagerInterface $manager,
RouterInterface $router,
CustomerManager $customerManager
) {
$this->userSchoolRepository = $userSchoolRepository;
$this->userRepository = $userRepository;
$this->dispatcher = $dispatcher;
$this->manager = $manager;
$this->router = $router;
$this->customerManager = $customerManager;
}
public static function getSubscribedEvents(): array
{
return [
BookstoreEvent::BOOKSTORE_REQUEST_FAILED => 'onBookstoreRequestFailed'
];
}
public function onBookstoreRequestFailed(BookstoreEvent $event)
{
$userSchool = $this->userSchoolRepository->findOneBy(['bookstoreId' => $event->getBookstoreId()]);
if ($userSchool && $this->isApiConnectionFails($userSchool) && $userSchool->isActive()) {
$this->userRepository->beginTransaction();
$userSchool->setActive(false);
$userSchool->getUserSchoolSettings()->setIsTestSuccessful(false);
$this->userSchoolRepository->add($userSchool);
$this->userSchoolRepository->save($userSchool);
$this->dispatchInternalUsersNotification($userSchool);
$this->dispatchExternalUsersNotification($userSchool);
$this->userRepository->commit();
}
return new RedirectResponse($this->router->generate(PublicDashboardController::ROUTE_INDEX));
}
private function dispatchExternalUsersNotification(UserSchool $userSchool)
{
$involvedUsers = $this->userRepository->getInvolvedUsersForSchool($userSchool);
$event = new BookstoreApiNotificationEvent($involvedUsers, $userSchool->getName());
$this->dispatcher->dispatch($event, BookstoreApiNotificationEvent::USERS_NOTIFICATION);
}
private function dispatchInternalUsersNotification(UserSchool $userSchool)
{
$adminUsers = $this->userRepository->getUsersByRoles([User::ROLE_SUPER_ADMIN]);
$adminEvent = new BookstoreApiNotificationEvent($adminUsers, $userSchool->getName());
$this->dispatcher->dispatch($adminEvent, BookstoreApiNotificationEvent::ADMIN_NOTIFICATION);
}
/**
* @return bool
*/
private function isApiConnectionFails(UserSchool $userSchool): bool
{
try {
$this->manager->getOptions(true, $userSchool->getBookstoreId());
$this->customerManager->getFundingSourceIriAdminTest($userSchool->getUserSchoolSettings());
} catch (Exception $exception) {
return true;
}
return false;
}
}