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

RecoveryManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentShopwareVersion() 0 18 5
A isFlexProject() 0 3 1
A getProjectDir() 0 6 1
A getBinary() 0 6 1
A getShopwareLocation() 0 22 5
A getPHPBinary() 0 6 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Services;
5
6
use Shopware\Core\Framework\Log\Package;
7
use Symfony\Component\HttpFoundation\Request;
8
9
/**
10
 * @internal
11
 */
12
#[Package('core')]
13
class RecoveryManager
14
{
15
    public function getBinary(): string
16
    {
17
        /** @var string $fileName */
18
        $fileName = $_SERVER['SCRIPT_FILENAME'];
19
20
        return $fileName;
21
    }
22
23
    public function getPHPBinary(Request $request): string
24
    {
25
        $phpBinary = $request->getSession()->get('phpBinary');
26
        \assert(\is_string($phpBinary));
27
28
        return $phpBinary;
29
    }
30
31
    public function getProjectDir(): string
32
    {
33
        /** @var string $fileName */
34
        $fileName = $_SERVER['SCRIPT_FILENAME'];
35
36
        return \dirname($fileName);
37
    }
38
39
    public function getShopwareLocation(): string
40
    {
41
        $projectDir = $this->getProjectDir();
42
43
        $composerLookups = [
44
            \dirname($projectDir) . '/composer.json',
45
            $projectDir . '/composer.json',
46
            $projectDir . '/shopware/composer.json',
47
        ];
48
49
        foreach ($composerLookups as $composerLookup) {
50
            if (file_exists($composerLookup)) {
51
                /** @var array{require: array<string, string>} $composerJson */
52
                $composerJson = json_decode((string) file_get_contents($composerLookup), true, \JSON_THROW_ON_ERROR);
53
54
                if (isset($composerJson['require']['shopware/core']) || isset($composerJson['require']['shopware/platform'])) {
55
                    return \dirname($composerLookup);
56
                }
57
            }
58
        }
59
60
        throw new \RuntimeException('Could not find Shopware installation');
61
    }
62
63
    public function getCurrentShopwareVersion(string $shopwarePath): string
64
    {
65
        $lockFile = $shopwarePath . '/composer.lock';
66
67
        if (!file_exists($lockFile)) {
68
            return 'unknown';
69
        }
70
71
        /** @var array{packages: array{name: string, version: string}[]} $composerLock */
72
        $composerLock = json_decode((string) file_get_contents($lockFile), true, \JSON_THROW_ON_ERROR);
73
74
        foreach ($composerLock['packages'] as $package) {
75
            if ($package['name'] === 'shopware/core' || $package['name'] === 'shopware/platform') {
76
                return $package['version'];
77
            }
78
        }
79
80
        return 'unknown';
81
    }
82
83
    public function isFlexProject(string $shopwarePath): bool
84
    {
85
        return file_exists($shopwarePath . '/symfony.lock');
86
    }
87
}
88