Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
CachedListDecorator | |
0.00% |
0 / 24 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
getImplementation | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
getList | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
getCache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
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 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoClientDiagnostic\model\SupportedList; |
24 | |
25 | use common_exception_NoImplementation; |
26 | use common_exception_NotImplemented; |
27 | use DateInterval; |
28 | use oat\oatbox\cache\SimpleCache; |
29 | use oat\oatbox\service\ConfigurableService; |
30 | use oat\oatbox\service\ServiceManager; |
31 | |
32 | class CachedListDecorator extends ConfigurableService implements SupportedListInterface |
33 | { |
34 | /** @var string SupportedListInterface */ |
35 | public const OPTION_ORIGINAL_IMPLEMENTATION = 'OPTION_ORIGINAL_IMPLEMENTATION'; |
36 | |
37 | /** @var string value should be in seconds */ |
38 | public const OPTION_TTL_CACHE = 'OPTION_TTL_CACHE'; |
39 | |
40 | /** @var int seconds */ |
41 | private const DEFAULT_TTL_CACHE = 3600; |
42 | |
43 | /** @var string */ |
44 | private const CACHE_KEY = 'supported_browser_list'; |
45 | |
46 | /** @var SupportedListInterface */ |
47 | private $implementation; |
48 | |
49 | private function getImplementation(): SupportedListInterface |
50 | { |
51 | if ($this->implementation === null) { |
52 | $implementation = $this->getOption(self::OPTION_ORIGINAL_IMPLEMENTATION); |
53 | |
54 | if (!$implementation instanceof SupportedListInterface) { |
55 | throw new common_exception_NoImplementation(sprintf( |
56 | 'Implementation for %s should be of class SupportedListInterface', |
57 | __CLASS__ |
58 | )); |
59 | } |
60 | |
61 | if ($implementation instanceof self) { |
62 | throw new common_exception_NoImplementation( |
63 | 'CachedListDecorator can\'t be set as implementation for itself ' . __CLASS__ |
64 | ); |
65 | } |
66 | |
67 | $this->getServiceLocator()->propagate($implementation); |
68 | $this->implementation = $implementation; |
69 | } |
70 | return $this->implementation; |
71 | } |
72 | |
73 | public function getList(): ?array |
74 | { |
75 | if ($this->getCache()->has(self::CACHE_KEY)) { |
76 | return $this->getCache()->get(self::CACHE_KEY); |
77 | } |
78 | $list = $this->getImplementation()->getList(); |
79 | |
80 | $ttl = self::DEFAULT_TTL_CACHE; |
81 | if ($this->hasOption(self::OPTION_TTL_CACHE)) { |
82 | $ttl = (int)$this->getOption(self::OPTION_TTL_CACHE); |
83 | } |
84 | |
85 | try { |
86 | $this->getCache()->set(self::CACHE_KEY, $list, new DateInterval('PT' . $ttl . 'S')); |
87 | } catch (common_exception_NotImplemented $e) { |
88 | // for those implementation that don't support TTL we don't want cache |
89 | } |
90 | return $list; |
91 | } |
92 | |
93 | /** |
94 | * @return SimpleCache |
95 | * @throws \oat\oatbox\service\ServiceNotFoundException |
96 | */ |
97 | public function getCache() |
98 | { |
99 | return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID); |
100 | } |
101 | } |