Passed
Push — 6.5.0.0 ( 85bd4e...fe6596 )
by Christian
24:46 queued 09:10
created

ReleaseInfoProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B fetchLatestRelease() 0 33 8
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Services;
5
6
use Composer\Util\Platform;
7
use Shopware\Core\Framework\Log\Package;
8
use Symfony\Component\HttpClient\HttpClient;
9
use Symfony\Contracts\HttpClient\HttpClientInterface;
10
11
/**
12
 * @internal
13
 */
14
#[Package('core')]
15
class ReleaseInfoProvider
16
{
17
    private HttpClientInterface $client;
18
19
    public function __construct(?HttpClientInterface $client = null)
20
    {
21
        $this->client = $client ?? HttpClient::create();
22
    }
23
24
    /**
25
     * @return array<string, string>
26
     */
27
    public function fetchLatestRelease(): array
28
    {
29
        $nextVersion = Platform::getEnv('SW_RECOVERY_NEXT_VERSION');
30
        if (\is_string($nextVersion)) {
0 ignored issues
show
introduced by
The condition is_string($nextVersion) is always true.
Loading history...
31
            return [
32
                '6.4' => '6.4.17.2',
33
                '6.5' => $nextVersion,
34
            ];
35
        }
36
37
        /** @var array{packages: array{"shopware/core": array{version: string}[]}} $response */
38
        $response = $this->client->request('GET', 'https://repo.packagist.org/p2/shopware/core.json')->toArray();
39
40
        $versions = array_column($response['packages']['shopware/core'], 'version');
41
42
        // Index them by major version
43
        $mappedVersions = [];
44
45
        foreach ($versions as $version) {
46
            if (str_contains($version, 'dev-') || str_contains($version, 'alpha') || str_contains($version, 'beta') || str_contains($version, 'rc')) {
47
                continue;
48
            }
49
50
            $major = substr($version, 0, 3);
51
52
            if (isset($mappedVersions[$major])) {
53
                continue;
54
            }
55
56
            $mappedVersions[$major] = $version;
57
        }
58
59
        return $mappedVersions;
60
    }
61
}
62