Completed
Branch feature/pre-split (ca29cf)
by Anton
03:23
created

SchemaLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 4
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Schemas;
8
9
use Interop\Container\ContainerInterface;
10
use Spiral\Models\Reflections\ReflectionEntity;
11
use Spiral\ORM\Configs\MutatorsConfig;
12
use Spiral\ORM\Entities\RecordSource;
13
use Spiral\ORM\Record;
14
use Spiral\Tokenizer\ClassesInterface;
15
16
/**
17
 * Provides ability to automatically locate schemas in a project. Can be user redefined in order to
18
 * automatically include custom classes.
19
 */
20 View Code Duplication
class SchemaLocator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    /**
23
     * Container is used for lazy resolution for ClassesInterface.
24
     *
25
     * @var ContainerInterface
26
     */
27
    protected $container;
28
29
    /**
30
     * @param ContainerInterface $container
31
     */
32
    public function __construct(ContainerInterface $container)
33
    {
34
        $this->container = $container;
35
    }
36
37
    /**
38
     * Locate all available document schemas in a project.
39
     *
40
     * @return SchemaInterface[]
41
     */
42
    public function locateSchemas(): array
43
    {
44
        if (!$this->container->has(ClassesInterface::class)) {
45
            return [];
46
        }
47
48
        /**
49
         * @var ClassesInterface $classes
50
         */
51
        $classes = $this->container->get(ClassesInterface::class);
52
53
        $schemas = [];
54
        foreach ($classes->getClasses(Record::class) as $class) {
55
            if ($class['abstract']) {
56
                continue;
57
            }
58
59
            $schemas[] = new RecordSchema(
60
                new ReflectionEntity($class['name']),
61
                $this->container->get(MutatorsConfig::class)
62
            );
63
        }
64
65
        return $schemas;
66
    }
67
68
    /**
69
     * Locate all DocumentSources defined by user. Must return values in a form of
70
     * Document::class => Source::class.
71
     *
72
     * @return array
73
     */
74
    public function locateSources(): array
75
    {
76
        if (!$this->container->has(ClassesInterface::class)) {
77
            return [];
78
        }
79
80
        /**
81
         * @var ClassesInterface $classes
82
         */
83
        $classes = $this->container->get(ClassesInterface::class);
84
85
        $result = [];
86
        foreach ($classes->getClasses(RecordSource::class) as $class) {
87
            $source = $class['name'];
88
            if ($class['abstract'] || empty($source::RECORD)) {
89
                continue;
90
            }
91
92
            $result[$source::RECORD] = $source;
93
        }
94
95
        return $result;
96
    }
97
}