vendor/sulu/sulu/src/Sulu/Component/Webspace/Analyzer/Attributes/WebsiteRequestProcessor.php line 72

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\Webspace\Analyzer\Attributes;
  11. use Sulu\Component\Content\Mapper\ContentMapperInterface;
  12. use Sulu\Component\Webspace\Analyzer\Exception\UrlMatchNotFoundException;
  13. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  14. use Sulu\Component\Webspace\PortalInformation;
  15. use Symfony\Component\HttpFoundation\Request;
  16. /**
  17.  * Extracts attributes from request for the sulu-website.
  18.  */
  19. class WebsiteRequestProcessor implements RequestProcessorInterface
  20. {
  21.     /**
  22.      * @var WebspaceManagerInterface
  23.      */
  24.     private $webspaceManager;
  25.     /**
  26.      * @var ContentMapperInterface
  27.      */
  28.     private $contentMapper;
  29.     /**
  30.      * @var string
  31.      */
  32.     private $environment;
  33.     public function __construct(
  34.         WebspaceManagerInterface $webspaceManager,
  35.         ContentMapperInterface $contentMapper,
  36.         $environment
  37.     ) {
  38.         $this->webspaceManager $webspaceManager;
  39.         $this->contentMapper $contentMapper;
  40.         $this->environment $environment;
  41.     }
  42.     public function process(Request $requestRequestAttributes $requestAttributes)
  43.     {
  44.         $host $requestAttributes->getAttribute('host');
  45.         $url $host $requestAttributes->getAttribute('path');
  46.         $portalInformations $this->webspaceManager->findPortalInformationsByUrl(
  47.             $url,
  48.             $this->environment
  49.         );
  50.         if (=== \count($portalInformations)) {
  51.             return new RequestAttributes();
  52.         }
  53.         \usort(
  54.             $portalInformations,
  55.             function(PortalInformation $aPortalInformation $b) {
  56.                 if ($a->getPriority() === $b->getPriority()) {
  57.                     return \strlen($a->getUrl()) < \strlen($b->getUrl());
  58.                 }
  59.                 return $a->getPriority() < $b->getPriority();
  60.             }
  61.         );
  62.         /** @var PortalInformation $portalInformation */
  63.         $portalInformation \reset($portalInformations);
  64.         return new RequestAttributes(['portalInformation' => $portalInformation]);
  65.     }
  66.     public function validate(RequestAttributes $attributes)
  67.     {
  68.         if (null === $attributes->getAttribute('portalInformation')) {
  69.             $portalUrls = [];
  70.             foreach ($this->webspaceManager->getPortalInformations() as $portalInformation) {
  71.                 $portalUrls[] = $attributes->getAttribute('scheme') . '://'
  72.                     $portalInformation->getUrl();
  73.             }
  74.             $fullUrl $attributes->getAttribute('scheme') . '://'
  75.                 $attributes->getAttribute('host')
  76.                 . (!\in_array($attributes->getAttribute('port'), ['80''443'], true) ? ':' $attributes->getAttribute('port') : '')
  77.                     . $attributes->getAttribute('path');
  78.             throw new UrlMatchNotFoundException(
  79.                 $fullUrl,
  80.                 $portalUrls
  81.             );
  82.         }
  83.         return true;
  84.     }
  85. }