MetadataSchemaConveyor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 84
ccs 33
cts 33
cp 1
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setTableNaming() 0 5 1
A getEntityClassLocator() 0 17 3
A getTableNaming() 0 3 1
A getMetadataReader() 0 3 1
A getGenerators() 0 4 1
A addMetadataGenerators() 0 21 2
A addEntityPaths() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Conveyor;
6
7
use Cycle\Annotated\Embeddings;
8
use Cycle\Annotated\Entities;
9
use Cycle\Annotated\MergeColumns;
10
use Cycle\Annotated\MergeIndexes;
11
use Cycle\Annotated\TableInheritance;
12
use JetBrains\PhpStorm\ExpectedValues;
13
use Spiral\Attributes\AttributeReader;
14
use Spiral\Attributes\ReaderInterface;
15
use Spiral\Tokenizer\ClassLocator;
16
use Symfony\Component\Finder\Finder;
17
use Yiisoft\Aliases\Aliases;
18
use Yiisoft\Yii\Cycle\Exception\EmptyEntityPathsException;
19
use Yiisoft\Yii\Cycle\Schema\SchemaConveyorInterface as Conveyor;
20
21
class MetadataSchemaConveyor extends SchemaConveyor
22
{
23
    /** @var string[] */
24
    private array $entityPaths = [];
25
26
    private int $tableNaming = Entities::TABLE_NAMING_SINGULAR;
27
28
    private bool $isAddedMetadataGenerators = false;
29
30 3
    final public function setTableNaming(
31
        #[ExpectedValues(valuesFromClass: Entities::class)]
32
        int $type
33
    ): void {
34 3
        $this->tableNaming = $type;
35
    }
36
37 4
    final public function getTableNaming(): int
38
    {
39 4
        return $this->tableNaming;
40
    }
41
42
    /**
43
     * @param string[] $paths
44
     */
45 15
    final public function addEntityPaths(array $paths): void
46
    {
47 15
        $this->entityPaths = array_merge($this->entityPaths, $paths);
48
    }
49
50 11
    public function getGenerators(): array
51
    {
52 11
        $this->addMetadataGenerators();
53 10
        return parent::getGenerators();
54
    }
55
56 10
    protected function getMetadataReader(): ?ReaderInterface
57
    {
58 10
        return new AttributeReader();
59
    }
60
61
    /**
62
     * Add some generators in this conveyor into the INDEX stage
63
     * Added generators will search for entity classes and read their annotations
64
     */
65 11
    private function addMetadataGenerators(): void
66
    {
67 11
        if ($this->isAddedMetadataGenerators) {
68 1
            return;
69
        }
70 11
        $classLocator = $this->getEntityClassLocator();
71
72 10
        $reader = $this->getMetadataReader();
73
74
        // register embeddable entities
75 10
        $this->conveyor[Conveyor::STAGE_INDEX][] = new Embeddings($classLocator, $reader);
76
        // register annotated entities
77 10
        $this->conveyor[Conveyor::STAGE_INDEX][] = new Entities($classLocator, $reader, $this->tableNaming);
78
        // register STI/JTI
79 10
        $this->conveyor[Conveyor::STAGE_INDEX][] = new TableInheritance($reader);
80
        // add @Table(columns) declarations
81 10
        $this->conveyor[Conveyor::STAGE_INDEX][] = new MergeColumns($reader);
82
        // add @Table(indexes) declarations
83 10
        $this->conveyor[Conveyor::STAGE_RENDER][] = new MergeIndexes($reader);
84
85 10
        $this->isAddedMetadataGenerators = true;
86
    }
87
88 11
    private function getEntityClassLocator(): ClassLocator
89
    {
90 11
        $aliases = $this->container->get(Aliases::class);
91 11
        $list = [];
92 11
        foreach ($this->entityPaths as $path) {
93 10
            $list[] = $aliases->get($path);
94
        }
95
96 11
        if (!count($list)) {
97 1
            throw new EmptyEntityPathsException();
98
        }
99
100 10
        $finder = (new Finder())
101 10
            ->files()
102 10
            ->in($list);
103
104 10
        return new ClassLocator($finder);
105
    }
106
}
107