Passed
Pull Request — master (#102)
by Dmitriy
02:34
created

RepositoryContainer::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Factory;
6
7
use Closure;
8
use Cycle\ORM\ORMInterface;
9
use Cycle\ORM\RepositoryInterface;
10
use Cycle\ORM\SchemaInterface;
11
use Psr\Container\ContainerInterface;
12
use Yiisoft\Di\Container;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Di\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Di\CompositeContainer;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Di\CompositeContainer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Di\Contracts\ServiceProviderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Di\Contracts\ServiceProviderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
use Yiisoft\Factory\Exception\NotFoundException;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Factory\Exception\NotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use function is_string;
18
19
final class RepositoryContainer implements ContainerInterface
20
{
21
    private ORMInterface $orm;
22
    private array $repositoryFactories = [];
23
    private array $instances;
24
    private bool $build = false;
25
26
    /**
27
     * RepositoryContainer constructor.
28
     * @param ORMInterface $orm
29
     */
30
    public function __construct(ORMInterface $orm)
31
    {
32
        $this->orm = $orm;
33
    }
34
35
36
    public function get($id)
37
    {
38
        if (isset($this->instances[$id])) {
39
            return $this->instances[$id];
40
        }
41
42
        if ($this->has($id)) {
43
            return $this->instances[$id] = $this->repositoryFactories[$id]();
44
        }
45
46
        throw new NotFoundException("Repository $id doesn't exist.");
47
    }
48
49
    public function has($id): bool
50
    {
51
        if (!$this->build) {
52
            $this->makeRepositoryFactories();
53
            $this->build = true;
54
        }
55
56
        return isset($this->repositoryFactories[$id]);
57
    }
58
59
    private function makeRepositoryFactories(): void
60
    {
61
        $schema = $this->orm->getSchema();
62
        $roles = [];
63
        foreach ($schema->getRoles() as $role) {
64
            $repository = $schema->define($role, SchemaInterface::REPOSITORY);
65
            if (is_string($repository)) {
66
                $roles[$repository][] = $role;
67
            }
68
        }
69
        foreach ($roles as $repo => $role) {
70
            if (count($role) === 1) {
71
                $this->repositoryFactories[$repo] = $this->makeRepositoryFactory(current($role));
72
            }
73
        }
74
    }
75
76
    /**
77
     * @psalm-pure
78
     */
79
    private function makeRepositoryFactory(string $role): Closure
80
    {
81
        $orm = $this->orm;
82
        return static fn (): RepositoryInterface => $orm->getRepository($role);
83
    }
84
}
85