Passed
Push — 6.4.8.0 ( 68cdd6...a2feb4 )
by Christian
20:56 queued 17s
created

Container::setup()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 195
Code Lines 109

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 109
nc 1
nop 1
dl 0
loc 195
rs 7.0666
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Shopware\Recovery\Update\DependencyInjection;
4
5
use Doctrine\DBAL\DriverManager;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Log\NullLogger;
9
use Shopware\Core\Framework\Feature;
10
use Shopware\Core\Framework\Migration\MigrationCollectionLoader as CoreMigrationCollectionLoader;
11
use Shopware\Core\Framework\Migration\MigrationRuntime as CoreMigrationRuntime;
12
use Shopware\Core\Maintenance\System\Service\JwtCertificateGenerator;
13
use Shopware\Recovery\Common\DependencyInjection\Container as BaseContainer;
14
use Shopware\Recovery\Common\HttpClient\CurlClient;
15
use Shopware\Recovery\Common\MigrationSourceCollector;
16
use Shopware\Recovery\Common\Service\JwtCertificateService;
17
use Shopware\Recovery\Common\Service\RecoveryConfigManager;
18
use Shopware\Recovery\Common\SystemLocker;
19
use Shopware\Recovery\Update\Cleanup;
20
use Shopware\Recovery\Update\CleanupFilesFinder;
21
use Shopware\Recovery\Update\Controller\BatchController;
22
use Shopware\Recovery\Update\Controller\CleanupController;
23
use Shopware\Recovery\Update\Controller\RequirementsController;
24
use Shopware\Recovery\Update\FilePermissionChanger;
25
use Shopware\Recovery\Update\FilesystemFactory;
26
use Shopware\Recovery\Update\PathBuilder;
27
use Shopware\Recovery\Update\StoreApi;
28
use Shopware\Recovery\Update\Utils;
29
use Slim\App;
0 ignored issues
show
Bug introduced by
The type Slim\App was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Slim\Views\PhpRenderer;
0 ignored issues
show
Bug introduced by
The type Slim\Views\PhpRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use Symfony\Component\Dotenv\Dotenv;
32
use const SW_PATH;
33
34
class Container extends BaseContainer
35
{
36
    public function setup(\Pimple\Container $container): void
0 ignored issues
show
Bug introduced by
The type Pimple\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
    {
38
        $backupDir = SW_PATH . \DIRECTORY_SEPARATOR . 'files' . \DIRECTORY_SEPARATOR . 'backup' . \DIRECTORY_SEPARATOR . 'auto_update';
39
40
        $me = $this;
41
42
        $container['shopware.version'] = function () use ($me) {
0 ignored issues
show
Unused Code introduced by
The import $me is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
43
            $version = trim(file_get_contents(UPDATE_ASSET_PATH . \DIRECTORY_SEPARATOR . 'version'));
44
45
            return $version;
46
        };
47
48
        $container['env.path'] = static function () {
49
            return SW_PATH . '/.env';
50
        };
51
52
        $container['default.env.path'] = static function () {
53
            return UPDATE_ASSET_PATH . '/.env.defaults';
54
        };
55
56
        $container['default.env'] = static function ($c) {
57
            if (!is_readable($c['default.env.path'])) {
58
                return [];
59
            }
60
61
            return (new DotEnv())
62
                ->usePutenv(true)
63
                ->parse(file_get_contents($c['default.env.path']), $c['default.env.path']);
64
        };
65
66
        $container['env.path'] = static function () {
67
            return SW_PATH . '/.env';
68
        };
69
70
        $container['env.load'] = static function ($c) {
71
            $defaultPath = $c['default.env.path'];
72
            $path = $c['env.path'];
73
74
            return static function () use ($defaultPath, $path): void {
75
                if (is_readable((string) $defaultPath)) {
76
                    (new Dotenv())
77
                        ->usePutenv(true)
78
                        ->load((string) $defaultPath);
79
                }
80
                if (is_readable((string) $path)) {
81
                    (new Dotenv())
82
                        ->usePutenv(true)
83
                        ->load((string) $path);
84
                }
85
            };
86
        };
87
88
        $container['feature.isActive'] = static function ($c) {
89
            // load .env on first call
90
            $c['env.load']();
91
92
            return static function (string $featureName): bool {
93
                return Feature::isActive($featureName);
94
            };
95
        };
96
97
        $container['db'] = function () {
98
            return Utils::getConnection(SW_PATH);
99
        };
100
101
        $container['dbal'] = function ($c) {
102
            $options = [
103
                'pdo' => $c['db'],
104
                'driver' => 'pdo_mysql',
105
            ];
106
107
            return DriverManager::getConnection($options);
108
        };
109
110
        $container['filesystem.factory'] = function () use ($me) {
111
            $updateConfig = $me->getParameter('update.config');
112
            $ftp = (isset($updateConfig['ftp_credentials'])) ? $updateConfig['ftp_credentials'] : [];
113
114
            return new FilesystemFactory(SW_PATH, $ftp);
115
        };
116
117
        $container['path.builder'] = function () use ($backupDir) {
118
            $baseDir = SW_PATH;
119
            $updateDir = UPDATE_FILES_PATH;
120
121
            return new PathBuilder($baseDir, $updateDir, $backupDir);
122
        };
123
124
        $container['migration.sources'] = static function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

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

124
        $container['migration.sources'] = static function (/** @scrutinizer ignore-unused */ $c) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
            return MigrationSourceCollector::collect();
126
        };
127
128
        $container['migration.runtime'] = static function ($c) {
129
            return new CoreMigrationRuntime($c['dbal'], new NullLogger());
130
        };
131
132
        $container['migration.collection.loader'] = static function ($c) {
133
            return new CoreMigrationCollectionLoader($c['dbal'], $c['migration.runtime'], $c['migration.sources']);
134
        };
135
136
        $container['app'] = function ($c) {
137
            foreach ($c['config']['slim'] as $k => $v) {
138
                $c[$k] = $v;
139
            }
140
141
            return new App($c);
142
        };
143
144
        $container['http-client'] = function () {
145
            return new CurlClient();
146
        };
147
148
        $container['store.api'] = function () use ($me) {
149
            return new StoreApi(
150
                $me->get('http-client'),
151
                $me->getParameter('storeapi.endpoint')
152
            );
153
        };
154
155
        $container['cleanup.files.finder'] = function () {
156
            return new CleanupFilesFinder(SW_PATH);
157
        };
158
159
        $container['system.locker'] = function () {
160
            return new SystemLocker(
161
                SW_PATH . \DIRECTORY_SEPARATOR . 'recovery' . \DIRECTORY_SEPARATOR . 'install' . \DIRECTORY_SEPARATOR . 'data' . \DIRECTORY_SEPARATOR . 'install.lock'
162
            );
163
        };
164
165
        $container['controller.batch'] = function () use ($me) {
166
            return new BatchController(
167
                $me
168
            );
169
        };
170
171
        $container['controller.requirements'] = function () use ($me) {
172
            return new RequirementsController(
173
                $me,
174
                $me->get('app')
175
            );
176
        };
177
178
        $container['controller.cleanup'] = function () use ($me, $backupDir) {
179
            return new CleanupController(
180
                $me->get('cleanup.files.finder'),
181
                $me->get('shopware.update.cleanup'),
182
                $me->get('app'),
183
                SW_PATH,
184
                $backupDir
185
            );
186
        };
187
188
        $container['shopware.update.cleanup'] = function ($container) use ($backupDir) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

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

188
        $container['shopware.update.cleanup'] = function (/** @scrutinizer ignore-unused */ $container) use ($backupDir) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
            return new Cleanup(SW_PATH, $backupDir);
190
        };
191
192
        $container['shopware.update.chmod'] = function ($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

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

192
        $container['shopware.update.chmod'] = function (/** @scrutinizer ignore-unused */ $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
193
            return new FilePermissionChanger([
194
                ['chmod' => 0775, 'filePath' => SW_PATH . '/bin/console'],
195
            ]);
196
        };
197
198
        $container['renderer'] = function ($c) {
199
            return new PhpRenderer($c['config']['slim']['templates.path']);
200
        };
201
202
        $container['errorHandler'] = function ($c) {
203
            return static function (ServerRequestInterface $request, ResponseInterface $response, \Throwable $e) use ($c) {
0 ignored issues
show
Unused Code introduced by
The import $c is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
204
                if (empty($request->getHeader('X-Requested-With'))) {
205
                    throw $e;
206
                }
207
208
                $data = [
209
                    'code' => $e->getCode(),
210
                    'message' => $e->getMessage(),
211
                    'file' => $e->getFile(),
212
                    'line' => $e->getLine(),
213
                    'trace' => $e->getTraceAsString(),
214
                ];
215
216
                return $response->withStatus(500)
217
                    ->withHeader('Content-Type', 'application/json')
218
                    ->write(json_encode($data));
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

218
                    ->/** @scrutinizer ignore-call */ write(json_encode($data));

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...
219
            };
220
        };
221
222
        $container['jwt_certificate.writer'] = static function () {
223
            return new JwtCertificateService(
224
                SW_PATH . '/config/jwt/',
225
                new JwtCertificateGenerator()
226
            );
227
        };
228
229
        $container['system.config'] = static function ($c) {
230
            return new RecoveryConfigManager($c['dbal']);
231
        };
232
    }
233
}
234