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

eliashaeussler / typo3-badges / 16766588977

06 Aug 2025 03:12AM UTC coverage: 96.122%. Remained the same
16766588977

push

github

web-flow
[TASK] Update phpunit/phpunit to v12.3.0

| datasource | package         | from   | to     |
| ---------- | --------------- | ------ | ------ |
| packagist  | phpunit/phpunit | 12.2.7 | 12.3.0 |

471 of 490 relevant lines covered (96.12%)

7.22 hits per line

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

98.33
/src/Service/ApiService.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Symfony project "eliashaeussler/typo3-badges".
7
 *
8
 * Copyright (C) 2021-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace App\Service;
25

26
use App\Entity\Dto\ExtensionMetadata;
27
use DateTime;
28
use Symfony\Contracts\Cache\CacheInterface;
29
use Symfony\Contracts\Cache\ItemInterface;
30
use Symfony\Contracts\HttpClient\HttpClientInterface;
31

32
/**
33
 * ApiService.
34
 *
35
 * @author Elias Häußler <elias@haeussler.dev>
36
 * @license GPL-3.0-or-later
37
 */
38
final readonly class ApiService
39
{
40
    private const string FALLBACK_EXTENSION_KEY = 'handlebars';
41

42
    public function __construct(
34✔
43
        private HttpClientInterface $client,
44
        private CacheInterface $cache,
45
        private int $cacheExpirationPeriod = 3600,
46
    ) {}
34✔
47

48
    public function getExtensionMetadata(string $extensionKey): ExtensionMetadata
23✔
49
    {
50
        $apiPath = $this->buildApiPath('/extension/{extension}', ['extension' => $extensionKey]);
23✔
51

52
        // Fetch extension metadata from cache or external API
53
        $extensionMetadata = $this->cache->get(
23✔
54
            $this->calculateCacheIdentifier('typo3_api.extension_metadata', ['apiPath' => $apiPath]),
23✔
55
            fn (ItemInterface $item) => $this->sendRequestAndCacheResponse($apiPath, $item),
23✔
56
            null,
23✔
57
            $cacheMetadata,
23✔
58
        );
23✔
59

60
        return new ExtensionMetadata(
23✔
61
            $extensionMetadata,
23✔
62
            $this->determineCacheExpiryDateFromCacheMetadata($cacheMetadata),
23✔
63
        );
23✔
64
    }
65

66
    public function getRandomExtensionMetadata(): ExtensionMetadata
4✔
67
    {
68
        $apiPath = $this->buildApiPath('/extension');
4✔
69

70
        // Fetch current extensions from cache or external API
71
        $result = $this->cache->get(
4✔
72
            $this->calculateCacheIdentifier('typo3_api.random_extensions', ['apiPath' => $apiPath]),
4✔
73
            function (ItemInterface $item) use ($apiPath): array {
4✔
74
                // Build random filter options
75
                $filterOptions = [
3✔
76
                    'page' => random_int(1, 10),
3✔
77
                    'per_page' => 20,
3✔
78
                    'filter' => [
3✔
79
                        // @todo Go back to random_int(11, 13) once https://git.typo3.org/services/t3o-sites/extensions.typo3.org/ter/-/merge_requests/794 is merged
80
                        'typo3_version' => random_int(9, 11),
3✔
81
                    ],
3✔
82
                ];
3✔
83
                $apiUrl = $apiPath.'?'.http_build_query($filterOptions);
3✔
84

85
                return $this->sendRequestAndCacheResponse($apiUrl, $item, 60 * 60 * 24);
3✔
86
            },
4✔
87
            null,
4✔
88
            $cacheMetadata,
4✔
89
        );
4✔
90

91
        $extensions = $result['extensions'] ?? [];
4✔
92

93
        if ([] === $extensions) {
4✔
94
            return new ExtensionMetadata(['key' => self::FALLBACK_EXTENSION_KEY]);
1✔
95
        }
96

97
        return new ExtensionMetadata(
3✔
98
            ['key' => $extensions[array_rand($extensions)]['key']],
3✔
99
            $this->determineCacheExpiryDateFromCacheMetadata($cacheMetadata),
3✔
100
        );
3✔
101
    }
102

103
    /**
104
     * @return array<int|string, mixed>
105
     */
106
    private function sendRequestAndCacheResponse(string $path, ItemInterface $item, ?int $expiresAfter = null): array
25✔
107
    {
108
        $response = $this->client->request('GET', $path);
25✔
109
        $responseArray = $response->toArray();
25✔
110

111
        $item->expiresAfter($expiresAfter ?? $this->cacheExpirationPeriod);
25✔
112
        $item->set($responseArray);
25✔
113

114
        return $responseArray;
25✔
115
    }
116

117
    /**
118
     * @param array<string, mixed> $parameters
119
     */
120
    private function buildApiPath(string $endpoint, array $parameters = []): string
27✔
121
    {
122
        $replacePairs = array_combine(
27✔
123
            array_map(fn (string $parameter): string => '{'.trim($parameter, '{}').'}', array_keys($parameters)),
27✔
124
            array_values($parameters),
27✔
125
        );
27✔
126

127
        return '/api/v1/'.ltrim(strtr($endpoint, $replacePairs), '/');
27✔
128
    }
129

130
    /**
131
     * @param array<string, string> $options
132
     */
133
    private function calculateCacheIdentifier(string $key, array $options = []): string
27✔
134
    {
135
        return hash('sha512', $key.'_'.json_encode($options, JSON_THROW_ON_ERROR));
27✔
136
    }
137

138
    /**
139
     * @param array{expiry?: int|numeric-string}|null $cacheMetadata
140
     */
141
    private function determineCacheExpiryDateFromCacheMetadata(?array $cacheMetadata): ?DateTime
26✔
142
    {
143
        if (!isset($cacheMetadata[ItemInterface::METADATA_EXPIRY])) {
26✔
144
            return null;
2✔
145
        }
146

147
        $expiryDate = DateTime::createFromFormat('U', (string) (int) $cacheMetadata[ItemInterface::METADATA_EXPIRY]);
24✔
148

149
        if (false === $expiryDate) {
24✔
150
            return null;
×
151
        }
152

153
        return $expiryDate;
24✔
154
    }
155
}
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