Passed
Push — 3.x ( 080893...4e5cb3 )
by Aleksei
09:48
created

MergeColumns   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 70
ccs 28
cts 32
cp 0.875
rs 10
c 1
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 21 5
B copy() 0 32 7
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Annotated;
6
7
use Cycle\Annotated\Annotation\Column;
8
use Cycle\Annotated\Annotation\Table;
9
use Cycle\Annotated\Exception\AnnotationException;
10
use Cycle\Schema\Definition\Entity as EntitySchema;
11
use Cycle\Schema\GeneratorInterface;
12
use Cycle\Schema\Registry;
13
use Doctrine\Common\Annotations\Reader as DoctrineReader;
14
use Spiral\Attributes\ReaderInterface;
15
16
/**
17
 * Copy column definitions from Mapper/Repository to Entity.
18
 */
19
final class MergeColumns implements GeneratorInterface
20
{
21
    private ReaderInterface $reader;
22
23
    private Configurator $generator;
24
25 440
    public function __construct(DoctrineReader|ReaderInterface $reader = null)
26
    {
27 440
        $this->reader = ReaderFactory::create($reader);
28 440
        $this->generator = new Configurator($this->reader);
29 440
    }
30
31 416
    public function run(Registry $registry): Registry
32
    {
33 416
        foreach ($registry as $e) {
34 416
            if ($e->getClass() === null || !$registry->hasTable($e)) {
35 24
                continue;
36
            }
37
38 416
            $this->copy($e, $e->getClass());
39
40
            // copy from related classes
41 413
            $this->copy($e, $e->getMapper());
42 413
            $this->copy($e, $e->getRepository());
43 413
            $this->copy($e, $e->getSource());
44 413
            $this->copy($e, $e->getScope());
45
46 413
            foreach ($registry->getChildren($e) as $child) {
47 268
                $this->copy($e, $child->getClass());
48
            }
49
        }
50
51 413
        return $registry;
52
    }
53
54
    /**
55
     * @param class-string|null  $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|null.
Loading history...
56
     */
57 416
    private function copy(EntitySchema $e, ?string $class): void
58
    {
59 416
        if ($class === null) {
60 413
            return;
61
        }
62
63
        try {
64 416
            $class = new \ReflectionClass($class);
65
        } catch (\ReflectionException) {
66
            return;
67
        }
68
69
        try {
70
            /** @var Table|null $tableMeta */
71 416
            $tableMeta = $this->reader->firstClassMetadata($class, Table::class);
72
            /** @var Column[] $columnsMeta */
73 416
            $columnsMeta = $this->reader->getClassMetadata($class, Column::class);
74
        } catch (\Exception $e) {
75
            throw new AnnotationException($e->getMessage(), $e->getCode(), $e);
76
        }
77
78 416
        $columns = $tableMeta === null ? [] : $tableMeta->getColumns();
79 416
        foreach ($columnsMeta as $column) {
80 217
            $columns[] = $column;
81
        }
82
83 416
        if ($columns === []) {
84 388
            return;
85
        }
86
87
        // additional columns (mapped to local fields automatically)
88 236
        $this->generator->initColumns($e, $columns, $class);
89 233
    }
90
}
91