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

ProjectComposerJsonUpdater::update()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
nc 14
nop 2
dl 0
loc 44
rs 8.5866
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace App\Services;
4
5
use Composer\Util\Platform;
6
use Shopware\Core\Framework\Log\Package;
7
8
/**
9
 * @internal
10
 */
11
#[Package('core')]
12
class ProjectComposerJsonUpdater
13
{
14
    public static function update(string $file, string $latestVersion): void
15
    {
16
        $shopwarePackages = [
17
            'shopware/core',
18
            'shopware/administration',
19
            'shopware/storefront',
20
            'shopware/elasticsearch',
21
        ];
22
23
        /** @var array{minimum-stability?: string, require: array<string, string>} $composerJson */
24
        $composerJson = json_decode((string) file_get_contents($file), true, \JSON_THROW_ON_ERROR);
25
26
        if (str_contains(strtolower($latestVersion), 'rc')) {
27
            $composerJson['minimum-stability'] = 'RC';
28
        } else {
29
            unset($composerJson['minimum-stability']);
30
        }
31
32
        foreach ($shopwarePackages as $shopwarePackage) {
33
            if (!isset($composerJson['require'][$shopwarePackage])) {
34
                continue;
35
            }
36
37
            // Lock the composer version to that major version
38
            $version = '~' . substr($latestVersion, 0, 3) . '.0';
39
40
            $nextVersion = Platform::getEnv('SW_RECOVERY_NEXT_VERSION');
41
            if (\is_string($nextVersion)) {
42
                $nextBranch = Platform::getEnv('SW_RECOVERY_NEXT_BRANCH');
43
                if ($nextBranch === false) {
44
                    $nextBranch = 'dev-trunk';
45
                }
46
47
                if ($nextBranch === $nextVersion) {
48
                    $version = $nextBranch;
49
                } else {
50
                    $version = $nextBranch . ' as ' . $nextVersion;
51
                }
52
            }
53
54
            $composerJson['require'][$shopwarePackage] = $version;
55
        }
56
57
        file_put_contents($file, json_encode($composerJson, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
58
    }
59
}
60