src/Event/Vehicle/VehicleEventSubscriber.php line 167

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