src/Twig/PaidPlacementsExtension.php line 86

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-25
  5.  * Time: 13:45
  6.  */
  7. namespace App\Twig;
  8. use App\Entity\Profile\Genders;
  9. use App\Entity\Profile\Profile;
  10. use App\Entity\Sales\SidebarPlacement;
  11. use App\Entity\Sales\SidebarPlacementType;
  12. use App\Entity\Saloon\Saloon;
  13. use App\Event\Profile\ProfilesShownEvent;
  14. use App\Repository\SidebarPlacementRepository;
  15. use App\Service\CurrentCityResolver;
  16. use App\Service\Features;
  17. use App\Service\ProfileTopBoard;
  18. use App\Service\Sidebar;
  19. use Porpaginas\Doctrine\ORM\ORMQueryResult;
  20. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Twig\Extension\AbstractExtension;
  23. use Twig\TwigFunction;
  24. use Twig\TwigTest;
  25. class PaidPlacementsExtension extends AbstractExtension
  26. {
  27.     public function __construct(
  28.         private RequestStack $requestStack,
  29.         private SidebarPlacementRepository $sidebarPlacementRepository,
  30.         private Features $features,
  31.         private Sidebar $sidebar,
  32.         private EventDispatcherInterface $eventDispatcher,
  33.         private ProfileTopBoard $profileTopBoard,
  34.         private CurrentCityResolver $currentCityResolver
  35.     ) {}
  36.     public function getFunctions()
  37.     {
  38.         return [
  39.             new TwigFunction('profile_top_placement', [$this'currentTopPlacement']),
  40.             new TwigFunction('profile_ultra_vip_placements', [$this'currentUltraVipPlacements']),
  41.             new TwigFunction('profile_vip_placements', [$this'currentVipPlacements']),
  42.             new TwigFunction('placement_name_ultra_vip', [$this'getPlacementNameUltraVip']),
  43.             new TwigFunction('placement_name_vip', [$this'getPlacementNameVip']),
  44.             new TwigFunction('placement_name_standard', [$this'getPlacementNameStandard']),
  45.         ];
  46.     }
  47.     public function getTests(): iterable
  48.     {
  49.         yield new TwigTest('top_placement', [$this'isTopPlacement']);
  50.     }
  51.     public function isTopPlacement($subject): bool
  52.     {
  53.         return ($subject instanceof Profile || $subject instanceof Saloon) && $subject->isTopCard();
  54.     }
  55.     /**
  56.      * @deprecated
  57.      */
  58.     public function currentTopPlacement(): ?Profile
  59.     {
  60.         return $this->profileTopBoard->currentTopPlacement(true);
  61.     }
  62.     public function currentUltraVipPlacements(): array
  63.     {
  64.         $filters $this->getSidebarPlacementsFilters();
  65.         $city $this->currentCityResolver->resolveCurrentCity();
  66.         return $this->sidebar->getPlacementsToShow(
  67.             $citySidebarPlacementType::ULTRA_VIP1, [],
  68.             $filters['genders'], $filters['only_profile'], $filters['only_masseur'], $filters['only_saloon']
  69.         );
  70.     }
  71.     public function currentVipPlacements(): array
  72.     {
  73.         $filters $this->getSidebarPlacementsFilters();
  74.         $city $this->currentCityResolver->resolveCurrentCity();
  75.         return $this->sidebar->getPlacementsToShow(
  76.             $citySidebarPlacementType::VIP4, [],
  77.             $filters['genders'], $filters['only_profile'], $filters['only_masseur'], $filters['only_saloon']
  78.         );
  79.     }
  80.     private function getSidebarPlacementsFilters(): array
  81.     {
  82.         $request $this->requestStack->getCurrentRequest();
  83.         $params = [
  84.             'genders' => [Genders::FEMALEGenders::MALEGenders::TRANS],
  85.             'only_profile' => false,
  86.             'only_masseur' => false,
  87.             'only_saloon' => false,
  88.         ];
  89.         if(str_contains($request->get('_route'), 'profile_list.list_by_gender')) {
  90.             if(Genders::getValueByUriIdentity($request->get('gender')) == Genders::MALE) {
  91.                 $params['genders'] = [Genders::MALE];
  92.             } elseif(Genders::getValueByUriIdentity($request->get('gender')) == Genders::TRANS) {
  93.                 $params['genders'] = [Genders::TRANS];
  94.             }
  95.             $params['only_profile'] = true;
  96.         } elseif (str_contains($request->get('_route'), 'masseur_list.page')) {
  97.             $params['only_masseur'] = true;
  98.         } elseif (str_contains($request->get('_route'), 'saloon_list')) {
  99.             $params['only_saloon'] = true;
  100.         }
  101.         return $params;
  102.     }
  103.     /**
  104.      * @deprecated
  105.      * TODO Remove?
  106.      */
  107.     private function filterDeletedProfilesAndSaloons(ORMQueryResult $result): array
  108.     {
  109.         $result iterator_to_array($result->getIterator());
  110.         $placementsIds array_map(function(SidebarPlacement $placement): int {
  111.             return $placement->getId();
  112.         }, $result);
  113.         $filteredIds $this->sidebarPlacementRepository->filterHavingEntityNotDeleted($placementsIds);
  114.         $result array_filter($result, function(SidebarPlacement $placement) use ($filteredIds): bool {
  115.             return in_array($placement->getId(), $filteredIds);
  116.         });
  117.         
  118.         $profileIds array_map(function(SidebarPlacement $sidebarPlacement) {
  119.             return ($sidebarPlacement instanceof SidebarPlacement) ? $sidebarPlacement->getProfile()->getId() : null;
  120.         }, iterator_to_array($result->getIterator()));
  121.         $profileIds array_filter($profileIds);
  122.         $this->eventDispatcher->dispatch(new ProfilesShownEvent($profileIds'deprecated'), ProfilesShownEvent::NAME);
  123.         return $result;
  124.     }
  125.     public function getPlacementNameUltraVip(): ?string
  126.     {
  127.         return $this->features->free_profiles() ? 'ULTRA' 'Ultra VIP';
  128.     }
  129.     public function getPlacementNameVip(): ?string
  130.     {
  131.         return $this->features->free_profiles() ? 'GOLD' 'VIP';
  132.     }
  133.     public function getPlacementNameStandard(): ?string
  134.     {
  135.         return $this->features->free_profiles() ? 'SILVER' '';
  136.     }
  137. }