src/Manager/BehaviourManager.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use App\Entity\Sale;
  4. use App\Entity\SaleBehaviour;
  5. use App\Entity\User;
  6. use App\Entity\Vehicle;
  7. use App\Entity\VehicleBehaviour;
  8. use App\Event\FilterBehaviourEvent;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. /**
  11.  * Manager users behaviour.
  12.  */
  13. class BehaviourManager
  14. {
  15.     public function __construct(private readonly EntityManagerInterface $em)
  16.     {
  17.     }
  18.     public function handleVehicleBehaviour(FilterBehaviourEvent $event): void
  19.     {
  20.         $user $event->getUser();
  21.         $vehicle $event->getEntity();
  22.         if (!$vehicle instanceof Vehicle || !$user instanceof User) {
  23.             return;
  24.         }
  25.         $vehicleRow $this->em->getRepository(VehicleBehaviour::class)
  26.                 ->findOneBy([
  27.                     'user' => $user,
  28.                     'vehicle' => $vehicle,
  29.                 ]);
  30.         if (null === $vehicleRow) {
  31.             $vehicleRow = new VehicleBehaviour($user$vehicle);
  32.         } else {
  33.             $vehicleRow->setUpdatedAt(new \DateTime());
  34.             $vehicleRow->setValue($vehicleRow->getValue() + 1);
  35.         }
  36.         $this->em->persist($vehicleRow);
  37.         $this->em->flush();
  38.         $this->handleSaleBehaviour($event);
  39.     }
  40.     public function handleSaleBehaviour(FilterBehaviourEvent $event): void
  41.     {
  42.         $user $event->getUser();
  43.         $entity $event->getEntity();
  44.         $sale $entity instanceof Vehicle $entity->getEvent()->getSale() : $entity;
  45.         if (!$sale instanceof Sale || !$user instanceof User) {
  46.             return;
  47.         }
  48.         $saleRow $this->em->getRepository(SaleBehaviour::class)
  49.                 ->findOneBy([
  50.                     'user' => $user,
  51.                     'sale' => $sale,
  52.                 ]);
  53.         if (null === $saleRow) {
  54.             $saleRow = new SaleBehaviour($user$sale);
  55.         } else {
  56.             $saleRow->setUpdatedAt(new \DateTime());
  57.             $saleRow->setValue($saleRow->getValue() + 1);
  58.         }
  59.         $this->em->persist($saleRow);
  60.         $this->em->flush();
  61.     }
  62. }