Passed
Push — 6.5.0.0 ( 149d7e...f7649e )
by Christian
11:39 queued 13s
created

ReleaseInfoProvider::getNextMajor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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>
26
     */
27
    public function fetchVersions(): array
28
    {
29
        /** @var array<string> $versions */
30
        $versions = $this->client->request('GET', 'https://releases.shopware.com/changelog/index.json')->toArray();
31
32
        usort($versions, function ($a, $b) {
33
            return version_compare($b, $a);
34
        });
35
36
        return array_values(array_filter($versions, function ($version) {
37
            return version_compare($version, '6.4.18.0', '>=');
38
        }));
39
    }
40
41
    /**
42
     * @return array<string>
43
     */
44
    public function fetchUpdateVersions(string $currentVersion): array
45
    {
46
        $nextVersion = Platform::getEnv('SW_RECOVERY_NEXT_VERSION');
47
        if (\is_string($nextVersion)) {
0 ignored issues
show
introduced by
The condition is_string($nextVersion) is always true.
Loading history...
48
            return [
49
                $nextVersion,
50
            ];
51
        }
52
53
        // Get all versions newer than the current one
54
        $versions = array_values(array_filter($this->fetchVersions(), function ($version) use ($currentVersion) {
55
            return version_compare($version, $currentVersion, '>');
56
        }));
57
58
        // Index them by major version
59
        $mappedVersions = [];
60
61
        foreach ($versions as $version) {
62
            $major = $this->getMajor($version);
63
64
            if (!isset($mappedVersions[$major])) {
65
                $mappedVersions[$major] = [];
66
            }
67
68
            $mappedVersions[$major][] = $version;
69
        }
70
71
        return [
72
            ...$mappedVersions[$this->getNextMajor($currentVersion)] ?? [],
73
            ...$mappedVersions[$this->getMajor($currentVersion)] ?? [],
74
        ];
75
    }
76
77
    private function getMajor(string $version): string
78
    {
79
        $list = explode('.', $version, 3);
80
81
        return $list[0] . '.' . $list[1];
82
    }
83
84
    private function getNextMajor(string $version): string
85
    {
86
        $list = explode('.', $version, 3);
87
88
        ++$list[1];
89
90
        return $list[0] . '.' . $list[1];
91
    }
92
}
93