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

InstallController::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 39
rs 9.472
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Controller;
5
6
use App\Services\ProjectComposerJsonUpdater;
7
use App\Services\RecoveryManager;
8
use App\Services\ReleaseInfoProvider;
9
use App\Services\StreamedCommandResponseGenerator;
10
use Shopware\Core\Framework\Log\Package;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\Filesystem\Filesystem;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpFoundation\StreamedResponse;
16
use Symfony\Component\Process\Process;
17
use Symfony\Component\Routing\Annotation\Route;
18
19
/**
20
 * @internal
21
 */
22
#[Package('core')]
23
class InstallController extends AbstractController
24
{
25
    public function __construct(
26
        private readonly RecoveryManager $recoveryManager,
27
        private readonly StreamedCommandResponseGenerator $streamedCommandResponseGenerator,
28
        private readonly ReleaseInfoProvider $releaseInfoProvider
29
    ) {
30
    }
31
32
    #[Route('/install', name: 'install', defaults: ['step' => 2])]
33
    public function index(): Response
34
    {
35
        $versions = $this->releaseInfoProvider->fetchVersions();
36
37
        return $this->render('install.html.twig', [
38
            'versions' => $versions,
39
        ]);
40
    }
41
42
    #[Route('/install/_run', name: 'install_run', methods: ['POST'])]
43
    public function run(Request $request): StreamedResponse
44
    {
45
        $shopwareVersion = $request->query->get('shopwareVersion', '');
46
        $folder = $this->recoveryManager->getProjectDir();
47
48
        $fs = new Filesystem();
49
        $fs->copy(\dirname(__DIR__) . '/Resources/install-template/composer.json', $folder . '/composer.json');
50
        $fs->dumpFile($folder . '/.env', \PHP_EOL);
51
        $fs->dumpFile($folder . '/.gitignore', '/.idea
52
/vendor/
53
');
54
        $fs->mkdir($folder . '/custom/plugins');
55
        $fs->mkdir($folder . '/custom/static-plugins');
56
57
        ProjectComposerJsonUpdater::update(
58
            $folder . '/composer.json',
59
            $shopwareVersion
60
        );
61
62
        $finish = function (Process $process) use ($request): void {
63
            echo json_encode([
64
                'success' => $process->isSuccessful(),
65
                'newLocation' => $request->getBasePath() . '/public/',
66
            ]);
67
        };
68
69
        return $this->streamedCommandResponseGenerator->run([
70
            $this->recoveryManager->getPhpBinary($request),
71
            '-dmemory_limit=1G',
72
            $this->recoveryManager->getBinary(),
73
            'composer',
74
            'install',
75
            '-d',
76
            $folder,
77
            '--no-interaction',
78
            '--no-ansi',
79
            '-v',
80
        ], $finish);
81
    }
82
83
    /**
84
     * @codeCoverageIgnore
85
     */
86
    #[Route('/install/_cleanup', name: 'install_cleanup', methods: ['POST'])]
87
    public function cleanup(): StreamedResponse
88
    {
89
        $folder = $this->recoveryManager->getProjectDir();
90
91
        $fs = new Filesystem();
92
        $htaccessFile = $folder . '/public/.htaccess';
93
94
        // Shopware 6.4 does not contain a htaccess by default
95
        if (!$fs->exists($htaccessFile)) {
96
            $fs = new Filesystem();
97
            $fs->copy(\dirname(__DIR__) . '/Resources/install-template/htaccess', $htaccessFile);
98
        }
99
100
        $self = $_SERVER['SCRIPT_FILENAME'];
101
        \assert(\is_string($self));
102
103
        // Below this line call only php native functions as we deleted our own files already
104
        unlink($self);
105
106
        if (\function_exists('opcache_reset')) {
107
            opcache_reset();
108
        }
109
110
        exit();
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Symfony\Component\HttpFoundation\StreamedResponse. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
111
    }
112
}
113