| Total Complexity | 12 |
| Total Lines | 135 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 36 | class ThemesService { |
||
| 37 | private IUserSession $userSession; |
||
| 38 | private IConfig $config; |
||
| 39 | |||
| 40 | /** @var ITheme[] */ |
||
| 41 | private array $themesProviders; |
||
| 42 | |||
| 43 | public function __construct(IUserSession $userSession, |
||
| 44 | IConfig $config, |
||
| 45 | DefaultTheme $defaultTheme, |
||
| 46 | DarkTheme $darkTheme, |
||
| 47 | HighContrastTheme $highContrastTheme, |
||
| 48 | DarkHighContrastTheme $darkHighContrastTheme, |
||
| 49 | DyslexiaFont $dyslexiaFont) { |
||
| 50 | $this->userSession = $userSession; |
||
| 51 | $this->config = $config; |
||
| 52 | |||
| 53 | // Register themes |
||
| 54 | $this->themesProviders = [ |
||
| 55 | $defaultTheme->getId() => $defaultTheme, |
||
| 56 | $darkTheme->getId() => $darkTheme, |
||
| 57 | $highContrastTheme->getId() => $highContrastTheme, |
||
| 58 | $darkHighContrastTheme->getId() => $darkHighContrastTheme, |
||
| 59 | $dyslexiaFont->getId() => $dyslexiaFont, |
||
| 60 | ]; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the list of all registered themes |
||
| 65 | * |
||
| 66 | * @return ITheme[] |
||
| 67 | */ |
||
| 68 | public function getThemes(): array { |
||
| 69 | return $this->themesProviders; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Enable a theme for the logged-in user |
||
| 74 | * |
||
| 75 | * @param ITheme $theme the theme to enable |
||
| 76 | * @return string[] the enabled themes |
||
| 77 | */ |
||
| 78 | public function enableTheme(ITheme $theme): array { |
||
| 79 | $themesIds = $this->getEnabledThemes(); |
||
| 80 | |||
| 81 | // If already enabled, ignore |
||
| 82 | if (in_array($theme->getId(), $themesIds)) { |
||
| 83 | return $themesIds; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** @var ITheme[] */ |
||
| 87 | $themes = array_map(function($themeId) { |
||
| 88 | return $this->getThemes()[$themeId]; |
||
| 89 | }, $themesIds); |
||
| 90 | |||
| 91 | // Filtering all themes with the same type |
||
| 92 | $filteredThemes = array_filter($themes, function(ITheme $t) use ($theme) { |
||
| 93 | return $theme->getType() === $t->getType(); |
||
| 94 | }); |
||
| 95 | |||
| 96 | // Retrieve IDs only |
||
| 97 | /** @var string[] */ |
||
| 98 | $filteredThemesIds = array_map(function(ITheme $t) { |
||
| 99 | return $t->getId(); |
||
| 100 | }, array_values($filteredThemes)); |
||
| 101 | |||
| 102 | $enabledThemes = array_merge(array_diff($themesIds, $filteredThemesIds), [$theme->getId()]); |
||
| 103 | $this->setEnabledThemes($enabledThemes); |
||
| 104 | |||
| 105 | return $enabledThemes; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Disable a theme for the logged-in user |
||
| 110 | * |
||
| 111 | * @param ITheme $theme the theme to disable |
||
| 112 | * @return string[] the enabled themes |
||
| 113 | */ |
||
| 114 | public function disableTheme(ITheme $theme): array { |
||
| 115 | $themesIds = $this->getEnabledThemes(); |
||
| 116 | |||
| 117 | // If enabled, removing it |
||
| 118 | if (in_array($theme->getId(), $themesIds)) { |
||
| 119 | $enabledThemes = array_diff($themesIds, [$theme->getId()]); |
||
| 120 | $this->setEnabledThemes($enabledThemes); |
||
| 121 | return $enabledThemes; |
||
| 122 | } |
||
| 123 | |||
| 124 | return $themesIds; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Check whether a theme is enabled or not |
||
| 129 | * for the logged-in user |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function isEnabled(ITheme $theme): bool { |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the list of all enabled themes IDs |
||
| 145 | * for the logged-in user |
||
| 146 | * |
||
| 147 | * @return string[] |
||
| 148 | */ |
||
| 149 | public function getEnabledThemes(): array { |
||
| 150 | $user = $this->userSession->getUser(); |
||
| 151 | if ($user === null) { |
||
| 152 | return []; |
||
| 153 | } |
||
| 154 | |||
| 155 | try { |
||
| 156 | return json_decode($this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]')); |
||
| 157 | } catch (\Exception $e) { |
||
| 158 | return []; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set the list of enabled themes |
||
| 164 | * for the logged-in user |
||
| 165 | * |
||
| 166 | * @param string[] $themes the list of enabled themes IDs |
||
| 167 | */ |
||
| 168 | private function setEnabledThemes(array $themes): void { |
||
| 171 | } |
||
| 172 | } |
||
| 173 |