src/EventSubscriber/TablePositionSubscriber.php line 31

  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Document\Area\Area;
  5. use App\Document\Area\TablePosition;
  6. use Doctrine\ODM\MongoDB\DocumentManager;
  7. use Doctrine\ODM\MongoDB\MongoDBException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. final class TablePositionSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(private readonly DocumentManager $manager)
  15.     {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             KernelEvents::VIEW => ['updateTablePosition'EventPriorities::PRE_WRITE],
  21.         ];
  22.     }
  23.     /**
  24.      * @throws MongoDBException
  25.      */
  26.     public function updateTablePosition(ViewEvent $event): void
  27.     {
  28.         $tablePosition $event->getControllerResult();
  29.         $method $event->getRequest()->getMethod();
  30.         if (!$tablePosition instanceof TablePosition || !in_array($method, [Request::METHOD_POSTRequest::METHOD_PUT])) {
  31.             return;
  32.         }
  33.         if (Request::METHOD_POST === $method) {
  34.             $area $tablePosition->getArea();
  35.             $this->updateOtherTablePositions($area);
  36.         }
  37.         if (Request::METHOD_PUT === $method) {
  38.             $newPosition $tablePosition->getPosition();
  39.             /** @var int $oldPosition */
  40.             $oldPosition $this->manager->getUnitOfWork()->getOriginalDocumentData($tablePosition)['position'];
  41.             $area $tablePosition->getArea();
  42.             if ($newPosition != $oldPosition) {
  43.                 if ($newPosition $oldPosition) {
  44.                     $this->updateOtherTablePositions($area$oldPosition);
  45.                 } else {
  46.                     $this->updateOtherTablePositions($areanull$oldPosition);
  47.                 }
  48.             }
  49.         }
  50.     }
  51.     /**
  52.      * @throws MongoDBException
  53.      */
  54.     private function updateOtherTablePositions(Area $area, ?int $gt null, ?int $lte null): void
  55.     {
  56.         $query $this->manager->createQueryBuilder(TablePosition::class);
  57.         $query->updateMany()
  58.             ->field('area')->references($area);
  59.         if ($gt) {
  60.             $query->field('position')->gt($gt);
  61.         }
  62.         if ($lte) {
  63.             $query->field('position')->lte($lte);
  64.         }
  65.         if ($gt && $lte) {
  66.             $query->addAnd($query->expr()->field('position')->gt($gt));
  67.             $query->addAnd($query->expr()->field('position')->lte($lte));
  68.         }
  69.         $query->field('position')->inc($gt ? -1);
  70.         $query->getQuery()->execute();
  71.     }
  72. }