Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| FeatureVisibilityService | |
100.00% |
22 / 22 |
|
100.00% |
8 / 8 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| showFeature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hideFeature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setFeaturesVisibility | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| removeFeature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateFeatureVisibility | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| updateConfig | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| removeFeatureFromConfig | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\featureVisibility; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | use oat\oatbox\AbstractRegistry; |
| 27 | use oat\tao\model\ClientLibConfigRegistry; |
| 28 | |
| 29 | /** |
| 30 | * Please refer to `oat\tao\model\featureFlag\FeatureFlagConfigSwitcher` |
| 31 | * to change visibility or configuration based on feature flags |
| 32 | */ |
| 33 | class FeatureVisibilityService |
| 34 | { |
| 35 | public const HIDE_PARAM = 'hide'; |
| 36 | public const SHOW_PARAM = 'show'; |
| 37 | |
| 38 | private const GLOBAL_UI_CONFIG_NAME = 'services/features'; |
| 39 | |
| 40 | /** @var AbstractRegistry */ |
| 41 | private $registry; |
| 42 | |
| 43 | public function __construct(ClientLibConfigRegistry $registry) |
| 44 | { |
| 45 | $this->registry = $registry; |
| 46 | } |
| 47 | |
| 48 | public function showFeature(string $featureName): void |
| 49 | { |
| 50 | $this->updateFeatureVisibility('updateConfig', [$featureName => self::SHOW_PARAM]); |
| 51 | } |
| 52 | |
| 53 | public function hideFeature(string $featureName): void |
| 54 | { |
| 55 | $this->updateFeatureVisibility('updateConfig', [$featureName => self::HIDE_PARAM]); |
| 56 | } |
| 57 | |
| 58 | public function setFeaturesVisibility(array $featureNameVisibilityMap): void |
| 59 | { |
| 60 | $this->updateFeatureVisibility('updateConfig', $featureNameVisibilityMap); |
| 61 | } |
| 62 | |
| 63 | public function removeFeature(string $featureName): void |
| 64 | { |
| 65 | $this->updateFeatureVisibility('removeFeatureFromConfig', $featureName); |
| 66 | } |
| 67 | |
| 68 | private function updateFeatureVisibility(string $configUpdaterCallableName, $configUpdaterArg): void |
| 69 | { |
| 70 | $existingConfig = $this->registry->get(self::GLOBAL_UI_CONFIG_NAME); |
| 71 | if ($existingConfig === '') { |
| 72 | $existingConfig = []; |
| 73 | } |
| 74 | |
| 75 | $updatedConfig = $this->{$configUpdaterCallableName}($existingConfig, $configUpdaterArg); |
| 76 | |
| 77 | $this->registry->set(self::GLOBAL_UI_CONFIG_NAME, $updatedConfig); |
| 78 | } |
| 79 | |
| 80 | private function updateConfig(array $existingConfig, array $newStatuses): array |
| 81 | { |
| 82 | foreach ($newStatuses as $featureName => $featureValue) { |
| 83 | if (!in_array($featureValue, [self::HIDE_PARAM, self::SHOW_PARAM], true)) { |
| 84 | throw new InvalidArgumentException(sprintf( |
| 85 | 'Feature value should be either %s or %s, %s given', |
| 86 | self::SHOW_PARAM, |
| 87 | self::HIDE_PARAM, |
| 88 | $featureValue |
| 89 | )); |
| 90 | } |
| 91 | $existingConfig['visibility'][$featureName] = $featureValue; |
| 92 | } |
| 93 | |
| 94 | return $existingConfig; |
| 95 | } |
| 96 | |
| 97 | private function removeFeatureFromConfig(array $existingConfig, string $featureName): array |
| 98 | { |
| 99 | unset($existingConfig['visibility'][$featureName]); |
| 100 | |
| 101 | return $existingConfig; |
| 102 | } |
| 103 | } |