src/Service/HotelHelper.php line 164

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Complex;
  4. use App\Entity\Hotel;
  5. use App\Entity\Order;
  6. use App\Entity\Page;
  7. use App\Entity\Reporting;
  8. use App\Entity\RoomReserve;
  9. use App\Entity\User;
  10. use App\Security\User as SecurityUser;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Util\ClassUtils;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\Security\Core\Security;
  16. class HotelHelper
  17. {
  18.     public const SESSION_CURRENT_HOTEL_KEY 'currentHotel';
  19.     private Security $security;
  20.     private RequestStack $requestStack;
  21.     private EntityManagerInterface $em;
  22.     private ?Hotel $hotel;
  23.     private array $hotels;
  24.     private array $reportings;
  25.     public function __construct(
  26.         Security $security,
  27.         RequestStack $requestStack,
  28.         EntityManagerInterface $em
  29.     ) {
  30.         $this->security $security;
  31.         $this->requestStack $requestStack;
  32.         $this->em $em;
  33.         $this->hotel null;
  34.         $this->hotels = [];
  35.         $this->reportings = [];
  36.     }
  37.     public function getEntityManager(): EntityManagerInterface
  38.     {
  39.         return $this->em;
  40.     }
  41.     public function getHotels(?User $user): array
  42.     {
  43.         if (!$user) {
  44.             return [];
  45.         }
  46.         if (empty($this->hotels)) {
  47.             if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  48.                 $this->hotels $this->em->getRepository(Hotel::class)->findAll();
  49.             } else {
  50.                 $this->hotels $this->getUserHotels($user);
  51.             }
  52.         }
  53.         return $this->hotels;
  54.     }
  55.     public function getUserHotels(?User $user): array
  56.     {
  57.         if (!$user) {
  58.             return [];
  59.         }
  60.         $hotels = [];
  61.         foreach ($user->getAssignedHotels() as $assignedHotel) {
  62.             $hotelId is_object($assignedHotel->getId()) ? $assignedHotel->getId()->__toString() : $assignedHotel->getId();
  63.             $hotels[$hotelId] = $assignedHotel;
  64.         }
  65.         foreach ($user->getHotels() as $hotel) {
  66.             $hotelId is_object($hotel->getId()) ? $hotel->getId()->__toString() : $hotel->getId();
  67.             if (array_key_exists($hotelId$hotels)) {
  68.                 continue;
  69.             }
  70.             $hotels[] = $hotel;
  71.         }
  72.         usort($hotels, static fn(Hotel $aHotel $b) => strnatcasecmp($a->getName(), $b->getName()));
  73.         return array_values($hotels);
  74.     }
  75.     public function getReportings(?User $user): array
  76.     {
  77.         if (!$user) {
  78.             return [];
  79.         }
  80.         if (empty($this->reportings)) {
  81.             if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  82.                 $this->reportings $this->em->getRepository(Reporting::class)->findAllByCurrentHotel();
  83.             } else {
  84.                 $this->reportings $this->getUserReportings($user);
  85.             }
  86.         }
  87.         return $this->reportings;
  88.     }
  89.     public function getUserReportings(?User $user): array
  90.     {
  91.         if (!$user) {
  92.             return [];
  93.         }
  94.         $reportings = [];
  95.         foreach ($this->em->getRepository(Reporting::class)->findAllByCurrentHotel() as $reporting) {
  96.             if ($this->security->isGranted('ACL_VIEW'$reporting)) {
  97.                 $reportings[] = $reporting;
  98.             }
  99.         }
  100.         return array_values($reportings);
  101.     }
  102.     public function userCanAccessToComplex(?User $user, ?Complex $complex): bool
  103.     {
  104.         if (!$user || !$complex) {
  105.             return false;
  106.         }
  107.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  108.             return true;
  109.         }
  110.         $complexHotels $complex->getHotels()->toArray();
  111.         $userHotels $this->getUserHotels($user);
  112.         return !empty(array_intersect($complexHotels$userHotels));
  113.     }
  114.     public function userCanAccessToHotel(?User $user, ?Hotel $hotel): bool
  115.     {
  116.         if (!$user || !$hotel) {
  117.             return false;
  118.         }
  119.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  120.             return true;
  121.         }
  122.         return !empty(
  123.             array_filter($this->getUserHotels($user), static function (Hotel $hotelItem) use ($hotel) {
  124.                 return $hotelItem->getId() === $hotel->getId();
  125.             })
  126.         );
  127.     }
  128.     public function setCurrentHotel(Hotel $hotel): void
  129.     {
  130.         $this->hotel $hotel;
  131.         $this->requestStack->getSession()->set(self::SESSION_CURRENT_HOTEL_KEY$hotel);
  132.     }
  133.     public function getCurrentHotel($userComplex $complex null): ?Hotel
  134.     {
  135.         if ($this->hotel) {
  136.             return $this->hotel;
  137.         }
  138.         $sessionHotel $this->requestStack->getSession()->get(self::SESSION_CURRENT_HOTEL_KEY);
  139.         if ($sessionHotel) {
  140.             $this->hotel $this->em->getRepository(Hotel::class)->find($sessionHotel->getId());
  141.             return $this->hotel;
  142.         }
  143.         // Get the guests Hotel from the RoomReserve ID in \App\Security\User instead of \App\Entity\User
  144.         if ($user instanceof SecurityUser) {
  145.             $roomReserve $this->em->getRepository(RoomReserve::class)->findOneBy(['id' => $user->getRoomReserveId()]); // Don't inject RoomReserveRepository because of circular reference
  146.             if ($roomReserve && $roomReserve->getRoom()) {
  147.                 return $roomReserve->getRoom()->getHotel();
  148.             }
  149.             return null;
  150.         }
  151.         if (!$user) {
  152.             $user $this->security->getUser();
  153.         }
  154.         $hotels $this->getHotels($user);
  155.         if (empty($hotels)) {
  156.             return null;
  157.         }
  158.         if (!is_null($complex)) {
  159.             $complexHotels $complex->getHotels()->toArray();
  160.             $commonHotels array_intersect($hotels$complexHotels);
  161.             if (count($commonHotels) === 0) {
  162.                 return null;
  163.             }
  164.             $hotels $commonHotels;
  165.         }
  166.         $this->hotel array_pop($hotels);
  167.         return $this->hotel;
  168.     }
  169.     public function rootPages(?SecurityUser $user): ArrayCollection
  170.     {
  171.         if (empty($user)) {
  172.             return new ArrayCollection();
  173.         }
  174.         $currentHotel $this->getCurrentHotel($user);
  175.         if (!$currentHotel) {
  176.             return new ArrayCollection();
  177.         }
  178.         /** @var RoomReserve $roomReserve */
  179.         $roomReserve $this->em->getRepository(RoomReserve::class)->findOneBy(['id' => $user->getRoomReserveId()]);
  180.         if (!$roomReserve) {
  181.             return new ArrayCollection();
  182.         }
  183.         $rootPages $currentHotel->getPages()->filter(function(Page $page) use ($roomReserve) {
  184.             return !$page->isDeleted() && $page->getParent() === null && ($page->getTourOperators()->isEmpty() || $page->getTourOperators()->contains($roomReserve->getTourOperator()));
  185.         })->toArray();
  186.         return new ArrayCollection($rootPages);
  187.     }
  188.     public function menuPages(?SecurityUser $user): ArrayCollection
  189.     {
  190.         if (empty($user)) {
  191.             return new ArrayCollection();
  192.         }
  193.         $currentHotel $this->getCurrentHotel($user);
  194.         if (!$currentHotel) {
  195.             return new ArrayCollection();
  196.         }
  197.         /** @var RoomReserve $roomReserve */
  198.         $roomReserve $this->em->getRepository(RoomReserve::class)->findOneBy(['id' => $user->getRoomReserveId()]);
  199.         if (!$roomReserve) {
  200.             return new ArrayCollection();
  201.         }
  202.         $menuPages $currentHotel->getPages()->filter(function(Page $page) use ($roomReserve) {
  203.             return !$page->isDeleted() && $page->getShowMenu() === true && ($page->getTourOperators()->isEmpty() || $page->getTourOperators()->contains($roomReserve->getTourOperator()));
  204.         })->toArray();
  205.         return new ArrayCollection($menuPages);
  206.     }
  207.     public function getOrder(): ?Order
  208.     {
  209.         $orderId $this->requestStack->getSession()->get('orderId');
  210.         if (!empty($orderId)) {
  211.             $order $this->em->getRepository(Order::class)->find($orderId); // Don't inject OrderRepository because of circular reference
  212.             if (!empty($order)) {
  213.                 return $order;
  214.             }
  215.         }
  216.         return null;
  217.     }
  218.     public function getOrderItemsAmount(): int
  219.     {
  220.         $order $this->getOrder();
  221.         $totalAmount 0;
  222.         if ($order && !$order->getOrderItems()->isEmpty()) {
  223.             foreach ($order->getOrderItems() as $orderItem) {
  224.                 $totalAmount += $orderItem->getAmount();
  225.             }
  226.         }
  227.         return $totalAmount;
  228.     }
  229.     public function getUserById($id): ?User
  230.     {
  231.         $user $this->em->getRepository(User::class)->find($id);
  232.         $userClass ClassUtils::getRealClass(get_class($user));
  233.         return $userClass === User::class? $user null;
  234.     }
  235. }