Passed
Push — trunk ( aca09f...bd7f95 )
by Christian
14:04 queued 13s
created

RecoveryManager::getCurrentShopwareVersion()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 18
rs 9.6111
1
<?php
2
declare(strict_types=1);
3
4
namespace Shopware\WebInstaller\Services;
5
6
use Shopware\Core\Framework\Log\Package;
7
use Shopware\WebInstaller\InstallerException;
8
use Symfony\Component\HttpFoundation\Request;
9
10
/**
11
 * @internal
12
 */
13
#[Package('core')]
14
class RecoveryManager
15
{
16
    public function getBinary(): string
17
    {
18
        /** @var string $fileName */
19
        $fileName = $_SERVER['SCRIPT_FILENAME'];
20
21
        return $fileName;
22
    }
23
24
    public function getPHPBinary(Request $request): string
25
    {
26
        $phpBinary = $request->getSession()->get('phpBinary');
27
        \assert(\is_string($phpBinary));
28
29
        return $phpBinary;
30
    }
31
32
    public function getProjectDir(): string
33
    {
34
        $fileName = realpath($_SERVER['SCRIPT_FILENAME']);
35
        \assert(\is_string($fileName));
36
37
        return \dirname($fileName);
38
    }
39
40
    public function getShopwareLocation(): string
41
    {
42
        $projectDir = $this->getProjectDir();
43
44
        $composerLookup = \dirname($projectDir) . '/composer.lock';
45
46
        // The Shopware installation is always in the "public" directory
47
        if (basename($projectDir) !== 'public') {
48
            throw InstallerException::cannotFindShopwareInstallation();
49
        }
50
51
        if (file_exists($composerLookup)) {
52
            /** @var array{packages: array{name: string, version: string}[]} $composerJson */
53
            $composerJson = json_decode((string) file_get_contents($composerLookup), true, \JSON_THROW_ON_ERROR);
54
55
            foreach ($composerJson['packages'] as $package) {
56
                if ($package['name'] === 'shopware/core' || $package['name'] === 'shopware/platform') {
57
                    return \dirname($composerLookup);
58
                }
59
            }
60
        }
61
62
        throw InstallerException::cannotFindShopwareInstallation();
63
    }
64
65
    public function getCurrentShopwareVersion(string $shopwarePath): string
66
    {
67
        $lockFile = $shopwarePath . '/composer.lock';
68
69
        if (!file_exists($lockFile)) {
70
            throw InstallerException::cannotFindComposerLock();
71
        }
72
73
        /** @var array{packages: array{name: string, version: string}[]} $composerLock */
74
        $composerLock = json_decode((string) file_get_contents($lockFile), true, \JSON_THROW_ON_ERROR);
75
76
        foreach ($composerLock['packages'] as $package) {
77
            if ($package['name'] === 'shopware/core' || $package['name'] === 'shopware/platform') {
78
                return ltrim($package['version'], 'v');
79
            }
80
        }
81
82
        throw InstallerException::cannotFindShopwareInComposerLock();
83
    }
84
85
    public function isFlexProject(string $shopwarePath): bool
86
    {
87
        return file_exists($shopwarePath . '/symfony.lock');
88
    }
89
}
90