src/Controller/PageController.php line 149

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Dictionary\ForbiddenEmails;
  4. use App\Dictionary\PageDictionary;
  5. use App\Dictionary\RedisKey;
  6. use App\Redis\RedisBlogArticle;
  7. use App\Redis\RedisCategory;
  8. use App\Redis\RedisPageMetadata;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use App\Service\PageStatus;
  11. use App\Entity\Category;
  12. use App\Entity\Offer;
  13. use App\Dictionary\TypeDictionary;
  14. use App\Entity\Page;
  15. use App\Form\ContactForm;
  16. use Doctrine\ORM\EntityNotFoundException;
  17. use Psr\Cache\InvalidArgumentException;
  18. use Psr\Log\LoggerInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\Form\FormInterface;
  21. use Symfony\Component\HttpFoundation\RedirectResponse;
  22. use Symfony\Component\HttpFoundation\RequestStack;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  25. use Symfony\Component\HttpFoundation\Request;
  26. /**
  27.  * Default controller for each subpage on website
  28.  */
  29. class PageController extends AbstractController
  30. {
  31.     private const TWIG_TEMPLATE_PATH 'content/';
  32.     private const DB_COLUMN_SYSTEM_NAME 'systemName';
  33.     /**
  34.      * @var EntityManagerInterface
  35.      */
  36.     private $em;
  37.     /**
  38.      * @var RequestStack
  39.      */
  40.     private $requestStack;
  41.     /**
  42.      * @var LoggerInterface
  43.      */
  44.     private $logger;
  45.     /**
  46.      * @var Page|null
  47.      */
  48.     private $page;
  49.     /**
  50.      * @var SessionInterface
  51.      */
  52.     private $session;
  53.     /**
  54.      * @var PageStatus
  55.      */
  56.     private $pageStatus;
  57.     /**
  58.      * @var ContactFormMailerController
  59.      */
  60.     private $pageMailer;
  61.     /**
  62.      * @var string|null
  63.      */
  64.     private $requestedPageName;
  65.     /**
  66.      * @var RedisBlogArticle
  67.      */
  68.     private $blogArticle;
  69.     /**
  70.      * @var RedisPageMetadata
  71.      */
  72.     private RedisPageMetadata $redisPageMetadata;
  73.     /**
  74.      * @var string
  75.      */
  76.     private string $pageType;
  77.     /**
  78.      * @var RedisCategory
  79.      */
  80.     private RedisCategory $redisCategory;
  81.     /**
  82.      * @param RequestStack $requestStack
  83.      * @param LoggerInterface $logger
  84.      * @param EntityManagerInterface $em
  85.      * @param SessionInterface $session
  86.      * @param PageStatus $pageStatus
  87.      * @param ContactFormMailerController $pageMailer
  88.      * @param RedisBlogArticle $blogArticle
  89.      * @param RedisPageMetadata $redisPageMetadata
  90.      * @param RedisCategory $redisCategory
  91.      * @param string $pageType
  92.      */
  93.     public function __construct(
  94.         RequestStack $requestStack,
  95.         LoggerInterface $logger,
  96.         EntityManagerInterface $em,
  97.         SessionInterface $session,
  98.         PageStatus $pageStatus,
  99.         ContactFormMailerController $pageMailer,
  100.         RedisBlogArticle $blogArticle,
  101.         RedisPageMetadata $redisPageMetadata,
  102.         RedisCategory $redisCategory,
  103.         $pageType TypeDictionary::PAGE_TYPE_WWW
  104.     )
  105.     {
  106.         $this->requestStack $requestStack;
  107.         $this->requestedPageName $requestStack->getCurrentRequest()->get('page_name');
  108.         $this->logger $logger;
  109.         $this->em $em;
  110.         $this->session $session;
  111.         $this->pageStatus $pageStatus;
  112.         $this->pageMailer $pageMailer;
  113.         $this->blogArticle $blogArticle;
  114.         $this->redisPageMetadata $redisPageMetadata;
  115.         $this->pageType $pageType;
  116.         /** Nie można uzyć session managera, gdyz ta sesja jest wywoływana bezpośrednio ze stałej globalnej $_SESSION */
  117.         if (isset($_SESSION['ogloszenie_filtr'])) {
  118.             unset($_SESSION['ogloszenie_filtr']);
  119.         }
  120.         $this->redisCategory $redisCategory;
  121.     }
  122.     /**
  123.      * Default action run on each subpage
  124.      *
  125.      * @param Request $request
  126.      *
  127.      * @return RedirectResponse|Response
  128.      * @throws EntityNotFoundException
  129.      * @throws InvalidArgumentException
  130.      */
  131.     public function indexAction(Request $request)
  132.     {
  133.         /** @var null|Category $category */
  134.         $category $this->redisCategory->getRedisItem(RedisKey::OFFER_CATEGORIES$this->requestedPageName);
  135.         /** Przekierowanie na stronę wyszukiwania z branżą, jeżeli ktoś wpisze przyjazny adres np. /sale-weselne */
  136.         if ($category !== null) {
  137.             return $this->redirect(PageDictionary::ADRES_STRONY_WWW '/' $this->requestedPageName301);
  138.         }
  139.         /** @var Page Looking for an existing page page */
  140.         $this->page $this->redisPageMetadata->getRedisItem(RedisKey::PAGES_METADATA$this->requestedPageName);
  141.         if ($this->pageStatus->checkPageAvailability() === false) {
  142.             return $this->render("elements/page_not_available_error.html.twig");
  143.         }
  144.         if ($this->page === null || $this->page->getStatus() !== 'T') {
  145.             return $this->render("bundles/TwigBundle/Exception/error404.html.twig");
  146.         }
  147.         $contactForm $this->createForm(ContactForm::class);
  148.         $contactForm->handleRequest($request);
  149.         if ($contactForm->isSubmitted() && $contactForm->isValid()) {
  150.             $email $contactForm->getData()['email'] ?? '';
  151.             $message $contactForm->getData()['description'] ?? '';
  152.             if ($this->validateFrobiddenDomains($email) === false && $this->validateForbiddenStrings($message) === false) {
  153.                 $this->sendContactEmail($contactForm);
  154.             }
  155.         }
  156.         return $this->render('default/index.html.twig', [
  157.             'page' => $this->page,
  158.             'theme_name' => self::TWIG_TEMPLATE_PATH $this->page->getThemeName(),
  159.             'menu_data' => [
  160.                 'blog_articles' => $this->blogArticle->getRedisItems(RedisKey::BLOG_ARTICLES),
  161.                 'categories' => $this->redisCategory->getRedisItems(RedisKey::OFFER_CATEGORIES),
  162.                 'comments' => $this->getLastOpinion(3)
  163.             ],
  164.             'form' => $contactForm->createView(),
  165.             'messages' => [],
  166.         ]);
  167.     }
  168.     /**
  169.      * Sending emails via contact form
  170.      *
  171.      * @param FormInterface $contactForm
  172.      */
  173.     private function sendContactEmail(FormInterface $contactForm)
  174.     {
  175.         try {
  176.             $this->pageMailer->sendMailToAdministrator($contactForm->getData());
  177.             $this->pageMailer->sendMailToSender($contactForm->getData());
  178.             $this->addFlash('success''Formularz kontaktowy został wysłany.');
  179.         } catch (\Swift_TransportException \Swift_RfcComplianceException \Swift_SwiftException $e) {
  180.             $this->logger->error('Blad wysylki emaila z formularza kontaktowego'$e->getMessage());
  181.             $this->addFlash('error''Błąd podczas wysyłki formularza kontaktowego. Prosimy spróbować ponownie.');
  182.         }
  183.     }
  184.     /**
  185.      * @param int $numbersOpinions
  186.      *
  187.      * @return array
  188.      * @todo Zrefaktoryzować poniższy kod, przpeisać na ORM, bardziej obiektowo
  189.      *
  190.      * Pobieranie ostatnio dodanyhch komentarzy
  191.      *
  192.      */
  193.     private function getLastOpinion(int $numbersOpinions 3)
  194.     {
  195.         $offerRepository $this->em->getRepository(Offer::class);
  196.         $lastOfferRate $offerRepository->getLastRateOffer($numbersOpinions);
  197.         $offersId array_column($lastOfferRate'oferta_id');
  198.         return [
  199.             'offer_rate_details' => $offerRepository->getLastRateOfferDetails($offersId),
  200.             'offer_rate' => $lastOfferRate
  201.         ];
  202.     }
  203.     /**
  204.      * Sprawdzenie zakazanych domen, które nie będą wysyłały e-maili poprzez formularzk ontaktowy
  205.      *
  206.      * @param string $email
  207.      *
  208.      * @return bool
  209.      */
  210.     private function validateFrobiddenDomains(string $email ''): bool
  211.     {
  212.         $emailDomain substr($emailstrpos($email'@') + 1255);
  213.         if (in_array(strtolower($emailDomain), ForbiddenEmails::FORBIDDEN_EMAIL_DOMAINS)) {
  214.             return true;
  215.         }
  216.         return false;
  217.     }
  218.     /**
  219.      * Sprawdzenie czy dany fragment tekstu, wystepuje w treści wiadomości
  220.      *
  221.      * @param string $emailMessage
  222.      *
  223.      * @return bool
  224.      */
  225.     private function validateForbiddenStrings(string $emailMessage ''): bool
  226.     {
  227.         $emailMessage strtolower($emailMessage);
  228.         foreach (ForbiddenEmails::FORBIDDEN_STRINGS as $string) {
  229.             $strPosition strpos($emailMessagestrtolower($string));
  230.             if ($strPosition !== false) {
  231.                 return true;
  232.             }
  233.         }
  234.         return false;
  235.     }
  236. }