Passed
Push — 6.4.15.2 ( 9656d6 )
by
unknown
16:35
created

BatchController::validateFilesytems()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Recovery\Update\Controller;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Shopware\Core\Framework\Migration\MigrationCollectionLoader;
8
use Shopware\Recovery\Common\Steps\FinishResult;
9
use Shopware\Recovery\Common\Steps\MigrationStep;
10
use Shopware\Recovery\Common\Steps\ResultMapper;
11
use Shopware\Recovery\Common\Steps\ValidResult;
12
use Shopware\Recovery\Common\Utils;
13
use Shopware\Recovery\Update\DependencyInjection\Container;
14
use Shopware\Recovery\Update\FilesystemFactory;
15
use Shopware\Recovery\Update\PathBuilder;
16
use Shopware\Recovery\Update\Steps\UnpackStep;
17
18
class BatchController
19
{
20
    /**
21
     * @var Container
22
     */
23
    private $container;
24
25
    /**
26
     * @var ResultMapper
27
     */
28
    private $resultMapper;
29
30
    public function __construct(Container $container)
31
    {
32
        $this->container = $container;
33
34
        $this->resultMapper = new ResultMapper();
35
    }
36
37
    public function applyMigrations(ServerRequestInterface $request, ResponseInterface $response)
38
    {
39
        $queryParameters = $request->getQueryParams();
40
41
        $offset = (int) $queryParameters['offset'];
42
        $total = (int) $queryParameters['total'];
43
        $modus = $queryParameters['modus'];
44
45
        /** @var MigrationCollectionLoader $migrationCollectionLoader */
46
        $migrationCollectionLoader = $this->container->get('migration.collection.loader');
47
48
        $shopwareVersion = (string) $this->container->get('shopware.version');
49
        $versionSelectionMode = $modus === MigrationStep::UPDATE_DESTRUCTIVE
50
            // only execute safe destructive migrations
51
            ? MigrationCollectionLoader::VERSION_SELECTION_SAFE
52
            : MigrationCollectionLoader::VERSION_SELECTION_ALL;
53
54
        $coreMigrations = $migrationCollectionLoader->collectAllForVersion(
55
            $shopwareVersion,
56
            // only execute safe destructive migrations
57
            $versionSelectionMode
58
        );
59
60
        $result = (new MigrationStep($coreMigrations))
61
            ->run($modus, $offset, $total);
62
63
        return $this->toJson($response, 200, $this->resultMapper->toExtJs($result));
64
    }
65
66
    /**
67
     * @throws \RuntimeException
68
     */
69
    public function unpack(ServerRequestInterface $request, ResponseInterface $response)
70
    {
71
        // Manual updates do not contain files to overwrite
72
        if (UPDATE_IS_MANUAL) {
73
            Utils::clearOpcodeCache();
74
75
            return $this->toJson($response, 200, $this->resultMapper->toExtJs(new FinishResult(0, 0)));
76
        }
77
78
        $queryParameters = $request->getQueryParams();
79
80
        $offset = (int) $queryParameters['offset'];
81
        $total = (int) $queryParameters['total'];
82
83
        /** @var FilesystemFactory $factory */
84
        $factory = $this->container->get('filesystem.factory');
85
86
        $localFilesystem = $factory->createLocalFilesystem();
87
        $remoteFilesystem = $factory->createRemoteFilesystem();
88
89
        /** @var PathBuilder $pathBuilder */
90
        $pathBuilder = $this->container->get('path.builder');
91
92
        $debug = false;
93
        $step = new UnpackStep($localFilesystem, $remoteFilesystem, $pathBuilder, $debug);
94
95
        $result = $step->run($offset, $total);
96
97
        if ($result instanceof ValidResult) {
98
            Utils::clearOpcodeCache();
99
        }
100
101
        return $this->toJson($response, 200, $this->resultMapper->toExtJs($result));
102
    }
103
104
    private function toJson(ResponseInterface $response, int $code, array $data): ResponseInterface
105
    {
106
        return $response
107
            ->withStatus($code)
108
            ->withHeader('Content-Type', 'application/json')
109
            ->write(json_encode($data, \JSON_PRETTY_PRINT));
0 ignored issues
show
Bug introduced by
The method write() does not exist on Psr\Http\Message\ResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
            ->/** @scrutinizer ignore-call */ write(json_encode($data, \JSON_PRETTY_PRINT));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
    }
111
}
112