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

ContainerProvider::createMigrationSource()   B

Complexity

Conditions 8
Paths 60

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
c 0
b 0
f 0
nc 60
nop 2
dl 0
loc 42
rs 8.4444
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Recovery\Install;
4
5
use Pimple\Container;
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...
6
use Pimple\ServiceProviderInterface;
0 ignored issues
show
Bug introduced by
The type Pimple\ServiceProviderInterface 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...
7
use Psr\Log\NullLogger;
8
use Shopware\Core\Framework\Feature;
9
use Shopware\Core\Framework\Migration\MigrationCollectionLoader as CoreMigrationCollectionLoader;
10
use Shopware\Core\Framework\Migration\MigrationRuntime as CoreMigrationRuntime;
11
use Shopware\Core\Maintenance\SalesChannel\Service\SalesChannelCreator;
12
use Shopware\Core\Maintenance\System\Service\JwtCertificateGenerator;
13
use Shopware\Core\Maintenance\System\Service\ShopConfigurator;
14
use Shopware\Core\Maintenance\User\Service\UserProvisioner;
15
use Shopware\Recovery\Common\HttpClient\CurlClient;
16
use Shopware\Recovery\Common\MigrationSourceCollector;
17
use Shopware\Recovery\Common\Service\JwtCertificateService;
18
use Shopware\Recovery\Common\Service\Notification;
19
use Shopware\Recovery\Common\Service\UniqueIdGenerator;
20
use Shopware\Recovery\Common\SystemLocker;
21
use Shopware\Recovery\Install\Service\AdminService;
22
use Shopware\Recovery\Install\Service\BlueGreenDeploymentService;
23
use Shopware\Recovery\Install\Service\EnvConfigWriter;
24
use Shopware\Recovery\Install\Service\ShopService;
25
use Shopware\Recovery\Install\Service\TranslationService;
26
use Shopware\Recovery\Install\Service\WebserverCheck;
27
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...
28
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...
29
use Symfony\Component\Dotenv\Dotenv;
30
31
class ContainerProvider implements ServiceProviderInterface
32
{
33
    /**
34
     * @var array
35
     */
36
    private $config;
37
38
    public function __construct(array $config = [])
39
    {
40
        $this->config = $config;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function register(Container $container): void
47
    {
48
        $recoveryRoot = \dirname(__DIR__, 2);
49
        $container['config'] = $this->config;
50
        $container['install.language'] = '';
51
52
        $container['shopware.version'] = static function ($c) {
53
            return $c['shopware.kernel']->getContainer()->getParameter('kernel.shopware_version');
54
        };
55
56
        $container['default.env.path'] = static function () {
57
            return SW_PATH . '/.env.defaults';
58
        };
59
60
        $container['default.env'] = static function ($c) {
61
            if (!is_readable($c['default.env.path'])) {
62
                return [];
63
            }
64
65
            return (new DotEnv())
66
                ->usePutenv(true)
67
                ->parse(file_get_contents($c['default.env.path']), $c['default.env.path']);
68
        };
69
70
        $container['env.path'] = static function () {
71
            return SW_PATH . '/.env';
72
        };
73
74
        $container['env.load'] = static function ($c) {
75
            $defaultPath = $c['default.env.path'];
76
            $path = $c['env.path'];
77
78
            return static function () use ($defaultPath, $path): void {
79
                if (is_readable((string) $defaultPath)) {
80
                    (new Dotenv())
81
                        ->usePutenv(true)
82
                        ->load((string) $defaultPath);
83
                }
84
                if (is_readable((string) $path)) {
85
                    (new Dotenv())
86
                        ->usePutenv(true)
87
                        ->load((string) $path);
88
                }
89
            };
90
        };
91
92
        $container['feature.isActive'] = static function ($c) {
93
            // load .env on first call
94
            $c['env.load']();
95
96
            return static function (string $featureName): bool {
97
                return Feature::isActive($featureName);
98
            };
99
        };
100
101
        $container['slim.app'] = static function ($c) {
102
            foreach ($c['config']['slim'] as $k => $v) {
103
                $c[$k] = $v;
104
            }
105
106
            return new App($c);
107
        };
108
109
        $container['renderer'] = static function ($c) {
110
            return new PhpRenderer($c['config']['slim']['templates.path']);
111
        };
112
113
        $container['system.locker'] = static function () {
114
            return new SystemLocker(
115
                SW_PATH . '/install.lock'
116
            );
117
        };
118
119
        $container['translations'] = static function (Container $c) {
120
            // load 'en' as fallback translation
121
            $fallbackTranslation = require __DIR__ . '/../data/lang/en.php';
122
123
            $selectedLanguage = $c->offsetGet('install.language') ?: 'en';
124
            $selectedTranslation = require __DIR__ . "/../data/lang/$selectedLanguage.php";
125
126
            return array_merge($fallbackTranslation, $selectedTranslation);
127
        };
128
129
        $container['translation.service'] = static function (Container $c) {
130
            return new TranslationService($c->offsetGet('translations'));
131
        };
132
133
        $container['http-client'] = static function () {
134
            return new CurlClient();
135
        };
136
137
        $container['install.requirements'] = static function ($c) use ($recoveryRoot) {
138
            return new Requirements($recoveryRoot . '/Common/requirements.php', $c['translation.service']);
139
        };
140
141
        $container['install.requirementsPath'] = static function () use ($recoveryRoot) {
142
            $check = new RequirementsPath(SW_PATH, $recoveryRoot . '/Common/requirements.php');
143
144
            return $check;
145
        };
146
147
        $container['uniqueid.generator'] = static function () {
148
            return new UniqueIdGenerator(
149
                SW_PATH . '/.uniqueid.txt'
150
            );
151
        };
152
153
        $container['config.writer'] = static function ($c) {
154
            return new EnvConfigWriter(
155
                SW_PATH . '/.env',
156
                $c['uniqueid.generator']->getUniqueId(),
157
                $c['default.env'],
158
                $c['shopware.kernel']
159
            );
160
        };
161
162
        $container['jwt_certificate.writer'] = static function () {
163
            return new JwtCertificateService(
164
                SW_PATH . '/config/jwt/',
165
                new JwtCertificateGenerator()
166
            );
167
        };
168
169
        $container['webserver.check'] = static function ($c) {
170
            return new WebserverCheck(
171
                $c['config']['check.ping_url'],
172
                $c['http-client']
173
            );
174
        };
175
176
        $container['menu.helper'] = static function ($c) {
177
            $routes = $c['config']['menu.helper']['routes'];
178
179
            return new MenuHelper(
180
                $c['slim.app'],
181
                $c['translation.service'],
182
                $routes
183
            );
184
        };
185
186
        $container['shopware.notify'] = static function ($c) {
187
            return new Notification(
188
                $c['config']['api.endpoint'],
189
                $c['uniqueid.generator']->getUniqueId(),
190
                $c['http-client'],
191
                $c['shopware.version']
192
            );
193
        };
194
195
        $container['dbal'] = static function (): void {
196
            throw new \RuntimeException('Identifier dbal not initialized yet');
197
        };
198
199
        $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

199
        $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...
200
            return MigrationSourceCollector::collect();
201
        };
202
203
        $container['migration.runtime'] = static function ($c) {
204
            return new CoreMigrationRuntime($c['dbal'], new NullLogger());
205
        };
206
207
        $container['migration.collection.loader'] = static function ($c) {
208
            $sources = $c['migration.sources'];
209
210
            return new CoreMigrationCollectionLoader($c['dbal'], $c['migration.runtime'], $sources);
211
        };
212
213
        $container['blue.green.deployment.service'] = static function ($c) {
214
            return new BlueGreenDeploymentService($c['dbal']);
215
        };
216
217
        $container['shop.configurator'] = static function ($c) {
218
            return new ShopConfigurator($c['dbal']);
219
        };
220
221
        $container['shop.service'] = static function ($c) {
222
            return new ShopService($c['dbal'], $c['shop.configurator'], $c['shopware.kernel']->getContainer()->get(SalesChannelCreator::class));
223
        };
224
225
        $container['admin.service'] = static function ($c) {
226
            return new AdminService($c['dbal'], $c['shopware.kernel']->getContainer()->get(UserProvisioner::class));
227
        };
228
    }
229
}
230