• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / docker-symfony-api / #138

24 Feb 2025 09:36PM UTC coverage: 43.903% (-0.02%) from 43.927%
#138

push

DKravtsov
Updated composer dependencies, small refactoring.

0 of 2 new or added lines in 1 file covered. (0.0%)

1631 of 3715 relevant lines covered (43.9%)

18.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/Tool/Application/Service/LocalizationService.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\Tool\Application\Service;
6

7
use App\General\Domain\Enum\Language;
8
use App\General\Domain\Enum\Locale;
9
use App\Tool\Domain\Service\Interfaces\LocalizationServiceInterface;
10
use Closure;
11
use DateTimeImmutable;
12
use DateTimeZone;
13
use Psr\Log\LoggerInterface;
14
use Symfony\Component\HttpFoundation\RequestStack;
15
use Symfony\Contracts\Cache\CacheInterface;
16
use Symfony\Contracts\Cache\ItemInterface;
17
use Throwable;
18

19
use function explode;
20
use function floor;
21
use function in_array;
22
use function str_replace;
23

24
/**
25
 * @package App\Tool
26
 */
27
readonly class LocalizationService implements LocalizationServiceInterface
28
{
29
    public function __construct(
30
        private CacheInterface $appCache,
31
        private LoggerInterface $logger,
32
        private RequestStack $requestStack,
33
    ) {
34
    }
6✔
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getLanguages(): array
40
    {
41
        return Language::getValues();
1✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getLocales(): array
48
    {
49
        return Locale::getValues();
1✔
50
    }
51

52
    public function getRequestLocale(): string
53
    {
NEW
54
        $locale = $this->requestStack->getCurrentRequest()?->getLocale() ?? Locale::getDefault()->value;
×
55

NEW
56
        return in_array($locale, $this->getLocales(), true) ? $locale : Locale::getDefault()->value;
×
57
    }
58

59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getTimezones(): array
63
    {
64
        $output = [];
4✔
65

66
        try {
67
            /** @var array<int, array{timezone: string, identifier: string, offset: string, value: string}> $output */
68
            $output = $this->appCache->get('application_timezone', $this->getClosure());
4✔
69
        } catch (Throwable $exception) {
×
70
            $this->logger->error($exception->getMessage(), $exception->getTrace());
×
71
        }
72

73
        return $output;
4✔
74
    }
75

76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getFormattedTimezones(): array
80
    {
81
        $output = [];
1✔
82

83
        /** @var array<int, non-empty-string> $identifiers */
84
        $identifiers = DateTimeZone::listIdentifiers();
1✔
85

86
        foreach ($identifiers as $identifier) {
1✔
87
            $dateTimeZone = new DateTimeZone($identifier);
1✔
88

89
            $dateTime = new DateTimeImmutable(timezone: $dateTimeZone);
1✔
90

91
            $hours = floor($dateTimeZone->getOffset($dateTime) / 3600);
1✔
92
            $minutes = floor(($dateTimeZone->getOffset($dateTime) - ($hours * 3600)) / 60);
1✔
93

94
            $hours = 'GMT' . ($hours < 0 ? $hours : '+' . $hours);
1✔
95
            $minutes = ($minutes > 0 ? $minutes : '0' . $minutes);
1✔
96

97
            $output[] = [
1✔
98
                'timezone' => explode('/', $identifier)[0],
1✔
99
                'identifier' => $identifier,
1✔
100
                'offset' => $hours . ':' . $minutes,
1✔
101
                'value' => str_replace('_', ' ', $identifier),
1✔
102
            ];
1✔
103
        }
104

105
        return $output;
1✔
106
    }
107

108
    private function getClosure(): Closure
109
    {
110
        return function (ItemInterface $item): array {
4✔
111
            // One year
112
            $item->expiresAfter(31536000);
1✔
113

114
            return $this->getFormattedTimezones();
1✔
115
        };
4✔
116
    }
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc