src/Event/Vehicle/VehicleEventSubscriber.php line 227

Open in your IDE?
  1. <?php
  2. namespace App\Event\Vehicle;
  3. use App\Entity\Customer;
  4. use App\Entity\NotificationSubscriptionUser;
  5. use App\Entity\User;
  6. use App\Entity\Vehicle;
  7. use App\Factory\VehicleNewPhotosNotificationFactory;
  8. use App\Service\UserNotification\UserNotificationService;
  9. use App\Task\UserNotification\AddContainerToVehicleNotification;
  10. use App\Task\UserNotification\DriverPickupNotification;
  11. use App\Task\UserNotification\NewVehicleNotification;
  12. use App\Task\UserNotification\VehicleArrivalNotification;
  13. use App\Task\UserNotification\VehicleContainerTrackingNotification;
  14. use App\Task\UserNotification\VehicleDisbandmentDateNotification;
  15. use App\Task\UserNotification\VehicleInvoiceNotification;
  16. use App\Task\UserNotification\VehicleLandDeliveryStatusNotification;
  17. use App\Task\UserNotification\VehicleNewPhotosNotification;
  18. use App\Task\UserNotification\VehicleSevenDaysEatDateNotification;
  19. use App\Task\UserNotification\VehicleUnpaidNotification;
  20. use DateTimeImmutable;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\Messenger\MessageBusInterface;
  24. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  25. class VehicleEventSubscriber implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var MessageBusInterface
  29.      */
  30.     private $bus;
  31.     /**
  32.      * @var EntityManagerInterface
  33.      */
  34.     private $em;
  35.     /**
  36.      * @var UserNotificationService
  37.      */
  38.     private $notificationService;
  39.     /**
  40.      * @var UrlGeneratorInterface
  41.      */
  42.     private $router;
  43.     public function __construct(MessageBusInterface $busEntityManagerInterface $emUserNotificationService $notificationServiceUrlGeneratorInterface $router)
  44.     {
  45.         $this->bus $bus;
  46.         $this->em $em;
  47.         $this->notificationService $notificationService;
  48.         $this->router $router;
  49.     }
  50.     public static function getSubscribedEvents(){
  51.         return [
  52.             NewVehicleEvent::NAME => ['onVehicleAdded'10],
  53.             VehicleNewPhotosEvent::NAME => ['onPhotosAdded'10],
  54.             VehicleChildClientSetted::NAME => ['onVehicleChildClientSetted'10],
  55.             VehicleArrivalEvent::NAME => ['onVehicleArrived'10],
  56.             VehicleUnpaidEvent::NAME => ['onVehicleUnpaid'10],
  57.             VehicleDockReceiptAvailableEvent::NAME => ['onVehicleDockReceiptAvailable'10],
  58.             VehicleInvoiceAvailableEvent::NAME => ['onVehicleInvoiceAvailable'10],
  59.             VehicleLandDeliveryStatusEvent::NAME => ['onVehicleLandDeliveryStatusChange'10],
  60.             VehicleDriverPickupEvent::NAME => ['onDriverPickupNotification'10],
  61.             VehicleDisbandmentDateEvent::NAME => ['onVehicleDisbandmentDateNotification'10],
  62. //            VehicleSevenDaysEatDateEvent::NAME => ['onVehicleSevenDaysEatDate', 10],
  63. //            AddContainerToVehicleEvent::NAME => ['onAddContainerToVehicle', 10],
  64.         ];
  65.     }
  66.     private function getOwnersPersonalManagerSubscriptions(Vehicle $vehicle) : array {
  67.         $owner $vehicle->getOwner();
  68.         if (!$owner instanceof User){
  69.             return [];
  70.         }
  71.         $managers $this->em->getRepository(User::class)->findPersonalManagersOf($owner);
  72.         $managerIds array_map(function($manager) {
  73.             return $manager->getId();
  74.         }, $managers);
  75.         return $this->em->getRepository(NotificationSubscriptionUser::class)->findSubscriptionsForUserIds($managerIds);
  76.     }
  77.     private function getVehicleOwnerSubscriptions(Vehicle $vehicle) : array {
  78.         $owner $vehicle->getOwner();
  79.         if (!$owner instanceof User){
  80.             return [];
  81.         }
  82.         return $this->em->getRepository(NotificationSubscriptionUser::class)->findSubscriptionsForUser($owner);
  83.     }
  84.     private function getVehicleChildClientSubscriptions(Vehicle $vehicle) : array {
  85.         $customer $vehicle->getChildClient();
  86.         if (!$customer instanceof Customer){
  87.             return [];
  88.         }
  89.         return $this->em->getRepository(NotificationSubscriptionUser::class)->findSubscriptionsForUser($customer);
  90.     }
  91.     private function getVehicleTransferDataFormUrl(Vehicle $vehicle) : string {
  92.         if (!$vehicle instanceof Vehicle){
  93.             return '';
  94.         }
  95.         $locale 'ru';
  96.         $owner $vehicle->getOwner();
  97.         if ($owner instanceof User){
  98.             $locale $owner->getLocale();
  99.         }
  100.         $this->router->getContext()
  101.             ->setHost('my.zvigerauto.com')
  102.             ->setScheme('https');
  103.         return $this->router->generate('transfer_edit_data', ['id' => $vehicle->getId(), 'slug' => 'need_to_fill''_locale' => $locale], UrlGeneratorInterface::ABSOLUTE_URL) ?? '';
  104.     }
  105.     public function onVehicleAdded(NewVehicleEvent $event){
  106.         $vehicle $event->getVehicle();
  107.         $user $vehicle->getOwner();
  108.         if (!$user){
  109.             return;
  110.         }
  111.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $user){
  112.             $this->notificationService->dispatchNotification(new NewVehicleNotification($user$vehicle), $user);
  113.         }
  114.     }
  115.     public function onVehicleChildClientSetted(VehicleChildClientSetted $event){
  116.         $vehicle $event->getVehicle();
  117.         foreach ($this->getVehicleChildClientSubscriptions($vehicle) as $user){
  118.                 $this->notificationService->dispatchNotification(new NewVehicleNotification($user$vehicle), $user);
  119.         }
  120.     }
  121.     public function onPhotosAdded(VehicleNewPhotosEvent $event){
  122.         $vehicle $event->getVehicle();
  123.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
  124.             try {
  125.                 $this->notificationService->dispatchNotification(VehicleNewPhotosNotificationFactory::build($owner_subscription$vehicle$event->getPhotosType()), $owner_subscription);
  126.             } catch (\Exception $e) {
  127.             }
  128.         }
  129.         foreach ($this->getVehicleChildClientSubscriptions($vehicle) as $customer_subscription){
  130.             try {
  131.                 $this->notificationService->dispatchNotification(VehicleNewPhotosNotificationFactory::build($customer_subscription$vehicle$event->getPhotosType()), $customer_subscription);
  132.             } catch (\Exception $e) {
  133.             }
  134.         }
  135.         foreach ($this->getOwnersPersonalManagerSubscriptions($vehicle) as $pm_subscription){
  136.             try {
  137.                 $this->notificationService->dispatchNotification(VehicleNewPhotosNotificationFactory::build($pm_subscription$vehicle$event->getPhotosType()), $pm_subscription);
  138.             } catch (\Exception $e) {
  139.             }
  140.         }
  141.     }
  142.     public function onVehicleArrived(VehicleArrivalEvent $event){
  143.         $vehicle $event->getVehicle();
  144.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
  145.             $this->notificationService->dispatchNotification(new VehicleArrivalNotification($owner_subscription$vehicle), $owner_subscription);
  146.         }
  147.         foreach ($this->getVehicleChildClientSubscriptions($vehicle) as $customer_subscription){
  148.             $this->notificationService->dispatchNotification(new VehicleArrivalNotification($customer_subscription$vehicle), $customer_subscription);
  149.         }
  150.     }
  151.     public function onVehicleUnpaid(VehicleUnpaidEvent $event){
  152.         $vehicle $event->getVehicle();
  153.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
  154.             $this->notificationService->dispatchNotification(new VehicleUnpaidNotification($owner_subscription$vehicle), $owner_subscription);
  155.         }
  156.     }
  157.     public function onVehicleDockReceiptAvailable(VehicleDockReceiptAvailableEvent $event){
  158.         $vehicle $event->getVehicle();
  159.         if (!$vehicle->getContainer()){
  160.             return;
  161.         }
  162.         if (!$vehicle->getDockReceipt()){
  163.             return;
  164.         }
  165.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
  166.             $this->notificationService->dispatchNotification(new VehicleContainerTrackingNotification($owner_subscription$vehicle), $owner_subscription);
  167.         }
  168.     }
  169.     public function onVehicleInvoiceAvailable(VehicleInvoiceAvailableEvent $event){
  170.         $vehicle $event->getVehicle();
  171. //        if (!$vehicle->getInvoiceCode()){
  172. //            return;
  173. //        }
  174.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
  175.             $this->notificationService->dispatchNotification(new VehicleInvoiceNotification($owner_subscription$vehicle), $owner_subscription);
  176.         }
  177.     }
  178.     public function onVehicleSevenDaysEatDate(VehicleSevenDaysEatDateEvent $event){
  179.         return;
  180.         /*$vehicle = $event->getVehicle();
  181.         $url = $this->getVehicleTransferDataFormUrl($vehicle);
  182.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
  183.             $this->notificationService->dispatchNotification(new VehicleSevenDaysEatDateNotification($owner_subscription, $vehicle, $url), $owner_subscription);
  184.         }*/
  185.     }
  186.     public function onAddContainerToVehicle(AddContainerToVehicleEvent $event){
  187.         return;
  188.         /*$vehicle = $event->getVehicle();
  189.         $url = $this->getVehicleTransferDataFormUrl($vehicle);
  190.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
  191.             $this->notificationService->dispatchNotification(new AddContainerToVehicleNotification($owner_subscription, $vehicle, $url), $owner_subscription);
  192.         }*/
  193.     }
  194.     public function onVehicleLandDeliveryStatusChange(VehicleLandDeliveryStatusEvent $event){
  195.         $vehicle $event->getVehicle();
  196.         $user $vehicle->getOwner();
  197.         if (!$user){
  198.             return;
  199.         }
  200.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $user){
  201.             $this->notificationService->dispatchNotification(new VehicleLandDeliveryStatusNotification($user$vehicle), $user);
  202.         }
  203.     }
  204.     public function onVehicleDisbandmentDateNotification(VehicleDisbandmentDateEvent $event)
  205.     {
  206.         $vehicle $event->getVehicle();
  207.         $user $vehicle->getOwner();
  208.         if (!$user){
  209.             return;
  210.         }
  211.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $user){
  212.             $this->notificationService->dispatchNotification(new VehicleDisbandmentDateNotification($user$vehicle), $user);
  213.         }
  214.     }
  215.     public function onDriverPickupNotification(VehicleDriverPickupEvent $event)
  216.     {
  217.         $vehicle $event->getVehicle();
  218.         $owner $vehicle->getOwner();
  219.         if (!$owner) {
  220.             return;
  221.         }
  222.         /** @var NotificationSubscriptionUser $subscription */
  223.         foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $subscription) {
  224.             $onlyForeignNotification in_array('only_foreign_auction_notification'$subscription->getEnabledNotifications());
  225.             if ($onlyForeignNotification) {
  226.                 $auctionAccount $vehicle->getAuctionType();
  227.                 if (stripos($auctionAccount'чужой') === false) {
  228.                     continue;
  229.                 }
  230.             }
  231.             $this->notificationService->dispatchNotification(
  232.                 new DriverPickupNotification($subscription$vehicle'immediate'),
  233.                 $subscription
  234.             );
  235.         }
  236.     }
  237. }