vendor/pimcore/openid-connect/src/Service/ConfigurationService.php line 68

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\OpenIdConnectBundle\Service;
  12. use Pimcore\Config\LocationAwareConfigRepository;
  13. class ConfigurationService
  14. {
  15.     const CONFIG_ID 'pimcore_open_id_connect';
  16.     /**
  17.      * @var null|string
  18.      */
  19.     protected ?string $defaultProvider;
  20.     /**
  21.      * @var array
  22.      */
  23.     protected array $providers;
  24.     /**
  25.      * @var bool
  26.      */
  27.     protected bool $isWriteable;
  28.     /**
  29.      * @var bool
  30.      */
  31.     protected bool $configLoaded false;
  32.     /**
  33.      * @var LocationAwareConfigRepository
  34.      */
  35.     protected $locationAwareConfigRepository;
  36.     public function __construct(array $config)
  37.     {
  38.         $containerConfig = [];
  39.         if (!empty($config['default_provider'])) {
  40.             $containerConfig[self::CONFIG_ID]['default_provider'] = $config['default_provider'];
  41.         }
  42.         if (!empty($config['providers'])) {
  43.             $containerConfig[self::CONFIG_ID]['providers'] = $config['providers'];
  44.         }
  45.         if (\Pimcore\Version::getMajorVersion() >= 11) {
  46.             $storageConfig $config['config_location']['open_id_connect'];
  47.             $this->locationAwareConfigRepository = new LocationAwareConfigRepository(
  48.                 $containerConfig,
  49.                 'pimcore_oidc',
  50.                 $storageConfig
  51.             );
  52.         } else {
  53.             $storageConfig LocationAwareConfigRepository::getStorageConfigurationCompatibilityLayer(
  54.                 $config,
  55.                 'open_id_connect',
  56.                 'PIMCORE_CONFIG_STORAGE_DIR_OIDC',
  57.                 'PIMCORE_WRITE_TARGET_OIDC'
  58.             );
  59.             $this->locationAwareConfigRepository = new LocationAwareConfigRepository(
  60.                 $containerConfig,
  61.                 'pimcore_oidc',
  62.                 $storageConfig,
  63.                 null
  64.             );
  65.         }
  66.     }
  67.     protected function loadConfig()
  68.     {
  69.         if ($this->configLoaded === false) {
  70.             list($config$dataSource) = $this->locationAwareConfigRepository->loadConfigByKey(self::CONFIG_ID);
  71.             $this->defaultProvider $config['default_provider'] ?? null;
  72.             $this->providers $config['providers'] ?? [];
  73.             $this->isWriteable $this->locationAwareConfigRepository->isWriteable(self::CONFIG_ID$dataSource);
  74.             $this->configLoaded true;
  75.         }
  76.     }
  77.     /**
  78.      * @return string|null
  79.      */
  80.     public function getDefaultProvider(): ?string
  81.     {
  82.         $this->loadConfig();
  83.         return $this->defaultProvider;
  84.     }
  85.     /**
  86.      * @return array
  87.      */
  88.     public function getProviders(): array
  89.     {
  90.         $this->loadConfig();
  91.         return array_keys($this->providers);
  92.     }
  93.     /**
  94.      * @param string $providerName
  95.      *
  96.      * @return array|null
  97.      */
  98.     public function getProviderConfig(string $providerName): ?array
  99.     {
  100.         $this->loadConfig();
  101.         return $this->providers[$providerName] ?? null;
  102.     }
  103.     /**
  104.      * @param array $providers
  105.      * @param string|null $defaultProvider
  106.      *
  107.      * @throws \Exception
  108.      */
  109.     public function saveConfiguration(array $providers, ?string $defaultProvider)
  110.     {
  111.         $data = [
  112.             'default_provider' => $defaultProvider,
  113.             'providers' => $providers
  114.         ];
  115.         $this->locationAwareConfigRepository->saveConfig(self::CONFIG_ID$data, function ($key$data) {
  116.             return [
  117.                 $key => $data
  118.             ];
  119.         });
  120.         $this->configLoaded false;
  121.     }
  122.     /**
  123.      * @return array
  124.      */
  125.     public function getFullConfiguration(): array
  126.     {
  127.         $this->loadConfig();
  128.         return [
  129.             'default_provider' => $this->defaultProvider,
  130.             'providers' => $this->providers
  131.         ];
  132.     }
  133.     /**
  134.      * @return bool
  135.      */
  136.     public function isWriteable(): bool
  137.     {
  138.         $this->loadConfig();
  139.         return $this->isWriteable;
  140.     }
  141. }