<?php
namespace App\Event\Vehicle;
use App\Entity\Customer;
use App\Entity\NotificationSubscriptionUser;
use App\Entity\User;
use App\Entity\Vehicle;
use App\Factory\VehicleNewPhotosNotificationFactory;
use App\Service\UserNotification\UserNotificationService;
use App\Task\UserNotification\AddContainerToVehicleNotification;
use App\Task\UserNotification\DriverPickupNotification;
use App\Task\UserNotification\NewVehicleNotification;
use App\Task\UserNotification\VehicleArrivalNotification;
use App\Task\UserNotification\VehicleContainerTrackingNotification;
use App\Task\UserNotification\VehicleInvoiceNotification;
use App\Task\UserNotification\VehicleLandDeliveryStatusNotification;
use App\Task\UserNotification\VehicleNewPhotosNotification;
use App\Task\UserNotification\VehicleSevenDaysEatDateNotification;
use App\Task\UserNotification\VehicleUnpaidNotification;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class VehicleEventSubscriber implements EventSubscriberInterface
{
/**
* @var MessageBusInterface
*/
private $bus;
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var UserNotificationService
*/
private $notificationService;
/**
* @var UrlGeneratorInterface
*/
private $router;
public function __construct(MessageBusInterface $bus, EntityManagerInterface $em, UserNotificationService $notificationService, UrlGeneratorInterface $router)
{
$this->bus = $bus;
$this->em = $em;
$this->notificationService = $notificationService;
$this->router = $router;
}
public static function getSubscribedEvents(){
return [
NewVehicleEvent::NAME => ['onVehicleAdded', 10],
VehicleNewPhotosEvent::NAME => ['onPhotosAdded', 10],
VehicleChildClientSetted::NAME => ['onVehicleChildClientSetted', 10],
VehicleArrivalEvent::NAME => ['onVehicleArrived', 10],
VehicleUnpaidEvent::NAME => ['onVehicleUnpaid', 10],
VehicleDockReceiptAvailableEvent::NAME => ['onVehicleDockReceiptAvailable', 10],
VehicleInvoiceAvailableEvent::NAME => ['onVehicleInvoiceAvailable', 10],
VehicleLandDeliveryStatusEvent::NAME => ['onVehicleLandDeliveryStatusChange', 10],
VehicleDriverPickupEvent::NAME => ['onDriverPickupNotification', 10],
// VehicleSevenDaysEatDateEvent::NAME => ['onVehicleSevenDaysEatDate', 10],
// AddContainerToVehicleEvent::NAME => ['onAddContainerToVehicle', 10],
];
}
private function getOwnersPersonalManagerSubscriptions(Vehicle $vehicle) : array {
$owner = $vehicle->getOwner();
if (!$owner instanceof User){
return [];
}
$managers = $this->em->getRepository(User::class)->findPersonalManagersOf($owner);
$managerIds = array_map(function($manager) {
return $manager->getId();
}, $managers);
return $this->em->getRepository(NotificationSubscriptionUser::class)->findSubscriptionsForUserIds($managerIds);
}
private function getVehicleOwnerSubscriptions(Vehicle $vehicle) : array {
$owner = $vehicle->getOwner();
if (!$owner instanceof User){
return [];
}
return $this->em->getRepository(NotificationSubscriptionUser::class)->findSubscriptionsForUser($owner);
}
private function getVehicleChildClientSubscriptions(Vehicle $vehicle) : array {
$customer = $vehicle->getChildClient();
if (!$customer instanceof Customer){
return [];
}
return $this->em->getRepository(NotificationSubscriptionUser::class)->findSubscriptionsForUser($customer);
}
private function getVehicleTransferDataFormUrl(Vehicle $vehicle) : string {
if (!$vehicle instanceof Vehicle){
return '';
}
$locale = 'ru';
$owner = $vehicle->getOwner();
if ($owner instanceof User){
$locale = $owner->getLocale();
}
$this->router->getContext()
->setHost('my.zvigerauto.com')
->setScheme('https');
return $this->router->generate('transfer_edit_data', ['id' => $vehicle->getId(), 'slug' => 'need_to_fill', '_locale' => $locale], UrlGeneratorInterface::ABSOLUTE_URL) ?? '';
}
public function onVehicleAdded(NewVehicleEvent $event){
$vehicle = $event->getVehicle();
$user = $vehicle->getOwner();
if (!$user){
return;
}
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $user){
$this->notificationService->dispatchNotification(new NewVehicleNotification($user, $vehicle), $user);
}
}
public function onVehicleChildClientSetted(VehicleChildClientSetted $event){
$vehicle = $event->getVehicle();
foreach ($this->getVehicleChildClientSubscriptions($vehicle) as $user){
$this->notificationService->dispatchNotification(new NewVehicleNotification($user, $vehicle), $user);
}
}
public function onPhotosAdded(VehicleNewPhotosEvent $event){
$vehicle = $event->getVehicle();
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
try {
$this->notificationService->dispatchNotification(VehicleNewPhotosNotificationFactory::build($owner_subscription, $vehicle, $event->getPhotosType()), $owner_subscription);
} catch (\Exception $e) {
}
}
foreach ($this->getVehicleChildClientSubscriptions($vehicle) as $customer_subscription){
try {
$this->notificationService->dispatchNotification(VehicleNewPhotosNotificationFactory::build($customer_subscription, $vehicle, $event->getPhotosType()), $customer_subscription);
} catch (\Exception $e) {
}
}
foreach ($this->getOwnersPersonalManagerSubscriptions($vehicle) as $pm_subscription){
try {
$this->notificationService->dispatchNotification(VehicleNewPhotosNotificationFactory::build($pm_subscription, $vehicle, $event->getPhotosType()), $pm_subscription);
} catch (\Exception $e) {
}
}
}
public function onVehicleArrived(VehicleArrivalEvent $event){
$vehicle = $event->getVehicle();
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
$this->notificationService->dispatchNotification(new VehicleArrivalNotification($owner_subscription, $vehicle), $owner_subscription);
}
foreach ($this->getVehicleChildClientSubscriptions($vehicle) as $customer_subscription){
$this->notificationService->dispatchNotification(new VehicleArrivalNotification($customer_subscription, $vehicle), $customer_subscription);
}
}
public function onVehicleUnpaid(VehicleUnpaidEvent $event){
$vehicle = $event->getVehicle();
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
$this->notificationService->dispatchNotification(new VehicleUnpaidNotification($owner_subscription, $vehicle), $owner_subscription);
}
}
public function onVehicleDockReceiptAvailable(VehicleDockReceiptAvailableEvent $event){
$vehicle = $event->getVehicle();
if (!$vehicle->getContainer()){
return;
}
if (!$vehicle->getDockReceipt()){
return;
}
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
$this->notificationService->dispatchNotification(new VehicleContainerTrackingNotification($owner_subscription, $vehicle), $owner_subscription);
}
}
public function onVehicleInvoiceAvailable(VehicleInvoiceAvailableEvent $event){
$vehicle = $event->getVehicle();
// if (!$vehicle->getInvoiceCode()){
// return;
// }
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
$this->notificationService->dispatchNotification(new VehicleInvoiceNotification($owner_subscription, $vehicle), $owner_subscription);
}
}
public function onVehicleSevenDaysEatDate(VehicleSevenDaysEatDateEvent $event){
return;
/*$vehicle = $event->getVehicle();
$url = $this->getVehicleTransferDataFormUrl($vehicle);
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
$this->notificationService->dispatchNotification(new VehicleSevenDaysEatDateNotification($owner_subscription, $vehicle, $url), $owner_subscription);
}*/
}
public function onAddContainerToVehicle(AddContainerToVehicleEvent $event){
return;
/*$vehicle = $event->getVehicle();
$url = $this->getVehicleTransferDataFormUrl($vehicle);
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $owner_subscription){
$this->notificationService->dispatchNotification(new AddContainerToVehicleNotification($owner_subscription, $vehicle, $url), $owner_subscription);
}*/
}
public function onVehicleLandDeliveryStatusChange(VehicleLandDeliveryStatusEvent $event){
$vehicle = $event->getVehicle();
$user = $vehicle->getOwner();
if (!$user){
return;
}
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $user){
$this->notificationService->dispatchNotification(new VehicleLandDeliveryStatusNotification($user, $vehicle), $user);
}
}
public function onDriverPickupNotification(VehicleDriverPickupEvent $event)
{
$vehicle = $event->getVehicle();
$owner = $vehicle->getOwner();
if (!$owner) {
return;
}
/** @var NotificationSubscriptionUser $subscription */
foreach ($this->getVehicleOwnerSubscriptions($vehicle) as $subscription) {
$onlyForeignNotification = in_array('only_foreign_auction_notification', $subscription->getEnabledNotifications());
if ($onlyForeignNotification) {
$auctionAccount = $vehicle->getAuctionType();
if (stripos($auctionAccount, 'чужой') === false) {
continue;
}
}
$this->notificationService->dispatchNotification(
new DriverPickupNotification($subscription, $vehicle, 'immediate'),
$subscription
);
}
}
}