|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Service/Localization.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Service; |
|
10
|
|
|
|
|
11
|
|
|
use App\Enum\Language; |
|
|
|
|
|
|
12
|
|
|
use App\Enum\Locale; |
|
|
|
|
|
|
13
|
|
|
use Closure; |
|
14
|
|
|
use DateTimeImmutable; |
|
15
|
|
|
use DateTimeZone; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
|
18
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
|
19
|
|
|
use Throwable; |
|
20
|
|
|
use function explode; |
|
21
|
|
|
use function floor; |
|
22
|
|
|
use function str_replace; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class Localization |
|
26
|
|
|
* |
|
27
|
|
|
* @package App\Service |
|
28
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Localization |
|
31
|
|
|
{ |
|
32
|
|
|
public const DEFAULT_TIMEZONE = 'Europe/Helsinki'; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct( |
|
35
|
|
|
private CacheInterface $appCacheApcu, |
|
36
|
51 |
|
private LoggerInterface $logger, |
|
37
|
|
|
) { |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return array<int, Language> |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getLanguages(): array |
|
44
|
|
|
{ |
|
45
|
45 |
|
return Language::cases(); |
|
46
|
|
|
} |
|
47
|
45 |
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array<int, string> |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getLanguageValues(): array |
|
52
|
|
|
{ |
|
53
|
45 |
|
return Language::getValues(); |
|
54
|
|
|
} |
|
55
|
45 |
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array<int, Locale> |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getLocales(): array |
|
60
|
|
|
{ |
|
61
|
47 |
|
return Locale::cases(); |
|
62
|
|
|
} |
|
63
|
47 |
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return array<int, string> |
|
66
|
47 |
|
*/ |
|
67
|
1 |
|
public function getLocaleValues(): array |
|
68
|
1 |
|
{ |
|
69
|
|
|
return Locale::getValues(); |
|
70
|
|
|
} |
|
71
|
47 |
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return array<int, array{timezone: string, identifier: string, offset: string, value: string}> |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getTimezones(): array |
|
76
|
|
|
{ |
|
77
|
|
|
$output = []; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
46 |
|
try { |
|
80
|
|
|
$output = $this->appCacheApcu->get('application_timezone', $this->getClosure()); |
|
81
|
46 |
|
} catch (Throwable $exception) { |
|
82
|
|
|
$this->logger->error($exception->getMessage(), $exception->getTrace()); |
|
83
|
46 |
|
} |
|
84
|
|
|
|
|
85
|
46 |
|
return $output; |
|
86
|
46 |
|
} |
|
87
|
|
|
|
|
88
|
46 |
|
/** |
|
89
|
|
|
* @throws Throwable |
|
90
|
46 |
|
* |
|
91
|
46 |
|
* @return array<int, array{timezone: string, identifier: string, offset: string, value: string}> |
|
92
|
|
|
*/ |
|
93
|
46 |
|
public function getFormattedTimezones(): array |
|
94
|
46 |
|
{ |
|
95
|
|
|
$output = []; |
|
96
|
46 |
|
|
|
97
|
46 |
|
$identifiers = DateTimeZone::listIdentifiers(); |
|
98
|
|
|
|
|
99
|
46 |
|
foreach ($identifiers as $identifier) { |
|
100
|
46 |
|
$dateTimeZone = new DateTimeZone($identifier); |
|
101
|
|
|
|
|
102
|
|
|
$dateTime = new DateTimeImmutable(timezone: $dateTimeZone); |
|
103
|
|
|
|
|
104
|
46 |
|
$hours = floor($dateTimeZone->getOffset($dateTime) / 3600); |
|
105
|
|
|
$minutes = floor(($dateTimeZone->getOffset($dateTime) - ($hours * 3600)) / 60); |
|
106
|
|
|
|
|
107
|
47 |
|
$hours = 'GMT' . ($hours < 0 ? $hours : '+' . $hours); |
|
108
|
|
|
$minutes = ($minutes > 0 ? $minutes : '0' . $minutes); |
|
109
|
47 |
|
|
|
110
|
|
|
$output[] = [ |
|
111
|
46 |
|
'timezone' => explode('/', $identifier)[0], |
|
112
|
|
|
'identifier' => $identifier, |
|
113
|
46 |
|
'offset' => $hours . ':' . $minutes, |
|
114
|
|
|
'value' => str_replace('_', ' ', $identifier), |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $output; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function getClosure(): Closure |
|
122
|
|
|
{ |
|
123
|
|
|
return function (ItemInterface $item): array { |
|
124
|
|
|
// One year |
|
125
|
|
|
$item->expiresAfter(31536000); |
|
126
|
|
|
|
|
127
|
|
|
return $this->getFormattedTimezones(); |
|
128
|
|
|
}; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths