src/Twig/SeoExtension.php line 57

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-03-28
  5.  * Time: 16:32
  6.  */
  7. namespace App\Twig;
  8. use App\Entity\SEO\PageMetadata;
  9. use App\Entity\Profile\BodyTypes;
  10. use App\Entity\Profile\HairColors;
  11. use App\Service\CanonicalUrlGenerator;
  12. use App\Service\PageMetadataGenerator;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFilter;
  17. use Twig\TwigFunction;
  18. class SeoExtension extends AbstractExtension
  19. {
  20.     public function __construct(
  21.         protected PageMetadataGenerator $metadataGenerator,
  22.         protected RequestStack $requestStack,
  23.         protected CanonicalUrlGenerator $canonicalUrlGenerator,
  24.         protected TranslatorInterface $translator
  25.     ) {}
  26.     public function getFunctions()
  27.     {
  28.         return [
  29.             new TwigFunction('canonical_url', [$this'canonicalUrl']),
  30.             new TwigFunction('seo_title', [$this'metaTitle']),
  31.             new TwigFunction('_seo_title_fallback', [$this'metaTitleFallback']),
  32.             new TwigFunction('seo_description', [$this'metaDescription']),
  33.             new TwigFunction('_seo_description_fallback', [$this'metaDescriptionFallback']),
  34.             new TwigFunction('seo_keywords', [$this'metaKeywords']),
  35.             new TwigFunction('_seo_keywords_fallback', [$this'metaKeywordsFallback']),
  36.             new TwigFunction('seo_heading', [$this'pageHeading'], ['is_safe' => ['html']]),
  37.             new TwigFunction('_seo_heading_fallback', [$this'pageHeadingFallback'], ['is_safe' => ['html']]),
  38.             new TwigFunction('seo_heading_profile', [$this'profileHeadingGenerator']),
  39.             new TwigFunction('seo_top', [$this'topHtml'], ['is_safe' => ['html']]),
  40.             new TwigFunction('seo_footer', [$this'bottomHtml'], ['is_safe' => ['html']]),
  41.         ];
  42.     }
  43.     public function getFilters()
  44.     {
  45.         return [
  46.             new TwigFilter('seo_morphing', [$this'morphing']),
  47.         ];
  48.     }
  49.     public function canonicalUrl(string $host null): string
  50.     {
  51.         return $this->canonicalUrlGenerator->generate($host);
  52.     }
  53.     public function morphing($variantsint $index): string
  54.     {
  55.         if (empty($variants)) {
  56.             return '';
  57.         }
  58.         if (!is_array($variants)) {
  59.             if (!is_string($variants)) {
  60.                 throw new \InvalidArgumentException(sprintf('Unexpected type %s for morphing variants.'gettype($variants)));
  61.             }
  62.             $variants explode('|'$variants);
  63.         }
  64.         return $variants[$index count($variants)];
  65.     }
  66.     public function topHtml(): ?string
  67.     {
  68.         $page $this->getPage();
  69.         if ($page) {
  70.             $translation $this->translator->trans($page->getTopHtml());
  71.             if (!empty($translation)) {
  72.                 return $translation;
  73.             }
  74.         }
  75.         return $this->metadataGenerator->topHtml();
  76.     }
  77.     public function bottomHtml(): ?string
  78.     {
  79.         $page $this->getPage();
  80.         if ($page) {
  81.             $translation $this->translator->trans($page->getBottomHtml());
  82.             if (!empty($translation)) {
  83.                 return $translation;
  84.             }
  85.         }
  86.         return $this->metadataGenerator->bottomHtml();
  87.     }
  88.     public function metaTitleFallback(): ?string
  89.     {
  90.         $page $this->getFallbackPage();
  91.         if (!$page) {
  92.             return null;
  93.         }
  94.         $translation $this->translator->trans($page->getMetaTitle());
  95.         if (empty($translation)) {
  96.             return null;
  97.         }
  98.         return $translation;
  99.     }
  100.     public function metaTitle(): ?string
  101.     {
  102.         $page $this->getPage();
  103.         if ($page) {
  104.             $translation $this->translator->trans($page->getMetaTitle());
  105.             if (!empty($translation)) {
  106.                 return $translation;
  107.             }
  108.         }
  109.         return $this->metadataGenerator->metaTitle();
  110.     }
  111.     public function metaDescriptionFallback(): ?string
  112.     {
  113.         $page $this->getFallbackPage();
  114.         if (!$page) {
  115.             return null;
  116.         }
  117.         $translation $this->translator->trans($page->getMetaDescription());
  118.         if (empty($translation)) {
  119.             return null;
  120.         }
  121.         return $translation;
  122.     }
  123.     public function metaDescription(): ?string
  124.     {
  125.         $page $this->getPage();
  126.         if ($page) {
  127.             $translation $this->translator->trans($page->getMetaDescription());
  128.             if (!empty($translation)) {
  129.                 return $translation;
  130.             }
  131.         }
  132.         return $this->metadataGenerator->metaDescription();
  133.     }
  134.     public function metaKeywordsFallback(): ?string
  135.     {
  136.         $page $this->getFallbackPage();
  137.         if (!$page) {
  138.             return null;
  139.         }
  140.         $translation $this->translator->trans($page->getMetaKeywords());
  141.         if (empty($translation)) {
  142.             return null;
  143.         }
  144.         return $translation;
  145.     }
  146.     public function metaKeywords(): ?string
  147.     {
  148.         $page $this->getPage();
  149.         if ($page) {
  150.             $translation $this->translator->trans($page->getMetaKeywords());
  151.             if (!empty($translation)) {
  152.                 return $translation;
  153.             }
  154.         }
  155.         return $this->metadataGenerator->metaKeywords();
  156.     }
  157.     public function pageHeadingFallback(): ?string
  158.     {
  159.         $page $this->getFallbackPage();
  160.         if (!$page) {
  161.             return null;
  162.         }
  163.         $translation $this->translator->trans($page->getPageHeading());
  164.         if (empty($translation)) {
  165.             return null;
  166.         }
  167.         return $translation;
  168.     }
  169.     public function pageHeading(): ?string
  170.     {
  171.         $page $this->getPage();
  172.         if ($page) {
  173.             $translation $this->translator->trans($page->getPageHeading());
  174.             if (!empty($translation)) {
  175.                 return $translation;
  176.             }
  177.         }
  178.         return $this->metadataGenerator->pageHeading();
  179.     }
  180.     /**
  181.      * Функция генератор заголовков H1 для страницы Профиля модели
  182.      *
  183.      * Функция получает объект profile(данные модели)
  184.      * На выход отдаёт строку для H1
  185.      *
  186.      * таск https://redminez.net/issues/27126#note-2
  187.      */
  188.     public function profileHeadingGenerator($model): string
  189.     {
  190.         if (empty($model)) {
  191.             return '';
  192.         }
  193.         $LOW_TRIGGER_PRICE 1500;
  194.         $HIGH_TRIGGER_PRICE 5000;
  195.         $title $this->translator->trans($model->isMasseur() ? 'массажистка' 'проститутка', [], 'seo_heading');
  196.         $age $model->getPersonParameters()->getAge();
  197.         $bodyType BodyTypes::getLabel($model->getPersonParameters()->getBodyType()); // 1.худая 2стройная 3спорт 4плотная 5толстая
  198.         $hairColor HairColors::getLabel($model->getPersonParameters()->getHairColor()); // 1брюнетка,5блонд
  199.         $priceTakeOut $model->getTakeOutPricing()->getOneHourPrice();
  200.         $priceApart $model->getApartmentsPricing()->getOneHourPrice();
  201.         $prefix null;
  202.         if ( $priceApart >= $HIGH_TRIGGER_PRICE and !is_null($priceApart) ){
  203.             $prefix 'VIP';
  204.         }
  205.         else if ( $priceTakeOut >= $HIGH_TRIGGER_PRICE and !is_null($priceTakeOut)  ){
  206.             $prefix 'VIP';
  207.         }
  208.         else if ( $priceApart <= $LOW_TRIGGER_PRICE and !is_null($priceApart)  ){
  209.             $prefix $this->translator->trans('Дешевая', [], 'seo_heading');
  210.         }
  211.         else if ( $priceTakeOut <= $LOW_TRIGGER_PRICE and !is_null($priceTakeOut)  ){
  212.             $prefix $this->translator->trans('Дешевая', [], 'seo_heading');
  213.         }
  214.         else if ( $bodyType == and ($hairColor == 'BLONDE' or $hairColor == 'BRUNETTE') ){
  215.             $prefix $this->translator->trans('Красивая', [], 'seo_heading');
  216.         }
  217.         else if ( $age 20 ){
  218.             $prefix $this->translator->trans('Молодая', [], 'seo_heading');
  219.         }
  220.         else if ( $age 35 ){
  221.             $prefix $this->translator->trans('Взрослая', [], 'seo_heading');
  222.         }
  223.         else if ( $bodyType ){
  224.             $prefix $this->translator->trans($bodyType, [], 'body_types');
  225.         }
  226.         $translationParameters = [
  227.             'prefix' => $prefix,
  228.             'title' => $title,
  229.         ];
  230.         $translation $this->translator->trans($prefix 'complex_name_prefix' 'name_prefix'$translationParameters'profile');
  231.         return ucfirst($translation);
  232.     }
  233.     private function getPage(): ?PageMetadata
  234.     {
  235.         $request $this->requestStack->getMasterRequest();
  236.         if (!$request) {
  237.             return null;
  238.         }
  239.         return $request->attributes->get(PageMetadata::PAGE_REQUEST_ATTRIBUTE);
  240.     }
  241.     private function getFallbackPage(): ?PageMetadata
  242.     {
  243.         $request $this->requestStack->getMasterRequest();
  244.         if (!$request) {
  245.             return null;
  246.         }
  247.         return $request->attributes->get(PageMetadata::FALLBACK_REQUEST_ATTRIBUTE);
  248.     }
  249. }