Passed
Push — master ( 8926af...beb5fc )
by Anton
02:16
created

SchemaBootloader::getGenerators()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 9.6111
cc 5
nc 4
nop 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Bootloader\Cycle;
11
12
use Cycle\ORM\Schema;
13
use Cycle\ORM\SchemaInterface;
14
use Cycle\Schema\Generator;
15
use Cycle\Schema\GeneratorInterface;
16
use Spiral\Boot\Bootloader\Bootloader;
17
use Spiral\Boot\MemoryInterface;
18
use Spiral\Bootloader\TokenizerBootloader;
19
use Spiral\Config\ConfiguratorInterface;
20
use Spiral\Core\Container;
21
22
final class SchemaBootloader extends Bootloader implements Container\SingletonInterface
23
{
24
    public const GROUP_INDEX       = 'index';
25
    public const GROUP_RENDER      = 'render';
26
    public const GROUP_POSTPROCESS = 'postprocess';
27
28
    public const DEPENDENCIES = [
29
        TokenizerBootloader::class
30
    ];
31
32
    const BINDINGS = [
33
        SchemaInterface::class             => [self::class, 'schema'],
34
        Generator\GenerateRelations::class => [self::class, 'relationGenerator'],
35
    ];
36
37
    /** @var Container */
38
    private $container;
39
40
    /** @var ConfiguratorInterface */
41
    private $generators = [];
42
43
    /**
44
     * CycleSchemaBootloader constructor.
45
     *
46
     * @param Container $container
47
     */
48
    public function __construct(Container $container)
49
    {
50
        $this->container = $container;
51
        $this->generators = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array(self::GROUP_INDEX ...nerateTypecast::class)) of type array<string,array|array<integer,string>> is incompatible with the declared type Spiral\Config\ConfiguratorInterface of property $generators.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
            self::GROUP_INDEX       => [
53
                // find available entities
54
            ],
55
            self::GROUP_RENDER      => [
56
                // render tables and relations
57
                Generator\ResetTables::class,
58
                Generator\GenerateRelations::class,
59
                Generator\ValidateEntities::class,
60
                Generator\RenderTables::class,
61
                Generator\RenderRelations::class,
62
            ],
63
            self::GROUP_POSTPROCESS => [
64
                // post processing
65
                Generator\GenerateTypecast::class
66
            ],
67
        ];
68
    }
69
70
    /**
71
     * @param string $group
72
     * @param mixed  $generator
73
     */
74
    public function addGenerator(string $group, $generator)
75
    {
76
        $this->generators[$group][] = $generator;
77
    }
78
79
    /**
80
     * @return GeneratorInterface[]
81
     */
82
    public function getGenerators(): array
83
    {
84
        $result = [];
85
        foreach ($this->generators as $group) {
86
            foreach ($group as $generator) {
87
                if (is_object($generator) && !$generator instanceof Container\Autowire) {
88
                    $result[] = $generator;
89
                } else {
90
                    $result[] = $this->container->get($generator);
91
                }
92
            }
93
        }
94
95
        return $result;
96
    }
97
98
    /**
99
     * @param MemoryInterface $memory
100
     * @return SchemaInterface|null
101
     */
102
    protected function schema(MemoryInterface $memory): ?SchemaInterface
103
    {
104
        $schema = $memory->loadData('cycle');
105
        if (is_null($schema)) {
106
            return null;
107
        }
108
109
        return new Schema($schema);
0 ignored issues
show
Bug introduced by
It seems like $schema can also be of type string; however, parameter $schema of Cycle\ORM\Schema::__construct() does only seem to accept array, 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

109
        return new Schema(/** @scrutinizer ignore-type */ $schema);
Loading history...
110
    }
111
112
    /**
113
     * @return Generator\GenerateRelations
114
     */
115
    protected function relationGenerator(): Generator\GenerateRelations
116
    {
117
        return new Generator\GenerateRelations();
118
    }
119
}
120