Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.00% |
13 / 20 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| FeatureFlagConfigSwitcher | |
65.00% |
13 / 20 |
|
57.14% |
4 / 7 |
16.19 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getSwitchedClientConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSwitchedExtensionConfig | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| addClientConfigHandler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addExtensionConfigHandler | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
28.57% |
2 / 7 |
|
0.00% |
0 / 1 |
9.83 | |||
| getExtensionConfigHandlers | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2022 (original work) Open Assessment Technologies SA. |
| 19 | * |
| 20 | * @author Gabriel Felipe Soares <gabriel.felipe.soares@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\model\featureFlag; |
| 26 | |
| 27 | use common_ext_ExtensionsManager; |
| 28 | use oat\oatbox\AbstractRegistry; |
| 29 | use oat\tao\model\ClientLibConfigRegistry; |
| 30 | use Psr\Container\ContainerInterface; |
| 31 | |
| 32 | class FeatureFlagConfigSwitcher |
| 33 | { |
| 34 | /** @var AbstractRegistry */ |
| 35 | private $registry; |
| 36 | |
| 37 | /** @var ContainerInterface */ |
| 38 | private $container; |
| 39 | |
| 40 | /** @var common_ext_ExtensionsManager */ |
| 41 | private $extensionsManager; |
| 42 | |
| 43 | /** @var string[] */ |
| 44 | private $clientConfigHandlers = []; |
| 45 | |
| 46 | /** @var string[][] */ |
| 47 | private $extensionConfigHandlers = []; |
| 48 | |
| 49 | public function __construct( |
| 50 | ClientLibConfigRegistry $registry, |
| 51 | common_ext_ExtensionsManager $extensionsManager, |
| 52 | ContainerInterface $container |
| 53 | ) { |
| 54 | $this->registry = $registry; |
| 55 | $this->container = $container; |
| 56 | $this->extensionsManager = $extensionsManager; |
| 57 | } |
| 58 | |
| 59 | public function getSwitchedClientConfig(): array |
| 60 | { |
| 61 | return $this->handle($this->clientConfigHandlers, $this->registry->getMap()); |
| 62 | } |
| 63 | |
| 64 | public function getSwitchedExtensionConfig(string $extension, string $configName): array |
| 65 | { |
| 66 | $configs = $this->extensionsManager->getExtensionById($extension)->getConfig($configName); |
| 67 | |
| 68 | if (!$configs) { |
| 69 | return []; |
| 70 | } |
| 71 | |
| 72 | return $this->handle($this->getExtensionConfigHandlers($extension, $configName), $configs); |
| 73 | } |
| 74 | |
| 75 | public function addClientConfigHandler(string $handler): void |
| 76 | { |
| 77 | $this->clientConfigHandlers[$handler] = $handler; |
| 78 | } |
| 79 | |
| 80 | public function addExtensionConfigHandler(string $extension, string $configName, string $handler): void |
| 81 | { |
| 82 | $key = $extension . '_' . $configName; |
| 83 | |
| 84 | $this->extensionConfigHandlers[$key] = $this->extensionConfigHandlers[$key] ?? []; |
| 85 | $this->extensionConfigHandlers[$key][$handler] = $handler; |
| 86 | } |
| 87 | |
| 88 | private function handle(array $handlers, array $config): array |
| 89 | { |
| 90 | foreach ($handlers as $handlerId) { |
| 91 | if (!$this->container->has($handlerId)) { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | /** @var FeatureFlagConfigHandlerInterface $handler */ |
| 96 | $handler = $this->container->get($handlerId); |
| 97 | |
| 98 | if ($handler instanceof FeatureFlagConfigHandlerInterface) { |
| 99 | $config = $handler($config); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return $config; |
| 104 | } |
| 105 | |
| 106 | private function getExtensionConfigHandlers(string $extension, string $configName): array |
| 107 | { |
| 108 | return $this->extensionConfigHandlers[$extension . '_' . $configName] ?? []; |
| 109 | } |
| 110 | } |