Issues (105)

src/Service/Localization.php (1 issue)

Labels
Severity
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
    final public const DEFAULT_TIMEZONE = 'Europe/Helsinki';
33
34 51
    public function __construct(
35
        private readonly CacheInterface $appCacheApcu,
36
        private readonly LoggerInterface $logger,
37
    ) {
38 51
    }
39
40
    /**
41
     * @return array<int, string>
42
     */
43 2
    public function getLanguages(): array
44
    {
45 2
        return Language::getValues();
46
    }
47
48
    /**
49
     * @return array<int, string>
50
     */
51 2
    public function getLocales(): array
52
    {
53 2
        return Locale::getValues();
54
    }
55
56
    /**
57
     * @return array<int, array{timezone: string, identifier: string,  offset: string, value: string}>
58
     */
59 47
    public function getTimezones(): array
60
    {
61 47
        $output = [];
62
63
        try {
64
            /** @var array<int, array{timezone: string, identifier: string, offset: string, value: string}> $output */
65 47
            $output = $this->appCacheApcu->get('application_timezone', $this->getClosure());
66 1
        } catch (Throwable $exception) {
67 1
            $this->logger->error($exception->getMessage(), $exception->getTrace());
68
        }
69
70 47
        return $output;
71
    }
72
73
    /**
74
     * @throws Throwable
75
     *
76
     * @return array<int, array{timezone: string, identifier: non-empty-string,  offset: string, value: string}>
77
     */
78 46
    public function getFormattedTimezones(): array
79
    {
80 46
        $output = [];
81
82
        /** @var array<int, non-empty-string> $identifiers */
83 46
        $identifiers = DateTimeZone::listIdentifiers();
84
85 46
        foreach ($identifiers as $identifier) {
86 46
            $dateTimeZone = new DateTimeZone($identifier);
87
88 46
            $dateTime = new DateTimeImmutable(timezone: $dateTimeZone);
89
90 46
            $hours = floor($dateTimeZone->getOffset($dateTime) / 3600);
91 46
            $minutes = floor(($dateTimeZone->getOffset($dateTime) - ($hours * 3600)) / 60);
92
93 46
            $hours = 'GMT' . ($hours < 0 ? $hours : '+' . $hours);
94 46
            $minutes = ($minutes > 0 ? $minutes : '0' . $minutes);
95
96 46
            $output[] = [
97 46
                'timezone' => explode('/', $identifier)[0],
98 46
                'identifier' => $identifier,
99 46
                'offset' => $hours . ':' . $minutes,
100 46
                'value' => str_replace('_', ' ', $identifier),
101 46
            ];
102
        }
103
104 46
        return $output;
105
    }
106
107 47
    private function getClosure(): Closure
108
    {
109 47
        return function (ItemInterface $item): array {
110
            // One year
111 46
            $item->expiresAfter(31_536_000);
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting ',' or ')' on line 111 at column 34
Loading history...
112
113 46
            return $this->getFormattedTimezones();
114 47
        };
115
    }
116
}
117