Test Setup Failed
Push — master ( 32c7c7...b19ce7 )
by Josh
03:11
created

SeedMaker::makeMediaSourceSeeds()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 30
rs 9.6
c 0
b 0
f 0
cc 3
nc 4
nop 4
1
<?php
2
3
namespace LCI\Blend;
4
5
use Exception;
6
use LCI\Blend\Blendable\Chunk;
7
use LCI\Blend\Blendable\Context;
8
use LCI\Blend\Blendable\MediaSource;
9
use LCI\Blend\Blendable\Plugin;
10
use LCI\Blend\Blendable\Resource;
11
use LCI\Blend\Blendable\Snippet;
12
use LCI\Blend\Blendable\Template;
13
use LCI\Blend\Helpers\Format;
14
use LCI\Blend\Migrations\MigrationsCreator;
15
use LCI\Blend\Model\xPDO\BlendMigrations;
16
17
/**
18
 * Class SeedMaker
19
 * @package LCI\Blend
20
 */
21
class SeedMaker
22
{
23
    /** @var  \modx */
0 ignored issues
show
Bug introduced by
The type modx 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...
24
    protected $modx;
25
26
    /** @var Blender */
27
    protected $blender;
28
29
    protected $format;
30
31
    /**
32
     * SeedMaker constructor.
33
     * @param \modx $modx
34
     * @param Blender $blender
35
     */
36
    public function __construct(\modx $modx, Blender $blender)
37
    {
38
        $this->modx = $modx;
39
        $this->blender = $blender;
40
41
        $this->format = new Format($this->blender->getSeedsDir());
42
    }
43
44
// Find: ->makeChunkSeeds( Replace: ->getSeedMaker()->makeChunkSeeds(
45
    /**
46
     * @param \xPDOQuery|array|null $criteria
0 ignored issues
show
Bug introduced by
The type xPDOQuery 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...
47
     * @param string $server_type
48
     * @param string $name
49
     * @param bool $create_migration_file
50
     *
51
     * @return array
52
     */
53
    public function makeChunkSeeds($criteria, $server_type='master', $name=null, $create_migration_file=true)
54
    {
55
        $keys = [];
56
        $collection = $this->modx->getCollection('modChunk', $criteria);
57
58
        foreach ($collection as $chunk) {
59
            /** @var \LCI\Blend\Blendable\Chunk $blendChunk */
60
            $blendChunk = new Chunk($this->modx, $this->blender, $chunk->get('name'));
61
            $seed_key = $blendChunk
62
                ->setSeedsDir($this->format->getMigrationName('chunk', $name))
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $name of LCI\Blend\Helpers\Format::getMigrationName() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

62
                ->setSeedsDir($this->format->getMigrationName('chunk', /** @scrutinizer ignore-type */ $name))
Loading history...
63
                ->seed();
64
            $this->blender->out("Chunk: ".$chunk->get('name').' Key: '.$seed_key);
65
            $keys[] = $seed_key;
66
        }
67
68
        if ($create_migration_file) {
69
            /** @var MigrationsCreator $migrationCreator */
70
            $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys);
71
72
            $migrationCreator
73
                ->setPathTimeStamp($this->format->getPathTimeStamp())
74
                ->setName($name)
75
                ->setDescription('')
76
                ->setServerType($server_type)
77
                ->setMigrationsPath($this->blender->getMigrationPath())
78
                ->createChunkMigrationClassFile();
79
80
            $this->logCreatedSeedMigration($migrationCreator->getLogData());
81
        }
82
        return $keys;
83
    }
84
85
    /**
86
     * @param \xPDOQuery|array|null $criteria
87
     * @param string $server_type
88
     * @param string $name
89
     * @param bool $create_migration_file
90
     *
91
     * @return array
92
     */
93
    public function makeContextSeeds($criteria, $server_type='master', $name=null, $create_migration_file=true)
94
    {
95
        $keys = [];
96
        $collection = $this->modx->getCollection('modContext', $criteria);
97
98
        foreach ($collection as $context) {
99
            /** @var Context $blendContext */
100
            $blendContext = new Context($this->modx, $this->blender, $context->get('key'));
101
            $seed_key = $blendContext
102
                ->setSeedsDir($this->format->getMigrationName('context', $name))
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $name of LCI\Blend\Helpers\Format::getMigrationName() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

102
                ->setSeedsDir($this->format->getMigrationName('context', /** @scrutinizer ignore-type */ $name))
Loading history...
103
                ->seed();
104
            $this->blender->out("Context: ".$context->get('name').' Key: '.$seed_key);
105
            $keys[] = $seed_key;
106
        }
107
108
        if ($create_migration_file) {
109
            /** @var MigrationsCreator $migrationCreator */
110
            $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys);
111
112
            $migrationCreator
113
                ->setPathTimeStamp($this->format->getPathTimeStamp())
114
                ->setName($name)
115
                ->setDescription('')
116
                ->setServerType($server_type)
117
                ->setMigrationsPath($this->blender->getMigrationPath())
118
                ->createContextMigrationClassFile();
119
120
            $this->logCreatedSeedMigration($migrationCreator->getLogData());
121
        }
122
        return $keys;
123
    }
124
125
    /**
126
     * @param \xPDOQuery|array|null $criteria
127
     * @param string $server_type
128
     * @param string $name
129
     * @param bool $create_migration_file
130
     *
131
     * @return array
132
     */
133
    public function makeMediaSourceSeeds($criteria, $server_type='master', $name=null, $create_migration_file=true)
134
    {
135
        $keys = [];
136
        $collection = $this->modx->getCollection('modMediaSource', $criteria);
137
138
        foreach ($collection as $mediaSource) {
139
            /** @var MediaSource $blendMediaSource */
140
            $blendMediaSource = new MediaSource($this->modx, $this->blender, $mediaSource->get('name'));
141
            $seed_key = $blendMediaSource
142
                ->setSeedsDir($this->format->getMigrationName('mediaSource', $name))
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $name of LCI\Blend\Helpers\Format::getMigrationName() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

142
                ->setSeedsDir($this->format->getMigrationName('mediaSource', /** @scrutinizer ignore-type */ $name))
Loading history...
143
                ->seed();
144
            $this->blender->out("Media Source: ".$mediaSource->get('name').' Key: '.$seed_key);
145
            $keys[] = $seed_key;
146
        }
147
148
        if ($create_migration_file) {
149
            /** @var MigrationsCreator $migrationCreator */
150
            $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys);
151
152
            $migrationCreator
153
                ->setPathTimeStamp($this->format->getPathTimeStamp())
154
                ->setName($name)
155
                ->setDescription('')
156
                ->setServerType($server_type)
157
                ->setMigrationsPath($this->blender->getMigrationPath())
158
                ->createMediaSourceMigrationClassFile();
159
160
            $this->logCreatedSeedMigration($migrationCreator->getLogData());
161
        }
162
        return $keys;
163
    }
164
165
    /**
166
     * @param \xPDOQuery|array|null $criteria
167
     * @param string $server_type
168
     * @param string $name
169
     * @param bool $create_migration_file
170
     *
171
     * @return array
172
     */
173
    public function makePluginSeeds($criteria, $server_type='master', $name=null, $create_migration_file=true)
174
    {
175
        $keys = [];
176
        $collection = $this->modx->getCollection('modPlugin', $criteria);
177
178
        foreach ($collection as $plugin) {
179
            /** @var \LCI\Blend\Blendable\Plugin $blendPlugin */
180
            $blendPlugin = new Plugin($this->modx, $this->blender, $plugin->get('name'));
181
            $seed_key = $blendPlugin
182
                ->setSeedsDir($this->format->getMigrationName('plugin', $name))
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $name of LCI\Blend\Helpers\Format::getMigrationName() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

182
                ->setSeedsDir($this->format->getMigrationName('plugin', /** @scrutinizer ignore-type */ $name))
Loading history...
183
                ->seed();
184
            $this->blender->out("Plugin: ".$plugin->get('name').' Key: '.$seed_key);
185
            $keys[] = $seed_key;
186
        }
187
188
        if ($create_migration_file) {
189
            /** @var MigrationsCreator $migrationCreator */
190
            $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys);
191
192
            $migrationCreator
193
                ->setPathTimeStamp($this->format->getPathTimeStamp())
194
                ->setName($name)
195
                ->setDescription('')
196
                ->setServerType($server_type)
197
                ->setMigrationsPath($this->blender->getMigrationPath())
198
                ->createPluginMigrationClassFile();
199
200
            $this->logCreatedSeedMigration($migrationCreator->getLogData());
201
        }
202
        return $keys;
203
    }
204
205
    /**
206
     * @param \xPDOQuery|array|null $criteria
207
     * @param string $server_type
208
     * @param string $name
209
     * @param bool $create_migration_file
210
     *
211
     * @return array
212
     */
213
    public function makeResourceSeeds($criteria, $server_type='master', $name=null, $create_migration_file=true)
214
    {
215
        $keys = [
216
            'web' => []
217
        ];
218
219
        $collection = $this->modx->getCollection('modResource', $criteria);
220
        foreach ($collection as $resource) {
221
            $blendResource = new Resource($this->modx, $this->blender, $resource->get('alias'), $resource->get('context_key'));
222
            $seed_key = $blendResource
223
                ->setSeedsDir($this->format->getMigrationName('resource', $name))
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $name of LCI\Blend\Helpers\Format::getMigrationName() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

223
                ->setSeedsDir($this->format->getMigrationName('resource', /** @scrutinizer ignore-type */ $name))
Loading history...
224
                ->seed($resource);
225
            $this->blender->out("ID: ".$resource->get('id').' Key: '.$seed_key);
226
227
            if (!isset($keys[$resource->get('context_key')])) {
228
                $keys[$resource->get('context_key')] = [];
229
            }
230
231
            $keys[$resource->get('context_key')][] = $seed_key;
232
        }
233
234
        if ($create_migration_file) {
235
            /** @var MigrationsCreator $migrationCreator */
236
            $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys);
237
238
            $migrationCreator
239
                ->setPathTimeStamp($this->format->getPathTimeStamp())
240
                ->setName($name)
241
                ->setDescription('')
242
                ->setServerType($server_type)
243
                ->setMigrationsPath($this->blender->getMigrationPath())
244
                ->createResourceMigrationClassFile();
245
246
            $this->logCreatedSeedMigration($migrationCreator->getLogData());
247
        }
248
        return $keys;
249
    }
250
251
    /**
252
     * @param \xPDOQuery|array|null $criteria
253
     * @param string $server_type
254
     * @param string $name
255
     * @param bool $create_migration_file
256
     *
257
     * @return array
258
     */
259
    public function makeSnippetSeeds($criteria, $server_type='master', $name=null, $create_migration_file=true)
260
    {
261
        $keys = [];
262
        $collection = $this->modx->getCollection('modSnippet', $criteria);
263
264
        foreach ($collection as $snippet) {
265
            /** @var \LCI\Blend\Blendable\Snippet $blendSnippet */
266
            $blendSnippet = new Snippet($this->modx, $this->blender, $snippet->get('name'));
267
            $seed_key = $blendSnippet
268
                ->setSeedsDir($this->format->getMigrationName('snippet', $name))
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $name of LCI\Blend\Helpers\Format::getMigrationName() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

268
                ->setSeedsDir($this->format->getMigrationName('snippet', /** @scrutinizer ignore-type */ $name))
Loading history...
269
                ->seed();
270
            $this->blender->out("Snippet: ".$snippet->get('name').' Key: '.$seed_key);
271
            $keys[] = $seed_key;
272
        }
273
274
        if ($create_migration_file) {
275
            /** @var MigrationsCreator $migrationCreator */
276
            $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys);
277
278
            $migrationCreator
279
                ->setPathTimeStamp($this->format->getPathTimeStamp())
280
                ->setName($name)
281
                ->setDescription('')
282
                ->setServerType($server_type)
283
                ->setMigrationsPath($this->blender->getMigrationPath())
284
                ->createSnippetMigrationClassFile();
285
286
            $this->logCreatedSeedMigration($migrationCreator->getLogData());
287
        }
288
        return $keys;
289
    }
290
291
    /**
292
     * @param \xPDOQuery|array|null $criteria
293
     * @param string $server_type
294
     * @param string $name
295
     * @param bool $create_migration_file
296
     *
297
     * @return array
298
     */
299
    public function makeSystemSettingSeeds($criteria, $server_type='master', $name=null, $create_migration_file=true)
300
    {
301
        $collection = $this->modx->getCollection('modSystemSetting', $criteria);
302
303
        $setting_data = [];
304
        foreach ($collection as $setting) {
305
            /** @var \LCI\Blend\Blendable\SystemSetting $blendableSetting */
306
            $blendableSetting = $this->blender->getBlendableSystemSetting($setting->get('key'));
307
            $setting_data[] = $blendableSetting->seedToArray();
308
        }
309
310
        // https://docs.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.invokeevent
311
        $this->modx->invokeEvent(
312
            'OnBlendSeedSystemSettings',
313
            [
314
                'blender' => $this->blender,
315
                'data' => &$setting_data
316
            ]
317
        );
318
319
        if ($create_migration_file) {
320
            /** @var MigrationsCreator $migrationCreator */
321
            $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $setting_data);
322
323
            $migrationCreator
324
                ->setPathTimeStamp($this->format->getPathTimeStamp())
325
                ->setName($name)
326
                ->setDescription('')
327
                ->setServerType($server_type)
328
                ->setMigrationsPath($this->blender->getMigrationPath())
329
                ->createSystemSettingsMigrationClassFile();
330
331
            $this->logCreatedSeedMigration($migrationCreator->getLogData());
332
        }
333
        return $setting_data;
334
    }
335
336
    /**
337
     * @param \xPDOQuery|array|null $criteria
338
     * @param string $server_type
339
     * @param string $name
340
     * @param bool $create_migration_file
341
     *
342
     * @return array
343
     */
344
    public function makeTemplateSeeds($criteria, $server_type='master', $name=null, $create_migration_file=true)
345
    {
346
        $keys = [];
347
        $collection = $this->modx->getCollection('modTemplate', $criteria);
348
349
        foreach ($collection as $template) {
350
            //exit();
351
            /** @var \LCI\Blend\Blendable\Template $blendTemplate */
352
            $blendTemplate = new Template($this->modx, $this->blender, $template->get('templatename'));
353
            $seed_key = $blendTemplate
354
                ->setSeedsDir($this->format->getMigrationName('template', $name))
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type string; however, parameter $name of LCI\Blend\Helpers\Format::getMigrationName() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

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

354
                ->setSeedsDir($this->format->getMigrationName('template', /** @scrutinizer ignore-type */ $name))
Loading history...
355
                ->seed();
356
            $this->blender->out("Template ID: ".$template->get('id').' Key: '.$seed_key);
357
            $keys[] = $seed_key;
358
        }
359
360
        if ($create_migration_file) {
361
            /** @var MigrationsCreator $migrationCreator */
362
            $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys);
363
364
            $migrationCreator
365
                ->setPathTimeStamp($this->format->getPathTimeStamp())
366
                ->setName($name)
367
                ->setDescription('')
368
                ->setServerType($server_type)
369
                ->setMigrationsPath($this->blender->getMigrationPath())
370
                ->createTemplateMigrationClassFile();
371
372
            $this->logCreatedSeedMigration($migrationCreator->getLogData());
373
        }
374
        return $keys;
375
    }
376
377
    /**
378
     * @param string $server_type
379
     * @param null|string $name
380
     */
381
    public function makeSiteSeed($server_type='master', $name=null)
382
    {
383
        $site_data = [
384
            'mediaSources' => $this->makeMediaSourceSeeds(null, $server_type, $name, false),
385
            'contexts' => $this->makeContextSeeds(null, $server_type, $name, false),
386
            'chunks' => $this->makeChunkSeeds(null, $server_type, $name, false),
387
            'plugins' => $this->makePluginSeeds(null, $server_type, $name, false),
388
            'resources' => $this->makeResourceSeeds(null, $server_type, $name, false),
389
            'snippets' => $this->makeSnippetSeeds(null, $server_type, $name, false),
390
            'systemSettings' => $this->makeSystemSettingSeeds(null, $server_type, $name, false),
391
            'templates' => $this->makeTemplateSeeds(null, $server_type, $name, false)
392
        ];
393
394
        /** @var MigrationsCreator $migrationCreator */
395
        $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $site_data);
396
397
        $migrationCreator
398
            ->setPathTimeStamp($this->format->getPathTimeStamp())
399
            ->setName($name)
400
            ->setDescription('')
401
            ->setServerType($server_type)
402
            ->setMigrationsPath($this->blender->getMigrationPath())
403
            ->createSiteMigrationClassFile();
404
405
        $this->logCreatedSeedMigration($migrationCreator->getLogData());
406
    }
407
408
    /**
409
     * @param array $data
410
     */
411
    protected function logCreatedSeedMigration($data=[])
412
    {
413
        try {
414
            /** @var BlendMigrations $migration */
415
            $migration = $this->modx->newObject($this->blender->getBlendClassObject());
416
            if ($migration) {
0 ignored issues
show
introduced by
$migration is of type LCI\Blend\Model\xPDO\BlendMigrations, thus it always evaluated to true. If $migration can have other possible types, add them to src/SeedMaker.php:414
Loading history...
417
                $migration->fromArray($data);
418
                $migration->save();
419
            }
420
        } catch (Exception $exception) {
421
            $this->blender->out($exception->getMessage(), true);
422
        }
423
    }
424
}