Passed
Pull Request — master (#1620)
by Tarmo
63:23
created

Localization::getLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
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\Enum\Language;
0 ignored issues
show
Bug introduced by
The type App\Enum\Language was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use App\Enum\Locale;
0 ignored issues
show
Bug introduced by
The type App\Enum\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
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