Passed
Push — master ( c1a29b...ad038c )
by Anton
01:46
created

Compiler::renderRelations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\Schema;
11
12
use Cycle\ORM\Schema;
13
use Cycle\Schema\Definition\Entity;
14
use Cycle\Schema\Exception\BuilderException;
15
16
final class Compiler implements ProcessorInterface
17
{
18
    /** @var array */
19
    private $result = [];
20
21
    /**
22
     * Get compiled schema result.
23
     *
24
     * @return array
25
     */
26
    public function getResult(): array
27
    {
28
        return $this->result;
29
    }
30
31
    /**
32
     * Compile entity and relation definitions into packed ORM schema.
33
     *
34
     * @param Registry $registry
35
     * @param Entity   $entity
36
     */
37
    public function compute(Registry $registry, Entity $entity)
38
    {
39
        $schema = [
40
            Schema::ENTITY       => $entity->getClass(),
41
            Schema::SOURCE       => $entity->getSource(),
42
            Schema::MAPPER       => $entity->getMapper(),
43
            Schema::REPOSITORY   => $entity->getRepository(),
44
            Schema::CONSTRAIN    => $entity->getConstrain(),
45
            Schema::COLUMNS      => $this->renderColumns($entity),
46
            Schema::FIND_BY_KEYS => $this->renderReferences($entity),
47
            Schema::TYPECAST     => $this->renderTypecast($entity),
48
            Schema::RELATIONS    => $this->renderRelations($registry, $entity)
49
        ];
50
51
        if ($registry->hasTable($entity)) {
52
            $primaryKeys = $registry->getTableSchema($entity)->getPrimaryKeys();
53
            if (count($primaryKeys) !== 1) {
54
                throw new BuilderException("Entity `{$entity->getRole()}` must define exactly one primary key");
55
            }
56
57
            $schema[Schema::DATABASE] = $registry->getDatabase($entity);
58
            $schema[Schema::TABLE] = $registry->getTable($entity);
59
            $schema[Schema::PRIMARY_KEY] = current($primaryKeys);
60
        }
61
62
        // table inheritance
63
        foreach ($registry->getChildren($entity) as $child) {
64
            $this->result[$child->getClass()] = [Schema::ROLE => $entity->getRole()];
65
            $schema[Schema::CHILDREN][] = $child->getClass();
66
        }
67
68
        ksort($schema);
69
        $this->result[$entity->getRole()] = $schema;
70
    }
71
72
    /**
73
     * @param Entity $entity
74
     * @return array
75
     */
76
    protected function renderColumns(Entity $entity): array
77
    {
78
        $schema = [];
79
        foreach ($entity->getFields() as $name => $field) {
80
            $schema[$name] = $field->getColumn();
81
        }
82
83
        return $schema;
84
    }
85
86
    /**
87
     * @param Entity $entity
88
     * @return array
89
     */
90
    protected function renderTypecast(Entity $entity): array
91
    {
92
        $schema = [];
93
        foreach ($entity->getFields() as $name => $field) {
94
            if ($field->hasTypecast()) {
95
                $schema[$name] = $field->getTypecast();
96
            }
97
        }
98
99
        return $schema;
100
    }
101
102
    /**
103
     * @param Entity $entity
104
     * @return array
105
     */
106
    protected function renderReferences(Entity $entity): array
107
    {
108
        $schema = [];
109
        foreach ($entity->getFields() as $name => $field) {
110
            if ($field->isReferenced()) {
111
                $schema[] = $name;
112
            }
113
        }
114
115
        return $schema;
116
    }
117
118
    /**
119
     * @param Registry $registry
120
     * @param Entity   $entity
121
     * @return array
122
     */
123
    protected function renderRelations(Registry $registry, Entity $entity): array
0 ignored issues
show
Unused Code introduced by
The parameter $entity 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

123
    protected function renderRelations(Registry $registry, /** @scrutinizer ignore-unused */ Entity $entity): array

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...
Unused Code introduced by
The parameter $registry 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

123
    protected function renderRelations(/** @scrutinizer ignore-unused */ Registry $registry, Entity $entity): array

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...
124
    {
125
        return [];
126
    }
127
}