src/Controller/ProfilePreviewController.php line 48

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\Features;
  14. use App\Service\ModerationService;
  15. use App\Service\NearestProfiles;
  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(ref: new Model(type: Profile::class, groups: ['profile']))
  43.     )]
  44.     public function page(
  45.         Request               $request,
  46.         ServiceRepository     $serviceRepository,
  47.         City                  $city,
  48.         Profile               $profile,
  49.         ModerationService     $moderationService,
  50.         ParameterBagInterface $parameterBag,
  51.         NearestProfiles       $nearestProfiles,
  52.         Features              $features,
  53.     ): Response
  54.     {
  55.         //  DMCA hard delete — всегда 404
  56.         if ($profile->isHardDeleted()) {
  57.             throw $this->createNotFoundException();
  58.         }
  59.         $showHttp200FromDate = $this->createDateFromParameter(
  60.             $parameterBag->get('app.profile.page.deleted_profile_http_200_starting_from')
  61.         );
  62.         $softDeleted410 = $features->isActive('410_soft_delete') && $profile->isDeleted();
  63.         if (!$softDeleted410 && $profile->isDeleted() && $profile->getDeletedAt() < $showHttp200FromDate) {
  64.             throw new GoneHttpException();
  65.         }
  66.         //$entityManager->getFilters()->enable('not_deleted_profile_filter');
  67.         if ($profile->isModerationRejected()) {
  68.             //показываем
  69.         } else if (false == $moderationService->isProfileEligibleToShowByModeration($profile)) {
  70.             throw $this->createNotFoundException();
  71.         }
  72.         if ($this->isApiRequest($request)) {
  73.             return $this->json(
  74.                 $profile,
  75.                 $softDeleted410 ? Response::HTTP_GONE : Response::HTTP_OK,
  76.                 context: ['groups' => ['profile']]
  77.             );
  78.         }
  79.         $services = $serviceRepository->allIndexedByGroup();
  80.         $parameters = [
  81.             'profile' => $profile,
  82.             'services' => $services,
  83.             'masseurExcludeServiceGroups' => [
  84.                 ServiceGroups::SEX, ServiceGroups::EXTREME, ServiceGroups::BDSM, ServiceGroups::MISC,
  85.             ],
  86.             'recommendationSpec' => Spec::andX(),
  87.             'rating' => $this->countAverageRating($profile),
  88.             'nearest_profiles' => $nearestProfiles->getNearestProfiles($profile, 6),
  89.         ];
  90.         $response = new Response();
  91.         if ($softDeleted410) {
  92.             $response->setStatusCode(Response::HTTP_GONE);
  93.         }
  94.         return $this->render('ProfilePreview/page.html.twig', $parameters, $response);
  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. }