Passed
Push — master ( 75a4a8...8d5c50 )
by Alexander
16:28 queued 08:44
created

AnnotatedSchemaConveyor::getGenerators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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 Doctrine\Common\Annotations\AnnotationRegistry;
12
use Spiral\Tokenizer\ClassLocator;
13
use Symfony\Component\Finder\Finder;
14
use Yiisoft\Aliases\Aliases;
15
use Yiisoft\Yii\Cycle\Exception\EmptyEntityPathsException;
16
17
final class AnnotatedSchemaConveyor extends SchemaConveyor
18
{
19
    /** @var string[] */
20
    private array $entityPaths = [];
21
22
    private int $tableNaming = Entities::TABLE_NAMING_SINGULAR;
23
24
    private bool $isAddedAnnotated = false;
25
26 3
    public function setTableNaming(int $type): void
27
    {
28 3
        $this->tableNaming = $type;
29 3
    }
30
31 4
    public function getTableNaming(): int
32
    {
33 4
        return $this->tableNaming;
34
    }
35
36
    /**
37
     * @param string[] $paths
38
     */
39 13
    public function addEntityPaths(array $paths): void
40
    {
41 13
        $this->entityPaths = array_merge($this->entityPaths, $paths);
42 13
    }
43
44 9
    public function getGenerators(): array
45
    {
46 9
        $this->addAnnotatedGenerators();
47 8
        return parent::getGenerators();
48
    }
49
50
    /**
51
     * Add some generators in this conveyor into the INDEX stage
52
     * Added generators will search for entity classes and read their annotations
53
     */
54 9
    private function addAnnotatedGenerators(): void
55
    {
56 9
        if ($this->isAddedAnnotated) {
57 1
            return;
58
        }
59
        // autoload annotations
60 9
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

60
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
61
62 9
        $this->isAddedAnnotated = true;
63 9
        $classLocator = $this->getEntityClassLocator();
64
65
        // register embeddable entities
66 8
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Embeddings($classLocator);
67
        // register annotated entities
68 8
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Entities($classLocator, null, $this->tableNaming);
69
        // add @Table(columns) declarations
70 8
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = MergeColumns::class;
71
        // add @Table(indexes) declarations
72 8
        $this->conveyor[SchemaConveyor::STAGE_RENDER][] = MergeIndexes::class;
73 8
    }
74
75 9
    private function getEntityClassLocator(): ClassLocator
76
    {
77 9
        $aliases = $this->container->get(Aliases::class);
78 9
        $list = [];
79 9
        foreach ($this->entityPaths as $path) {
80 8
            $list[] = $aliases->get($path);
81
        }
82
83 9
        if (!count($list)) {
84 1
            throw new EmptyEntityPathsException();
85
        }
86
87 8
        $finder = (new Finder())
88 8
            ->files()
89 8
            ->in($list);
90
91 8
        return new ClassLocator($finder);
92
    }
93
}
94