src/Controller/ProfilePreviewController.php line 139

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\ProfileIndividuality;
  17. use App\Service\ProfileList;
  18. use App\Service\ProfileTagTileService;
  19. use App\Specification\ElasticSearch\Spec;
  20. use Carbon\CarbonImmutable;
  21. use Nelmio\ApiDocBundle\Attribute\Model;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  26. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpFoundation\Response;
  29. use Symfony\Component\HttpKernel\Exception\GoneHttpException;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. use OpenApi\Attributes as OA;
  32. #[Cache(maxage: 600, public: true)]
  33. class ProfilePreviewController extends AbstractController
  34. {
  35.     use ProfileCommentsTrait;
  36.     use ResponseTrait;
  37.     #[ParamConverter("city", converter: "city_converter")]
  38.     #[Entity("profile", expr: "repository.ofUriIdentityWithinCity(profile, city)")]
  39.     #[Route("/api/profiles/city/{city}/preview/{profile}", name: "api.profiles.preview", methods: "GET", format: "json")]
  40.     #[OA\Tag(name: "Preview")]
  41.     #[OA\Response(
  42.         response: 200,
  43.         description: 'Полная информация анкеты',
  44.         content: new OA\JsonContent(allOf: [
  45.             new OA\Schema(ref: new Model(type: Profile::class, groups: ['preview', 'comments'])),
  46.             new OA\Schema(properties: [
  47.                 new OA\Property(property: 'is_individual', type: 'boolean'),
  48.                 new OA\Property(
  49.                     property: 'tagTiles',
  50.                     type: 'array',
  51.                     items: new OA\Items(
  52.                         properties: [
  53.                             new OA\Property(property: 'label', type: 'string'),
  54.                             new OA\Property(property: 'routeName', type: 'string'),
  55.                             new OA\Property(property: 'routeParams', type: 'object'),
  56.                         ],
  57.                         type: 'object'
  58.                     )
  59.                 ),
  60.             ]),
  61.         ])
  62.     )]
  63.     public function page(
  64.         Request               $request,
  65.         ServiceRepository     $serviceRepository,
  66.         City                  $city,
  67.         Profile               $profile,
  68.         ModerationService     $moderationService,
  69.         ParameterBagInterface $parameterBag,
  70.         NearestProfiles       $nearestProfiles,
  71.         ProfileIndividuality  $profileIndividuality,
  72.         ProfileTagTileService $profileTagTileService,
  73.         Features              $features,
  74.     ): Response
  75.     {
  76.         //  DMCA hard delete — всегда 404
  77.         if ($profile->isHardDeleted()) {
  78.             throw $this->createNotFoundException();
  79.         }
  80.         $showHttp200FromDate = $this->createDateFromParameter(
  81.             $parameterBag->get('app.profile.page.deleted_profile_http_200_starting_from')
  82.         );
  83.         $softDeleted410 = $features->isActive('410_soft_delete') && $profile->isDeleted();
  84.         if (!$softDeleted410 && $profile->isDeleted() && $profile->getDeletedAt() < $showHttp200FromDate) {
  85.             throw new GoneHttpException();
  86.         }
  87.         //$entityManager->getFilters()->enable('not_deleted_profile_filter');
  88.         if ($profile->isModerationRejected()) {
  89.             //показываем
  90.         } else if (false == $moderationService->isProfileEligibleToShowByModeration($profile)) {
  91.             throw $this->createNotFoundException();
  92.         }
  93.         $isIndividual = $profileIndividuality->isIndividual($profile);
  94.         $tagTiles = $profileTagTileService->links($profile);
  95.         if ($this->isApiRequest($request)) {
  96.             return $this->json(
  97.                 $profile,
  98.                 $softDeleted410 ? Response::HTTP_GONE : Response::HTTP_OK,
  99.                 context: [
  100.                     'groups' => ['preview', 'comments'],
  101.                     'is_individual' => $isIndividual,
  102.                     'tag_tiles' => $tagTiles,
  103.                 ]
  104.             );
  105.         }
  106.         $services = $serviceRepository->allIndexedByGroup();
  107.         $parameters = [
  108.             'profile' => $profile,
  109.             'profile_saloon' => $profile->getSaloon(),
  110.             'services' => $services,
  111.             'masseurExcludeServiceGroups' => [
  112.                 ServiceGroups::SEX, ServiceGroups::EXTREME, ServiceGroups::BDSM, ServiceGroups::ROLE_PLAYING_AND_COSTUMES,
  113.             ],
  114.             'recommendationSpec' => Spec::andX(),
  115.             'rating' => $this->countAverageRating($profile),
  116.             'nearest_profiles' => $nearestProfiles->getNearestProfiles($profile, 6),
  117.             'is_individual' => $isIndividual,
  118.             'tag_tiles' => $tagTiles,
  119.         ];
  120.         $response = new Response();
  121.         if ($softDeleted410) {
  122.             $response->setStatusCode(Response::HTTP_GONE);
  123.         }
  124.         return $this->render('ProfilePreview/page.html.twig', $parameters, $response);
  125.     }
  126.     private function createDateFromParameter(string $value): CarbonImmutable
  127.     {
  128.         $value = trim(html_entity_decode($value, ENT_QUOTES), '"\' ');
  129.         $value = preg_replace('/([+-]\d{2}:\d{2})([+-]\d{2}:\d{2})$/', '$1', $value);
  130.         return CarbonImmutable::createFromTimeString($value);
  131.     }
  132. }