Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.30% covered (success)
97.30%
36 / 37
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserSettingsService
97.30% covered (success)
97.30%
36 / 37
66.67% covered (warning)
66.67%
2 / 3
9
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
6
 getCurrentUserSettings
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
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) 2021 (original work) Open Assessment Technologies SA.
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\user\implementation;
24
25use oat\generis\model\data\Ontology;
26use oat\generis\model\GenerisRdf;
27use oat\oatbox\user\UserTimezoneServiceInterface;
28use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
29use oat\tao\model\user\UserSettingsInterface;
30use oat\tao\model\user\UserSettingsServiceInterface;
31use core_kernel_classes_Resource;
32use tao_models_classes_UserService;
33
34class UserSettingsService implements UserSettingsServiceInterface
35{
36    /** @var Ontology */
37    private $ontology;
38
39    /** @var string */
40    private $defaultTimeZone;
41
42    /** @var tao_models_classes_UserService */
43    private $userService;
44
45    /** @var FeatureFlagCheckerInterface */
46    private $featureFlagChecker;
47
48    public function __construct(
49        UserTimezoneServiceInterface $userTimezoneService,
50        Ontology $ontology,
51        tao_models_classes_UserService $userService,
52        FeatureFlagCheckerInterface $featureFlagChecker
53    ) {
54        $this->defaultTimeZone = $userTimezoneService->getDefaultTimezone();
55        $this->ontology = $ontology;
56        $this->userService = $userService;
57        $this->featureFlagChecker = $featureFlagChecker;
58    }
59
60    public function get(core_kernel_classes_Resource $user): UserSettingsInterface
61    {
62        $props = $user->getPropertiesValues(
63            [
64                $this->ontology->getProperty(GenerisRdf::PROPERTY_USER_UILG),
65                $this->ontology->getProperty(GenerisRdf::PROPERTY_USER_DEFLG),
66                $this->ontology->getProperty(GenerisRdf::PROPERTY_USER_TIMEZONE),
67                $this->ontology->getProperty(GenerisRdf::PROPERTY_USER_INTERFACE_MODE)
68            ]
69        );
70
71        if (!empty($props[GenerisRdf::PROPERTY_USER_UILG])) {
72            $uiLanguageCode = current($props[GenerisRdf::PROPERTY_USER_UILG])->getUri();
73        }
74
75        if (!empty($props[GenerisRdf::PROPERTY_USER_DEFLG])) {
76            $dataLanguageCode = current($props[GenerisRdf::PROPERTY_USER_DEFLG])->getUri();
77        }
78
79        if (!empty($props[GenerisRdf::PROPERTY_USER_TIMEZONE])) {
80            $timezone = (string) current($props[GenerisRdf::PROPERTY_USER_TIMEZONE]);
81        }
82
83        $userSettings = new UserSettings(
84            $timezone ?? $this->defaultTimeZone,
85            $uiLanguageCode ?? null,
86            $dataLanguageCode ?? null
87        );
88
89        if (
90            $this->featureFlagChecker->isEnabled(
91                FeatureFlagCheckerInterface::FEATURE_FLAG_SOLAR_DESIGN_ENABLED
92            )
93        ) {
94            $userSettings->setSetting(
95                UserSettingsInterface::INTERFACE_MODE,
96                empty($props[GenerisRdf::PROPERTY_USER_INTERFACE_MODE])
97                    ? GenerisRdf::PROPERTY_USER_INTERFACE_MODE_SIMPLE
98                    : current($props[GenerisRdf::PROPERTY_USER_INTERFACE_MODE])->getUri()
99            );
100        }
101
102        return $userSettings;
103    }
104
105    public function getCurrentUserSettings(): UserSettingsInterface
106    {
107        $currentUser = $this->userService->getCurrentUser();
108
109        if ($currentUser) {
110            return $this->get($currentUser);
111        }
112
113        return new UserSettings($this->defaultTimeZone);
114    }
115}