Completed
Push — master ( 30ccda...71f35d )
by Tarmo
01:20 queued 28s
created

Localization::getTimezones()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
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\Doctrine\DBAL\Types\EnumLanguageType;
12
use App\Doctrine\DBAL\Types\EnumLocaleType;
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_LANGUAGE = EnumLanguageType::LANGUAGE_EN;
33
    public const DEFAULT_LOCALE = EnumLocaleType::LOCALE_EN;
34
    public const DEFAULT_TIMEZONE = 'Europe/Helsinki';
35
36 51
    public function __construct(
37
        private CacheInterface $appCacheApcu,
38
        private LoggerInterface $logger,
39
    ) {
40
    }
41
42
    /**
43
     * @return array<int, string>
44
     */
45 45
    public function getLanguages(): array
46
    {
47 45
        return EnumLanguageType::getValues();
48
    }
49
50
    /**
51
     * @return array<int, string>
52
     */
53 45
    public function getLocales(): array
54
    {
55 45
        return EnumLocaleType::getValues();
56
    }
57
58
    /**
59
     * @return array<int, array{timezone: string, identifier: string,  offset: string, value: string}>
60
     *
61
     * @noinspection PhpDocMissingThrowsInspection
62
     */
63 47
    public function getTimezones(): array
64
    {
65 47
        $output = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
66
67
        try {
68
            /** @noinspection PhpUnhandledExceptionInspection */
69 47
            $output = $this->appCacheApcu->get('application_timezone', $this->getClosure());
70 1
        } catch (Throwable $exception) {
71 1
            $this->logger->error($exception->getMessage(), $exception->getTrace());
72
        }
73
74 47
        return $output;
75
    }
76
77
    /**
78
     * @noinspection PhpDocMissingThrowsInspection
79
     *
80
     * @return array<int, array{timezone: string, identifier: string,  offset: string, value: string}>
81
     */
82 46
    public function getFormattedTimezones(): array
83
    {
84 46
        $output = [];
85
86 46
        $identifiers = DateTimeZone::listIdentifiers();
87
88 46
        foreach ($identifiers as $identifier) {
89 46
            $dateTimeZone = new DateTimeZone($identifier);
90
91
            /** @noinspection PhpUnhandledExceptionInspection */
92 46
            $dateTime = new DateTimeImmutable(timezone: $dateTimeZone);
93
94 46
            $hours = floor($dateTimeZone->getOffset($dateTime) / 3600);
95 46
            $minutes = floor(($dateTimeZone->getOffset($dateTime) - ($hours * 3600)) / 60);
96
97 46
            $hours = 'GMT' . ($hours < 0 ? $hours : '+' . $hours);
98 46
            $minutes = ($minutes > 0 ? $minutes : '0' . $minutes);
99
100 46
            $output[] = [
101 46
                'timezone' => explode('/', $identifier)[0],
102
                'identifier' => $identifier,
103 46
                'offset' => $hours . ':' . $minutes,
104 46
                'value' => str_replace('_', ' ', $identifier),
105
            ];
106
        }
107
108 46
        return $output;
109
    }
110
111 47
    private function getClosure(): Closure
112
    {
113 47
        return function (ItemInterface $item): array {
114
            // One year
115 46
            $item->expiresAfter(31536000);
116
117 46
            return $this->getFormattedTimezones();
118
        };
119
    }
120
}
121