<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-03-28
* Time: 16:32
*/
namespace App\Twig;
use App\Entity\SEO\PageMetadata;
use App\Entity\Profile\BodyTypes;
use App\Entity\Profile\HairColors;
use App\Service\CanonicalUrlGenerator;
use App\Service\PageMetadataGenerator;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class SeoExtension extends AbstractExtension
{
public function __construct(
protected PageMetadataGenerator $metadataGenerator,
protected RequestStack $requestStack,
protected CanonicalUrlGenerator $canonicalUrlGenerator,
protected TranslatorInterface $translator
) {}
public function getFunctions()
{
return [
new TwigFunction('canonical_url', [$this, 'canonicalUrl']),
new TwigFunction('seo_title', [$this, 'metaTitle']),
new TwigFunction('_seo_title_fallback', [$this, 'metaTitleFallback']),
new TwigFunction('seo_description', [$this, 'metaDescription']),
new TwigFunction('_seo_description_fallback', [$this, 'metaDescriptionFallback']),
new TwigFunction('seo_keywords', [$this, 'metaKeywords']),
new TwigFunction('_seo_keywords_fallback', [$this, 'metaKeywordsFallback']),
new TwigFunction('seo_heading', [$this, 'pageHeading'], ['is_safe' => ['html']]),
new TwigFunction('_seo_heading_fallback', [$this, 'pageHeadingFallback'], ['is_safe' => ['html']]),
new TwigFunction('seo_heading_profile', [$this, 'profileHeadingGenerator']),
new TwigFunction('seo_top', [$this, 'topHtml'], ['is_safe' => ['html']]),
new TwigFunction('seo_footer', [$this, 'bottomHtml'], ['is_safe' => ['html']]),
];
}
public function getFilters()
{
return [
new TwigFilter('seo_morphing', [$this, 'morphing']),
];
}
public function canonicalUrl(string $host = null): string
{
return $this->canonicalUrlGenerator->generate($host);
}
public function morphing($variants, int $index): string
{
if (empty($variants)) {
return '';
}
if (!is_array($variants)) {
if (!is_string($variants)) {
throw new \InvalidArgumentException(sprintf('Unexpected type %s for morphing variants.', gettype($variants)));
}
$variants = explode('|', $variants);
}
return $variants[$index % count($variants)];
}
public function topHtml(): ?string
{
$page = $this->getPage();
if ($page) {
$translation = $this->translator->trans($page->getTopHtml());
if (!empty($translation)) {
return $translation;
}
}
return $this->metadataGenerator->topHtml();
}
public function bottomHtml(): ?string
{
$page = $this->getPage();
if ($page) {
$translation = $this->translator->trans($page->getBottomHtml());
if (!empty($translation)) {
return $translation;
}
}
return $this->metadataGenerator->bottomHtml();
}
public function metaTitleFallback(): ?string
{
$page = $this->getFallbackPage();
if (!$page) {
return null;
}
$translation = $this->translator->trans($page->getMetaTitle());
if (empty($translation)) {
return null;
}
return $translation;
}
public function metaTitle(): ?string
{
$page = $this->getPage();
if ($page) {
$translation = $this->translator->trans($page->getMetaTitle());
if (!empty($translation)) {
return $translation;
}
}
return $this->metadataGenerator->metaTitle();
}
public function metaDescriptionFallback(): ?string
{
$page = $this->getFallbackPage();
if (!$page) {
return null;
}
$translation = $this->translator->trans($page->getMetaDescription());
if (empty($translation)) {
return null;
}
return $translation;
}
public function metaDescription(): ?string
{
$page = $this->getPage();
if ($page) {
$translation = $this->translator->trans($page->getMetaDescription());
if (!empty($translation)) {
return $translation;
}
}
return $this->metadataGenerator->metaDescription();
}
public function metaKeywordsFallback(): ?string
{
$page = $this->getFallbackPage();
if (!$page) {
return null;
}
$translation = $this->translator->trans($page->getMetaKeywords());
if (empty($translation)) {
return null;
}
return $translation;
}
public function metaKeywords(): ?string
{
$page = $this->getPage();
if ($page) {
$translation = $this->translator->trans($page->getMetaKeywords());
if (!empty($translation)) {
return $translation;
}
}
return $this->metadataGenerator->metaKeywords();
}
public function pageHeadingFallback(): ?string
{
$page = $this->getFallbackPage();
if (!$page) {
return null;
}
$translation = $this->translator->trans($page->getPageHeading());
if (empty($translation)) {
return null;
}
return $translation;
}
public function pageHeading(): ?string
{
$page = $this->getPage();
if ($page) {
$translation = $this->translator->trans($page->getPageHeading());
if (!empty($translation)) {
return $translation;
}
}
return $this->metadataGenerator->pageHeading();
}
/**
* Функция генератор заголовков H1 для страницы Профиля модели
*
* Функция получает объект profile(данные модели)
* На выход отдаёт строку для H1
*
* таск https://redminez.net/issues/27126#note-2
*/
public function profileHeadingGenerator($model): string
{
if (empty($model)) {
return '';
}
$LOW_TRIGGER_PRICE = 1500;
$HIGH_TRIGGER_PRICE = 5000;
$title = $this->translator->trans($model->isMasseur() ? 'массажистка' : 'проститутка', [], 'seo_heading');
$age = $model->getPersonParameters()->getAge();
$bodyType = BodyTypes::getLabel($model->getPersonParameters()->getBodyType()); // 1.худая 2стройная 3спорт 4плотная 5толстая
$hairColor = HairColors::getLabel($model->getPersonParameters()->getHairColor()); // 1брюнетка,5блонд
$priceTakeOut = $model->getTakeOutPricing()->getOneHourPrice();
$priceApart = $model->getApartmentsPricing()->getOneHourPrice();
$prefix = null;
if ( $priceApart >= $HIGH_TRIGGER_PRICE and !is_null($priceApart) ){
$prefix = 'VIP';
}
else if ( $priceTakeOut >= $HIGH_TRIGGER_PRICE and !is_null($priceTakeOut) ){
$prefix = 'VIP';
}
else if ( $priceApart <= $LOW_TRIGGER_PRICE and !is_null($priceApart) ){
$prefix = $this->translator->trans('Дешевая', [], 'seo_heading');
}
else if ( $priceTakeOut <= $LOW_TRIGGER_PRICE and !is_null($priceTakeOut) ){
$prefix = $this->translator->trans('Дешевая', [], 'seo_heading');
}
else if ( $bodyType == 2 and ($hairColor == 'BLONDE' or $hairColor == 'BRUNETTE') ){
$prefix = $this->translator->trans('Красивая', [], 'seo_heading');
}
else if ( $age < 20 ){
$prefix = $this->translator->trans('Молодая', [], 'seo_heading');
}
else if ( $age > 35 ){
$prefix = $this->translator->trans('Взрослая', [], 'seo_heading');
}
else if ( $bodyType ){
$prefix = $this->translator->trans($bodyType, [], 'body_types');
}
$translationParameters = [
'prefix' => $prefix,
'title' => $title,
];
$translation = $this->translator->trans($prefix ? 'complex_name_prefix' : 'name_prefix', $translationParameters, 'profile');
return ucfirst($translation);
}
private function getPage(): ?PageMetadata
{
$request = $this->requestStack->getMasterRequest();
if (!$request) {
return null;
}
return $request->attributes->get(PageMetadata::PAGE_REQUEST_ATTRIBUTE);
}
private function getFallbackPage(): ?PageMetadata
{
$request = $this->requestStack->getMasterRequest();
if (!$request) {
return null;
}
return $request->attributes->get(PageMetadata::FALLBACK_REQUEST_ATTRIBUTE);
}
}