Passed
Push — master ( 5c3f46...c58a59 )
by Anton
02:02
created

SchemaBootloader::relationGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
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\Bootloader\DependedInterface;
18
use Spiral\Boot\MemoryInterface;
19
use Spiral\Bootloader\TokenizerBootloader;
20
use Spiral\Config\ConfiguratorInterface;
21
use Spiral\Core\Container;
22
23
final class SchemaBootloader extends Bootloader implements DependedInterface, Container\SingletonInterface
24
{
25
    public const GROUP_INDEX       = 'index';
26
    public const GROUP_RENDER      = 'render';
27
    public const GROUP_POSTPROCESS = 'postprocess';
28
29
    public const BINDINGS = [
30
        SchemaInterface::class             => [self::class, 'schema'],
31
        Generator\GenerateRelations::class => [self::class, 'relationGenerator'],
32
    ];
33
34
    /** @var Container */
35
    private $container;
36
37
    /** @var ConfiguratorInterface */
38
    private $generators = [];
39
40
    /**
41
     * CycleSchemaBootloader constructor.
42
     *
43
     * @param Container $container
44
     */
45
    public function __construct(Container $container)
46
    {
47
        $this->container = $container;
48
        $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...
49
            self::GROUP_INDEX       => [
50
                // find available entities
51
            ],
52
            self::GROUP_RENDER      => [
53
                // render tables and relations
54
                Generator\ResetTables::class,
55
                Generator\GenerateRelations::class,
56
                Generator\ValidateEntities::class,
57
                Generator\RenderTables::class,
58
                Generator\RenderRelations::class,
59
            ],
60
            self::GROUP_POSTPROCESS => [
61
                // post processing
62
                Generator\GenerateTypecast::class
63
            ],
64
        ];
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function defineDependencies(): array
71
    {
72
        return [
73
            TokenizerBootloader::class
74
        ];
75
    }
76
77
    /**
78
     * @param string $group
79
     * @param mixed  $generator
80
     */
81
    public function addGenerator(string $group, $generator)
82
    {
83
        $this->generators[$group][] = $generator;
84
    }
85
86
    /**
87
     * @return GeneratorInterface[]
88
     */
89
    public function getGenerators(): array
90
    {
91
        $result = [];
92
        foreach ($this->generators as $group) {
93
            foreach ($group as $generator) {
94
                if (is_object($generator) && !$generator instanceof Container\Autowire) {
95
                    $result[] = $generator;
96
                } else {
97
                    $result[] = $this->container->get($generator);
98
                }
99
            }
100
        }
101
102
        return $result;
103
    }
104
105
    /**
106
     * @param MemoryInterface $memory
107
     * @return SchemaInterface|null
108
     */
109
    protected function schema(MemoryInterface $memory): ?SchemaInterface
110
    {
111
        $schema = $memory->loadData('cycle');
112
        if (is_null($schema)) {
113
            return null;
114
        }
115
116
        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

116
        return new Schema(/** @scrutinizer ignore-type */ $schema);
Loading history...
117
    }
118
119
    /**
120
     * @return Generator\GenerateRelations
121
     */
122
    protected function relationGenerator(): Generator\GenerateRelations
123
    {
124
        return new Generator\GenerateRelations();
125
    }
126
}