Passed
Push — master ( ae25c0...fd0368 )
by Anton
01:32
created

TableGenerator::compute()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 7
nop 2
dl 0
loc 22
rs 9.6111
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\Generator;
11
12
use Cycle\Schema\Definition\Entity;
13
use Cycle\Schema\Generator\Table\ColumnSchema;
14
use Cycle\Schema\GeneratorInterface;
15
use Cycle\Schema\Registry;
16
17
/**
18
 * Generate table columns based on entity definition.
19
 */
20
class TableGenerator implements GeneratorInterface
21
{
22
    /**
23
     * Generate table schema based on given entity definition.
24
     *
25
     * @param Registry $registry
26
     * @param Entity   $entity
27
     */
28
    public function compute(Registry $registry, Entity $entity)
29
    {
30
        if (!$registry->hasTable($entity)) {
31
            // do not render entities without associated table
32
            return;
33
        }
34
35
        $table = $registry->getTableSchema($entity);
36
37
        $primaryKeys = [];
38
        foreach ($entity->getFields() as $field) {
39
            $column = ColumnSchema::parse($field);
40
41
            if ($column->isPrimary()) {
42
                $primaryKeys[] = $field->getColumn();
43
            }
44
45
            $column->render($table->column($field->getColumn()));
46
        }
47
48
        if (count($primaryKeys)) {
49
            $table->setPrimaryKeys($primaryKeys);
50
        }
51
    }
52
}