Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.41% |
62 / 63 |
|
90.91% |
10 / 11 |
CRAP | |
0.00% |
0 / 1 |
| FeatureFlagRepository | |
98.41% |
62 / 63 |
|
90.91% |
10 / 11 |
26 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| list | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| save | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| clearCache | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
5.02 | |||
| getList | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getListFromDb | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| getPersistenceName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| filterVar | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOverrideFeatureFlag | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasOverrideFeatureFlag | |
100.00% |
1 / 1 |
|
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 | * @author Gabriel Felipe Soares <gabriel.felipe.soares@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\model\featureFlag\Repository; |
| 26 | |
| 27 | use core_kernel_classes_Triple; |
| 28 | use InvalidArgumentException; |
| 29 | use oat\generis\model\data\Ontology; |
| 30 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 31 | use oat\tao\model\TaoOntology; |
| 32 | use Psr\SimpleCache\CacheInterface; |
| 33 | |
| 34 | class FeatureFlagRepository implements FeatureFlagRepositoryInterface |
| 35 | { |
| 36 | private const ONTOLOGY_SUBJECT = 'http://www.tao.lu/Ontologies/TAO.rdf#featureFlags'; |
| 37 | private const ONTOLOGY_PREDICATE = 'http://www.tao.lu/Ontologies/TAO.rdf#featureFlags'; |
| 38 | private const CACHE_LIST_KEY = 'FEATURE_FLAG_LIST'; |
| 39 | private const FEATURE_FLAG_PREFIX = 'FEATURE_FLAG_'; |
| 40 | |
| 41 | /** @var Ontology */ |
| 42 | private $ontology; |
| 43 | |
| 44 | /** @var CacheInterface */ |
| 45 | private $cache; |
| 46 | |
| 47 | /** @var array */ |
| 48 | private $storageOverride; |
| 49 | |
| 50 | public function __construct(Ontology $ontology, CacheInterface $cache, array $storageOverride = null) |
| 51 | { |
| 52 | $this->ontology = $ontology; |
| 53 | $this->cache = $cache; |
| 54 | $this->storageOverride = $storageOverride ?? $_ENV; |
| 55 | } |
| 56 | |
| 57 | public function get(string $featureFlagName): bool |
| 58 | { |
| 59 | if ($this->hasOverrideFeatureFlag($featureFlagName)) { |
| 60 | return $this->getOverrideFeatureFlag($featureFlagName); |
| 61 | } |
| 62 | |
| 63 | $featureFlagName = $this->getPersistenceName($featureFlagName); |
| 64 | |
| 65 | $value = $this->cache->get($featureFlagName); |
| 66 | if (null !== $value) { |
| 67 | return $this->filterVar($value); |
| 68 | } |
| 69 | |
| 70 | $resource = $this->ontology->getResource(self::ONTOLOGY_SUBJECT); |
| 71 | $value = (string)$resource->getOnePropertyValue($this->ontology->getProperty($featureFlagName)); |
| 72 | $value = $this->filterVar($value); |
| 73 | |
| 74 | $this->cache->set($featureFlagName, $value); |
| 75 | |
| 76 | return $value; |
| 77 | } |
| 78 | |
| 79 | public function list(): array |
| 80 | { |
| 81 | $output = $this->getList(); |
| 82 | |
| 83 | foreach ($this->storageOverride as $key => $value) { |
| 84 | if (strpos($key, self::FEATURE_FLAG_PREFIX) === 0) { |
| 85 | $output[$key] = $this->filterVar($this->storageOverride[$key]); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @deprecated Only here for legacy support purposes, we should rely on storage |
| 91 | */ |
| 92 | if (!array_key_exists(FeatureFlagCheckerInterface::FEATURE_FLAG_LISTS_DEPENDENCY_ENABLED, $output)) { |
| 93 | $output[FeatureFlagCheckerInterface::FEATURE_FLAG_LISTS_DEPENDENCY_ENABLED] = false; |
| 94 | } |
| 95 | |
| 96 | return $output; |
| 97 | } |
| 98 | |
| 99 | public function save(string $featureFlagName, bool $value): void |
| 100 | { |
| 101 | if (strpos($featureFlagName, self::FEATURE_FLAG_PREFIX) !== 0) { |
| 102 | throw new InvalidArgumentException( |
| 103 | sprintf( |
| 104 | 'FeatureFlag name needs to start with "%s"', |
| 105 | self::FEATURE_FLAG_PREFIX |
| 106 | ) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | $featureFlagName = $this->getPersistenceName($featureFlagName); |
| 111 | |
| 112 | $resource = $this->ontology->getResource(self::ONTOLOGY_SUBJECT); |
| 113 | $resource->editPropertyValues($this->ontology->getProperty($featureFlagName), var_export($value, true)); |
| 114 | |
| 115 | if ($this->cache->has($featureFlagName)) { |
| 116 | $this->cache->delete($featureFlagName); |
| 117 | } |
| 118 | |
| 119 | if ($this->cache->has(self::CACHE_LIST_KEY)) { |
| 120 | $this->cache->delete(self::CACHE_LIST_KEY); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public function clearCache(): int |
| 125 | { |
| 126 | $resource = $this->ontology->getResource(self::ONTOLOGY_SUBJECT); |
| 127 | |
| 128 | $count = 0; |
| 129 | |
| 130 | /** @var core_kernel_classes_Triple $triple */ |
| 131 | foreach ($resource->getRdfTriples() as $triple) { |
| 132 | if ($triple->predicate === TaoOntology::PROPERTY_UPDATED_AT) { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | if ($this->cache->has($triple->predicate)) { |
| 137 | $this->cache->delete($triple->predicate); |
| 138 | $count++; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if ($this->cache->has(self::CACHE_LIST_KEY)) { |
| 143 | $this->cache->delete(self::CACHE_LIST_KEY); |
| 144 | } |
| 145 | |
| 146 | return $count; |
| 147 | } |
| 148 | |
| 149 | private function getList(): array |
| 150 | { |
| 151 | $output = $this->cache->get(self::CACHE_LIST_KEY); |
| 152 | if (is_array($output)) { |
| 153 | return $output; |
| 154 | } |
| 155 | |
| 156 | $output = $this->getListFromDb(); |
| 157 | $this->cache->set(self::CACHE_LIST_KEY, $output); |
| 158 | |
| 159 | return $output; |
| 160 | } |
| 161 | |
| 162 | private function getListFromDb(): array |
| 163 | { |
| 164 | $output = []; |
| 165 | |
| 166 | $resource = $this->ontology->getResource(self::ONTOLOGY_SUBJECT); |
| 167 | |
| 168 | /** @var core_kernel_classes_Triple $triple */ |
| 169 | foreach ($resource->getRdfTriples() as $triple) { |
| 170 | if ($triple->predicate !== TaoOntology::PROPERTY_UPDATED_AT) { |
| 171 | $featureFlagName = str_replace(self::ONTOLOGY_PREDICATE . '_', '', $triple->predicate); |
| 172 | |
| 173 | $output[$featureFlagName] = $this->get($featureFlagName); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return $output; |
| 178 | } |
| 179 | |
| 180 | private function getPersistenceName(string $featureFlagName): string |
| 181 | { |
| 182 | return self::ONTOLOGY_PREDICATE . '_' . $featureFlagName; |
| 183 | } |
| 184 | |
| 185 | private function filterVar($value): bool |
| 186 | { |
| 187 | return filter_var($value, FILTER_VALIDATE_BOOLEAN) ?? false; |
| 188 | } |
| 189 | |
| 190 | private function getOverrideFeatureFlag(string $key): bool |
| 191 | { |
| 192 | return $this->filterVar($this->storageOverride[$key] ?? false); |
| 193 | } |
| 194 | |
| 195 | private function hasOverrideFeatureFlag(string $key): bool |
| 196 | { |
| 197 | return array_key_exists($key, $this->storageOverride); |
| 198 | } |
| 199 | } |