|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Matomo - free/libre analytics platform |
| 5 | + * |
| 6 | + * @link https://matomo.org |
| 7 | + * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Piwik\Plugins\FeatureFlags\Storage; |
| 11 | + |
| 12 | +use Piwik\Plugins\FeatureFlags\FeatureFlagInterface; |
| 13 | +use Piwik\Plugins\FeatureFlags\FeatureFlagStorageInterface; |
| 14 | + |
| 15 | +class CookieFeatureFlagStorage implements FeatureFlagStorageInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @internal |
| 19 | + * @param FeatureFlagInterface $feature |
| 20 | + * @return bool|null |
| 21 | + */ |
| 22 | + public function isFeatureActive(FeatureFlagInterface $feature): ?bool |
| 23 | + { |
| 24 | + if (!$feature->allowsCookieOverwrite()) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + $cookieName = $this->getCookieNameForFeature($feature->getName()); |
| 29 | + |
| 30 | + if (!isset($_COOKIE[$cookieName])) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + return $_COOKIE[$cookieName] == '1'; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @internal |
| 39 | + * @param FeatureFlagInterface $feature |
| 40 | + * @return void |
| 41 | + */ |
| 42 | + public function disableFeatureFlag(FeatureFlagInterface $feature): void |
| 43 | + { |
| 44 | + // do nothing, as cookie values should only be set in frontend |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @internal |
| 49 | + * @param FeatureFlagInterface $feature |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function enableFeatureFlag(FeatureFlagInterface $feature): void |
| 53 | + { |
| 54 | + // do nothing as cookie values should only be set in frontend |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @internal |
| 59 | + * @param string $feature |
| 60 | + * @return void |
| 61 | + */ |
| 62 | + public function deleteFeatureFlag(string $featureName): void |
| 63 | + { |
| 64 | + // do nothing as cookie values should only be set in frontend |
| 65 | + } |
| 66 | + |
| 67 | + private function getCookieNameForFeature(string $featureName): string |
| 68 | + { |
| 69 | + return 'feature_' . $featureName; |
| 70 | + } |
| 71 | +} |
0 commit comments