Passed
Push — master ( ee28da...1c3e2f )
by Alexander
14:20 queued 13:06
created

AnnotatedSchemaConveyor::addEntityPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Generator;
4
5
use Cycle\Annotated;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Spiral\Tokenizer\ClassLocator;
8
use Symfony\Component\Finder\Finder;
9
use Yiisoft\Aliases\Aliases;
10
use Yiisoft\Yii\Cycle\Exception\EmptyEntityPathsException;
11
12
final class AnnotatedSchemaConveyor extends SchemaConveyor
13
{
14
    /** @var string[] */
15
    private array $entityPaths = [];
16
17
    private int $tableNaming = Annotated\Entities::TABLE_NAMING_SINGULAR;
18
19
    private bool $isAddedAnnotated = false;
20
21
    public function setTableNaming(int $type): void
22
    {
23
        $this->tableNaming = $type;
24
    }
25
26
    public function getTableNaming(): int
27
    {
28
        return $this->tableNaming;
29
    }
30
31
    /**
32
     * @param string[] $paths
33
     */
34 3
    public function addEntityPaths(array $paths): void
35
    {
36 3
        $this->entityPaths = array_merge($this->entityPaths, $paths);
37
    }
38
39 3
    public function getGenerators(): array
40
    {
41 3
        $this->addAnnotatedGenerators();
42 2
        return parent::getGenerators();
43
    }
44
45
    /**
46
     * Add some generators in this conveyor into the INDEX stage
47
     * Added generators will search for entity classes and read their annotations
48
     */
49 3
    private function addAnnotatedGenerators(): void
50
    {
51 3
        if ($this->isAddedAnnotated) {
52
            return;
53
        }
54
        // autoload annotations
55 3
        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 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists') ( Ignorable by Annotation )

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

55
        /** @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...
56
57 3
        $this->isAddedAnnotated = true;
58 3
        $classLocator = $this->getEntityClassLocator();
59
60
        // register embeddable entities
61 2
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Annotated\Embeddings($classLocator);
62
        // register annotated entities
63 2
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Annotated\Entities($classLocator, null, $this->tableNaming);
64
        // add @Table(columns) declarations
65 2
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = Annotated\MergeColumns::class;
66
        // add @Table(indexes) declarations
67 2
        $this->conveyor[SchemaConveyor::STAGE_RENDER][] = Annotated\MergeIndexes::class;
68
    }
69
70 3
    private function getEntityClassLocator(): ClassLocator
71
    {
72 3
        $aliases = $this->container->get(Aliases::class);
73 3
        $list = [];
74 3
        foreach ($this->entityPaths as $path) {
75 2
            $list[] = $aliases->get($path);
76
        }
77
78 3
        if (!count($list)) {
79 1
            throw new EmptyEntityPathsException();
80
        }
81
82 2
        $finder = (new Finder())
83 2
            ->files()
84 2
            ->in($list);
85
86 2
        return new ClassLocator($finder);
87
    }
88
}
89