<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-03-19
* Time: 22:20
*/
namespace App\Controller;
use App\Entity\Location\City;
use App\Entity\Profile\Profile;
use App\Entity\ServiceGroups;
use App\Repository\ServiceRepository;
use App\Serializer\Normalizer\ProfileNormalizer;
use App\Service\ModerationService;
use App\Service\NearestProfiles;
use App\Service\ProfileList;
use App\Specification\ElasticSearch\Spec;
use Carbon\CarbonImmutable;
use Nelmio\ApiDocBundle\Attribute\Model;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\GoneHttpException;
use Symfony\Component\Routing\Annotation\Route;
use OpenApi\Attributes as OA;
#[Cache(maxage: 600, public: true)]
class ProfilePreviewController extends AbstractController
{
use ProfileCommentsTrait;
use ResponseTrait;
#[ParamConverter("city", converter: "city_converter")]
#[Entity("profile", expr: "repository.ofUriIdentityWithinCity(profile, city)")]
#[Route("/api/profiles/city/{city}/preview/{profile}", name: "api.profiles.preview", methods: "GET", format: "json")]
#[OA\Tag(name: "Preview")]
#[OA\Response(
response: 200,
description: 'Полная информация анкеты',
content: new OA\JsonContent(ref: new Model(type: Profile::class, groups: ['preview', 'comments']))
)]
public function page(
Request $request,
ServiceRepository $serviceRepository,
City $city,
Profile $profile,
ModerationService $moderationService,
ParameterBagInterface $parameterBag,
NearestProfiles $nearestProfiles,
): Response
{
// DMCA hard delete — всегда 404
if ($profile->isHardDeleted()) {
throw $this->createNotFoundException();
}
$showHttp200FromDate = $this->createDateFromParameter(
$parameterBag->get('app.profile.page.deleted_profile_http_200_starting_from')
);
if ($profile->isDeleted() && $profile->getDeletedAt() < $showHttp200FromDate) {
throw new GoneHttpException();
}
//$entityManager->getFilters()->enable('not_deleted_profile_filter');
if ($profile->isModerationRejected()) {
//показываем
} else if (false == $moderationService->isProfileEligibleToShowByModeration($profile)) {
throw $this->createNotFoundException();
}
if ($this->isApiRequest($request)) {
return $this->json($profile, context: ['groups' => ['preview', 'comments']]);
}
$services = $serviceRepository->allIndexedByGroup();
$parameters = [
'profile' => $profile,
'profile_saloon' => $profile->getSaloon(),
'services' => $services,
'masseurExcludeServiceGroups' => [
ServiceGroups::SEX, ServiceGroups::EXTREME, ServiceGroups::BDSM, ServiceGroups::MISC,
],
'recommendationSpec' => Spec::andX(),
'rating' => $this->countAverageRating($profile),
'nearest_profiles' => $nearestProfiles->getNearestProfiles($profile, 6),
];
return $this->render('ProfilePreview/page.html.twig', $parameters);
}
private function createDateFromParameter(string $value): CarbonImmutable
{
$value = trim(html_entity_decode($value, ENT_QUOTES), '"\' ');
$value = preg_replace('/([+-]\d{2}:\d{2})([+-]\d{2}:\d{2})$/', '$1', $value);
return CarbonImmutable::createFromTimeString($value);
}
}