Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
51.35% |
19 / 37 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| SectionVisibilityFilter | |
51.35% |
19 / 37 |
|
33.33% |
2 / 6 |
28.58 | |
0.00% |
0 / 1 |
| isVisible | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
7.01 | |||
| createSectionPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hideSectionByFeatureFlag | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| showSectionByFeatureFlag | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| getFeatureFlagChecker | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUserSettingsService | |
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) 2020 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\menu; |
| 24 | |
| 25 | use oat\generis\model\GenerisRdf; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
| 28 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 29 | use oat\tao\model\user\implementation\UserSettingsService; |
| 30 | use oat\tao\model\user\UserSettingsInterface; |
| 31 | use oat\tao\model\user\UserSettingsServiceInterface; |
| 32 | |
| 33 | class SectionVisibilityFilter extends ConfigurableService implements SectionVisibilityFilterInterface |
| 34 | { |
| 35 | private const SECTION_PATH_SEPARATOR = '/'; |
| 36 | public const SERVICE_ID = 'tao/SectionVisibilityFilter'; |
| 37 | public const OPTION_FEATURE_FLAG_SECTIONS = 'featureFlagSections'; |
| 38 | public const OPTION_FEATURE_FLAG_SECTIONS_TO_HIDE = 'featureFlagSectionsToHide'; |
| 39 | private const DEFAULT_FEATURE_FLAG_SECTIONS_TO_HIDE = [ |
| 40 | 'settings_my_password' => [ |
| 41 | FeatureFlagCheckerInterface::FEATURE_FLAG_SOLAR_DESIGN_ENABLED |
| 42 | ] |
| 43 | ]; |
| 44 | |
| 45 | public const SIMPLE_INTERFACE_MODE_HIDDEN_SECTIONS = []; |
| 46 | |
| 47 | public function isVisible(string $sectionPath): bool |
| 48 | { |
| 49 | $sections = $this->getOption(self::OPTION_FEATURE_FLAG_SECTIONS, []); |
| 50 | $sectionToHide = array_merge_recursive( |
| 51 | $this->getOption(self::OPTION_FEATURE_FLAG_SECTIONS_TO_HIDE, []), |
| 52 | self::DEFAULT_FEATURE_FLAG_SECTIONS_TO_HIDE |
| 53 | ); |
| 54 | |
| 55 | foreach ($sectionToHide[$sectionPath] ?? [] as $featureFlag) { |
| 56 | if ($this->getFeatureFlagChecker()->isEnabled($featureFlag)) { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | foreach ($sections[$sectionPath] ?? [] as $featureFlag) { |
| 62 | if (!$this->getFeatureFlagChecker()->isEnabled($featureFlag)) { |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $userSettings = $this->getUserSettingsService()->getCurrentUserSettings(); |
| 68 | |
| 69 | if ( |
| 70 | $userSettings->getSetting( |
| 71 | UserSettingsInterface::INTERFACE_MODE |
| 72 | ) === GenerisRdf::PROPERTY_USER_INTERFACE_MODE_SIMPLE |
| 73 | && in_array($sectionPath, self::SIMPLE_INTERFACE_MODE_HIDDEN_SECTIONS, true) |
| 74 | ) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | public function createSectionPath(array $segments): string |
| 82 | { |
| 83 | return implode(self::SECTION_PATH_SEPARATOR, $segments); |
| 84 | } |
| 85 | |
| 86 | public function hideSectionByFeatureFlag(string $sectionPath, string $featureFlag): void |
| 87 | { |
| 88 | $options = $this->getOption(SectionVisibilityFilter::OPTION_FEATURE_FLAG_SECTIONS_TO_HIDE, []); |
| 89 | $options[$sectionPath] = array_merge( |
| 90 | $options[$sectionPath] ?? [], |
| 91 | [ |
| 92 | $featureFlag |
| 93 | ] |
| 94 | ); |
| 95 | $this->setOption(SectionVisibilityFilter::OPTION_FEATURE_FLAG_SECTIONS_TO_HIDE, $options); |
| 96 | } |
| 97 | |
| 98 | public function showSectionByFeatureFlag(string $sectionPath, string $featureFlag): void |
| 99 | { |
| 100 | $options = $this->getOption(SectionVisibilityFilter::OPTION_FEATURE_FLAG_SECTIONS, []); |
| 101 | $options[$sectionPath] = array_merge( |
| 102 | $options[$sectionPath] ?? [], |
| 103 | [ |
| 104 | $featureFlag |
| 105 | ] |
| 106 | ); |
| 107 | $this->setOption(SectionVisibilityFilter::OPTION_FEATURE_FLAG_SECTIONS, $options); |
| 108 | } |
| 109 | |
| 110 | private function getFeatureFlagChecker(): FeatureFlagCheckerInterface |
| 111 | { |
| 112 | return $this->getServiceManager()->getContainer()->get(FeatureFlagChecker::class); |
| 113 | } |
| 114 | |
| 115 | private function getUserSettingsService(): UserSettingsServiceInterface |
| 116 | { |
| 117 | return $this->getServiceManager()->getContainer()->get(UserSettingsService::class); |
| 118 | } |
| 119 | } |