Completed
Branch feature/split-orm (fa2f8e)
by Anton
03:35
created

SchemaLocator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B locateSchemas() 0 25 4
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ODM\Schemas;
8
9
use Interop\Container\ContainerInterface;
10
use Spiral\Models\Reflections\ReflectionEntity;
11
use Spiral\ODM\Configs\MutatorsConfig;
12
use Spiral\ODM\DocumentEntity;
13
use Spiral\Tokenizer\ClassesInterface;
14
15
/**
16
 * Provides ability to automatically locate schemas in a project. Can be user redefined in order to
17
 * automatically include custom classes.
18
 */
19
class SchemaLocator
20
{
21
    /**
22
     * Container is used for lazy resolution for ClassesInterface.
23
     *
24
     * @var ContainerInterface
25
     */
26
    protected $container;
27
28
    /**
29
     * @param ContainerInterface $container
30
     */
31
    public function __construct(ContainerInterface $container)
32
    {
33
        $this->container = $container;
34
    }
35
36
    /**
37
     * Locate all available document schemas in a project.
38
     *
39
     * @return SchemaInterface[]
40
     */
41
    public function locateSchemas(): array
42
    {
43
        if (!$this->container->has(ClassesInterface::class)) {
44
            return [];
45
        }
46
47
        /**
48
         * @var ClassesInterface $classes
49
         */
50
        $classes = $this->container->get(ClassesInterface::class);
51
52
        $schemas = [];
53
        foreach ($classes->getClasses(DocumentEntity::class) as $class) {
54
            if ($class['abstract']) {
55
                continue;
56
            }
57
58
            $schemas[] = new DocumentSchema(
59
                new ReflectionEntity($class['name']),
60
                $this->container->get(MutatorsConfig::class)
61
            );
62
        }
63
64
        return $schemas;
65
    }
66
}