<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-04-25
* Time: 13:45
*/
namespace App\Twig;
use App\Entity\Profile\Genders;
use App\Entity\Profile\Profile;
use App\Entity\Sales\SidebarPlacement;
use App\Entity\Sales\SidebarPlacementType;
use App\Event\Profile\ProfilesShownEvent;
use App\Repository\SidebarPlacementRepository;
use App\Service\CurrentCityResolver;
use App\Service\Features;
use App\Service\ProfileTopBoard;
use App\Service\Sidebar;
use Porpaginas\Doctrine\ORM\ORMQueryResult;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class PaidPlacementsExtension extends AbstractExtension
{
public function __construct(
private RequestStack $requestStack,
private SidebarPlacementRepository $sidebarPlacementRepository,
private Features $features,
private Sidebar $sidebar,
private EventDispatcherInterface $eventDispatcher,
private ProfileTopBoard $profileTopBoard,
private CurrentCityResolver $currentCityResolver
) {}
public function getFunctions()
{
return [
new TwigFunction('profile_top_placement', [$this, 'currentTopPlacement']),
new TwigFunction('profile_ultra_vip_placements', [$this, 'currentUltraVipPlacements']),
new TwigFunction('profile_vip_placements', [$this, 'currentVipPlacements']),
new TwigFunction('placement_name_ultra_vip', [$this, 'getPlacementNameUltraVip']),
new TwigFunction('placement_name_vip', [$this, 'getPlacementNameVip']),
new TwigFunction('placement_name_standard', [$this, 'getPlacementNameStandard']),
];
}
public function currentTopPlacement(): ?Profile
{
return $this->profileTopBoard->currentTopPlacement(true);
}
public function currentUltraVipPlacements(): array
{
$filters = $this->getSidebarPlacementsFilters();
$city = $this->currentCityResolver->resolveCurrentCity();
return $this->sidebar->getPlacementsToShow(
$city, SidebarPlacementType::ULTRA_VIP, 1, [],
$filters['genders'], $filters['only_profile'], $filters['only_masseur'], $filters['only_saloon']
);
}
public function currentVipPlacements(): array
{
$filters = $this->getSidebarPlacementsFilters();
$city = $this->currentCityResolver->resolveCurrentCity();
return $this->sidebar->getPlacementsToShow(
$city, SidebarPlacementType::VIP, 4, [],
$filters['genders'], $filters['only_profile'], $filters['only_masseur'], $filters['only_saloon']
);
}
private function getSidebarPlacementsFilters(): array
{
$request = $this->requestStack->getCurrentRequest();
$params = [
'genders' => [Genders::FEMALE, Genders::MALE, Genders::TRANS],
'only_profile' => false,
'only_masseur' => false,
'only_saloon' => false,
];
if(str_contains($request->get('_route'), 'profile_list.list_by_gender')) {
if(Genders::getValueByUriIdentity($request->get('gender')) == Genders::MALE) {
$params['genders'] = [Genders::MALE];
} elseif(Genders::getValueByUriIdentity($request->get('gender')) == Genders::TRANS) {
$params['genders'] = [Genders::TRANS];
}
$params['only_profile'] = true;
} elseif (str_contains($request->get('_route'), 'masseur_list.page')) {
$params['only_masseur'] = true;
} elseif (str_contains($request->get('_route'), 'saloon_list')) {
$params['only_saloon'] = true;
}
return $params;
}
/**
* @deprecated
* TODO Remove?
*/
private function filterDeletedProfilesAndSaloons(ORMQueryResult $result): array
{
$result = iterator_to_array($result->getIterator());
$placementsIds = array_map(function(SidebarPlacement $placement): int {
return $placement->getId();
}, $result);
$filteredIds = $this->sidebarPlacementRepository->filterHavingEntityNotDeleted($placementsIds);
$result = array_filter($result, function(SidebarPlacement $placement) use ($filteredIds): bool {
return in_array($placement->getId(), $filteredIds);
});
$profileIds = array_map(function(SidebarPlacement $sidebarPlacement) {
return ($sidebarPlacement instanceof SidebarPlacement) ? $sidebarPlacement->getProfile()->getId() : null;
}, iterator_to_array($result->getIterator()));
$profileIds = array_filter($profileIds);
$this->eventDispatcher->dispatch(new ProfilesShownEvent($profileIds, 'deprecated'), ProfilesShownEvent::NAME);
return $result;
}
public function getPlacementNameUltraVip(): ?string
{
return $this->features->free_profiles() ? 'ULTRA' : 'Ultra VIP';
}
public function getPlacementNameVip(): ?string
{
return $this->features->free_profiles() ? 'GOLD' : 'VIP';
}
public function getPlacementNameStandard(): ?string
{
return $this->features->free_profiles() ? 'SILVER' : '';
}
}