src/Controller/ProfilePreviewController.php line 102

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-03-19
  5.  * Time: 22:20
  6.  */
  7. namespace App\Controller;
  8. use App\Entity\Location\City;
  9. use App\Entity\Profile\Profile;
  10. use App\Entity\ServiceGroups;
  11. use App\Repository\ServiceRepository;
  12. use App\Serializer\Normalizer\ProfileNormalizer;
  13. use App\Service\ModerationService;
  14. use App\Service\NearestProfiles;
  15. use App\Service\ProfileIndividuality;
  16. use App\Service\ProfileList;
  17. use App\Specification\ElasticSearch\Spec;
  18. use Carbon\CarbonImmutable;
  19. use Nelmio\ApiDocBundle\Attribute\Model;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  24. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\HttpKernel\Exception\GoneHttpException;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. use OpenApi\Attributes as OA;
  30. #[Cache(maxage: 600, public: true)]
  31. class ProfilePreviewController extends AbstractController
  32. {
  33.     use ProfileCommentsTrait;
  34.     use ResponseTrait;
  35.     #[ParamConverter("city", converter: "city_converter")]
  36.     #[Entity("profile", expr: "repository.ofUriIdentityWithinCity(profile, city)")]
  37.     #[Route("/api/profiles/city/{city}/preview/{profile}", name: "api.profiles.preview", methods: "GET", format: "json")]
  38.     #[OA\Tag(name: "Preview")]
  39.     #[OA\Response(
  40.         response: 200,
  41.         description: 'Полная информация анкеты',
  42.         content: new OA\JsonContent(allOf: [
  43.             new OA\Schema(ref: new Model(type: Profile::class, groups: ['preview', 'comments'])),
  44.             new OA\Schema(properties: [new OA\Property(property: 'is_individual', type: 'boolean')]),
  45.         ])
  46.     )]
  47.     public function page(
  48.         Request               $request,
  49.         ServiceRepository     $serviceRepository,
  50.         City                  $city,
  51.         Profile               $profile,
  52.         ModerationService     $moderationService,
  53.         ParameterBagInterface $parameterBag,
  54.         NearestProfiles       $nearestProfiles,
  55.         ProfileIndividuality  $profileIndividuality,
  56.     ): Response
  57.     {
  58.         //  DMCA hard delete — всегда 404
  59.         if ($profile->isHardDeleted()) {
  60.             throw $this->createNotFoundException();
  61.         }
  62.         $showHttp200FromDate = $this->createDateFromParameter(
  63.             $parameterBag->get('app.profile.page.deleted_profile_http_200_starting_from')
  64.         );
  65.         if ($profile->isDeleted() && $profile->getDeletedAt() < $showHttp200FromDate) {
  66.             throw new GoneHttpException();
  67.         }
  68.         //$entityManager->getFilters()->enable('not_deleted_profile_filter');
  69.         if ($profile->isModerationRejected()) {
  70.             //показываем
  71.         } else if (false == $moderationService->isProfileEligibleToShowByModeration($profile)) {
  72.             throw $this->createNotFoundException();
  73.         }
  74.         $isIndividual = $profileIndividuality->isIndividual($profile);
  75.         if ($this->isApiRequest($request)) {
  76.             return $this->json($profile, context: [
  77.                 'groups' => ['preview', 'comments'],
  78.                 'is_individual' => $isIndividual,
  79.             ]);
  80.         }
  81.         $services = $serviceRepository->allIndexedByGroup();
  82.         $parameters = [
  83.             'profile' => $profile,
  84.             'profile_saloon' => $profile->getSaloon(),
  85.             'services' => $services,
  86.             'masseurExcludeServiceGroups' => [
  87.                 ServiceGroups::SEX, ServiceGroups::EXTREME, ServiceGroups::BDSM, ServiceGroups::ROLE_PLAYING_AND_COSTUMES,
  88.             ],
  89.             'recommendationSpec' => Spec::andX(),
  90.             'rating' => $this->countAverageRating($profile),
  91.             'nearest_profiles' => $nearestProfiles->getNearestProfiles($profile, 6),
  92.             'is_individual' => $isIndividual,
  93.         ];
  94.         return $this->render('ProfilePreview/page.html.twig', $parameters);
  95.     }
  96.     private function createDateFromParameter(string $value): CarbonImmutable
  97.     {
  98.         $value = trim(html_entity_decode($value, ENT_QUOTES), '"\' ');
  99.         $value = preg_replace('/([+-]\d{2}:\d{2})([+-]\d{2}:\d{2})$/', '$1', $value);
  100.         return CarbonImmutable::createFromTimeString($value);
  101.     }
  102. }