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

TableGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A compute() 0 22 5
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
}