Passed
Pull Request — master (#13)
by
unknown
02:44
created

RenderTables::compute()   B

Complexity

Conditions 7
Paths 19

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 3
b 0
f 0
nc 19
nop 2
dl 0
loc 31
rs 8.8333
1
<?php
2
3
/**
4
 * Cycle ORM Schema Builder.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Schema\Generator;
13
14
use Cycle\ORM\Mapper\Mapper;
15
use Cycle\Schema\Definition\Entity;
16
use Cycle\Schema\GeneratorInterface;
17
use Cycle\Schema\Registry;
18
use Cycle\Schema\Table\Column;
19
use Spiral\Database\Schema\AbstractTable;
20
use Spiral\Database\Schema\Reflector;
21
22
/**
23
 * Generate table columns based on entity definition.
24
 */
25
final class RenderTables implements GeneratorInterface
26
{
27
    /** @var Reflector */
28
    private $reflector;
29
30
    /**
31
     * TableGenerator constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->reflector = new Reflector();
36
    }
37
38
    /**
39
     * @param Registry $registry
40
     * @return Registry
41
     */
42
    public function run(Registry $registry): Registry
43
    {
44
        foreach ($registry as $entity) {
45
            $this->compute($registry, $entity);
46
        }
47
48
        return $registry;
49
    }
50
51
    /**
52
     * List of all involved tables sorted in order of their dependency.
53
     *
54
     * @return AbstractTable[]
55
     */
56
    public function getTables(): array
57
    {
58
        return $this->reflector->sortedTables();
59
    }
60
61
    /**
62
     * @return Reflector
63
     */
64
    public function getReflector(): Reflector
65
    {
66
        return $this->reflector;
67
    }
68
69
    /**
70
     * Generate table schema based on given entity definition.
71
     *
72
     * @param Registry $registry
73
     * @param Entity   $entity
74
     */
75
    protected function compute(Registry $registry, Entity $entity): void
76
    {
77
        if (!$registry->hasTable($entity)) {
78
            // do not render entities without associated table
79
            return;
80
        }
81
82
        $table = $registry->getTableSchema($entity);
83
84
        $primaryKeys = [];
85
        foreach ($entity->getFields() as $field) {
86
            $column = Column::parse($field);
87
88
            if ($column->isPrimary()) {
89
                $primaryKeys[] = $field->getColumn();
90
            }
91
92
            $column->render($table->column($field->getColumn()));
93
        }
94
95
        if ($registry->getChildren($entity) !== []) {
96
            if (!$table->hasColumn(Mapper::ENTITY_TYPE)) {
97
                $table->string(Mapper::ENTITY_TYPE, 32);
98
            }
99
        }
100
101
        if (count($primaryKeys)) {
102
            $table->setPrimaryKeys($primaryKeys);
103
        }
104
105
        $this->reflector->addTable($table);
106
    }
107
}
108