src/Core/Router/Router.php line 65

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2020 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2020 TECLA Consulting Group oü
  16.  */
  17. namespace App\Core\Router;
  18. use App\Component\Identity\Encoder\IdEncoderInterface;
  19. use App\Component\Identity\IdInterface;
  20. use App\Contact\Security\ContactContextInterface;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  23. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  24. use Symfony\Component\Routing\RequestContext;
  25. use Symfony\Component\Routing\RouteCollection;
  26. use Symfony\Component\Routing\RouterInterface;
  27. final class Router implements RouterInterfaceRequestMatcherInterfaceWarmableInterface
  28. {
  29.     private RouterInterface $router;
  30.     private IdEncoderInterface $idEncoder;
  31.     private ContactContextInterface $contactContext;
  32.     public function __construct(
  33.         RouterInterface $router,
  34.         IdEncoderInterface $idEncoder,
  35.         ContactContextInterface $contactContext
  36.     ) {
  37.         $this->router $router;
  38.         $this->idEncoder $idEncoder;
  39.         $this->contactContext $contactContext;
  40.     }
  41.     public function setContext(RequestContext $context)
  42.     {
  43.         return $this->router->setContext($context);
  44.     }
  45.     public function getContext(): RequestContext
  46.     {
  47.         return $this->router->getContext();
  48.     }
  49.     public function matchRequest(Request $request): array
  50.     {
  51.         if (false === $this->router instanceof RequestMatcherInterface) {
  52.             throw new \BadMethodCallException(sprintf('Router has to implement the "%s".'RequestMatcherInterface::class));
  53.         }
  54.         return $this->router->matchRequest($request);
  55.     }
  56.     public function getRouteCollection(): RouteCollection
  57.     {
  58.         return $this->router->getRouteCollection();
  59.     }
  60.     public function generate(string $name, array $parameters = [], int $referenceType self::ABSOLUTE_PATH): string
  61.     {
  62.         if (false === str_starts_with($name'_')) {
  63.             $this->addContextParameter($parameters);
  64.             $this->encodeParametersIds($parameters);
  65.         }
  66.         return $this->router->generate($name$parameters$referenceType);
  67.     }
  68.     public function match(string $pathinfo): array
  69.     {
  70.         return $this->router->match($pathinfo);
  71.     }
  72.     public function warmUp(string $cacheDir): array
  73.     {
  74.         if ($this->router instanceof WarmableInterface) {
  75.             $this->router->warmUp($cacheDir);
  76.         }
  77.         return [];
  78.     }
  79.     private function addContextParameter(array &$parameters): void
  80.     {
  81.         if (true === \array_key_exists('cid'$parameters)) {
  82.             return;
  83.         }
  84.         if (null === $currentContact $this->contactContext->getCurrent()) {
  85.             return;
  86.         }
  87.         $parameters['cid'] = $currentContact->getId();
  88.     }
  89.     private function encodeParametersIds(array &$parameters): void
  90.     {
  91.         foreach ($parameters as $key => $parameter) {
  92.             if (false === $parameter instanceof IdInterface) {
  93.                 continue;
  94.             }
  95.             $parameters[$key] = $this->idEncoder->encode($parameter);
  96.         }
  97.     }
  98. }