src/Service/Sync/Routing/Routing.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the nellapp-core package.
  4.  *
  5.  * (c) Benjamin Georgeault
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Service\Sync\Routing;
  11. use App\Entity\App\App;
  12. use App\Repository\App\AppRepository;
  13. use Nellapp\Bundle\SDKBundle\Enum\AppEnum;
  14. use Nellapp\Bundle\SDKBundle\Routing\Utils\ChannelMainDomainUtils;
  15. use Nellapp\Bundle\SDKBundle\Routing\Utils\DomainsRoutingUtils;
  16. use Nellapp\Bundle\SDKBundle\Sync\Routing\RouteCollection;
  17. use Symfony\Component\Routing\Route;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Symfony\Contracts\Cache\CacheInterface;
  20. use Symfony\Contracts\Cache\ItemInterface;
  21. use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
  22. use Symfony\Contracts\HttpClient\HttpClientInterface;
  23. /**
  24.  * Class Routing
  25.  *
  26.  * @author Benjamin Georgeault
  27.  */
  28. class Routing
  29. {
  30.     /** @var RouteCollection[] */
  31.     private array $routes = [];
  32.     public function __construct(
  33.         private CacheInterface      $nellappSyncRoutesCache,
  34.         private HttpClientInterface $client,
  35.         private AppRepository       $appRepository,
  36.         private RouterInterface     $router,
  37.         private string              $superAdminDomain,
  38.         private string              $commercialDomain,
  39.         private string              $syncDomain,
  40.         private bool                $debug false,
  41.         private DomainsRoutingUtils $domainsRoutingUtils,
  42.     )
  43.     {
  44.     }
  45.     /**
  46.      * @return RouteCollection[]
  47.      */
  48.     public function getCollections(?App $ignoreApp nullbool $cache true): array
  49.     {
  50.         $array = [
  51.             'core' => RouteCollection::createFromSymfonyCollection($this->router->getRouteCollection(), AppEnum::CORE$this->domainsRoutingUtils->getDefaultDomain(),
  52.                 function (Route $route) {
  53.                     return !in_array($route->getHost(), [
  54.                         $this->superAdminDomain,
  55.                         $this->syncDomain,
  56.                     ]);
  57.                 }),
  58.         ];
  59.         foreach ($this->appRepository->findAllExclude($ignoreApp) as $app) {
  60.             if ((null === $url $app->getUrl()) || !filter_var($urlFILTER_VALIDATE_URL)) {
  61.                 continue;
  62.             }
  63.             $routes $cache $this->getRoutes($app) : $this->doGetRoutes($app);
  64.             if (null !== $routes) {
  65.                 $array[$app->getQueueName()] = $routes;
  66.             }
  67.         }
  68.         return $array;
  69.     }
  70.     private function getRoutes(App $app): ?RouteCollection
  71.     {
  72.         if (array_key_exists($queue $app->getQueueName(), $this->routes)) {
  73.             return $this->routes[$queue];
  74.         }
  75.         return $this->routes[$queue] = $this->nellappSyncRoutesCache->get(sprintf('global_routes_%s'$app->getQueueName()), function (ItemInterface $item) use ($app) {
  76.             $item->expiresAfter($this->debug 300 3600);
  77.             return $this->doGetRoutes($app);
  78.         });
  79.     }
  80.     private function doGetRoutes(App $app): ?RouteCollection
  81.     {
  82.         if ((null === $url $app->getUrl()) || !filter_var($urlFILTER_VALIDATE_URL)) {
  83.             return null;
  84.         }
  85.         try {
  86.             $response $this->client->request('GET'$url 'routing/routes', [
  87.                 'timeout' => $this->debug 15 10,
  88.                 'headers' => [
  89.                     'Authorization' => 'Core ' $app->getSecret(),
  90.                 ],
  91.             ]);
  92.             return RouteCollection::createFromArray([
  93.                 'app_name' => $app->getQueueName(),
  94.                 'routes' => $response->toArray(),
  95.                 'host' => $app->getHost(),
  96.             ]);
  97.         } catch (ExceptionInterface) {
  98.             return null;
  99.         }
  100.     }
  101. }