src/Twig/PaidPlacementsExtension.php line 52

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