Passed
Push — master ( f13856...9f0b6e )
by Alexander
02:34
created

RepositoryContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Factory;
6
7
use Cycle\ORM\ORMInterface;
8
use Cycle\ORM\RepositoryInterface;
9
use Cycle\ORM\SchemaInterface;
10
use Psr\Container\ContainerInterface;
11
use Yiisoft\Definitions\Exception\NotFoundException;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Definitions\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...
12
use Yiisoft\Definitions\Exception\NotInstantiableClassException;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Definitions\Exce...tantiableClassException 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
14
use function is_string;
15
16
final class RepositoryContainer implements ContainerInterface
17
{
18
    private ContainerInterface $rootContainer;
19
    private ORMInterface $orm;
20
21
    private bool $rolesBuilt = false;
22
    private array $roles = [];
23
24
    private array $instances;
25
26
    public function __construct(ContainerInterface $rootContainer)
27
    {
28
        $this->rootContainer = $rootContainer;
29
    }
30
31
    public function get($id)
32
    {
33
        if (isset($this->instances[$id])) {
34
            return $this->instances[$id];
35
        }
36
37
        if ($this->has($id)) {
38
            return $this->instances[$id] = $this->makeRepository($this->roles[$id]);
39
        }
40
41
        throw new NotFoundException($id);
42
    }
43
44
    public function has($id): bool
45
    {
46
        if (!is_subclass_of($id, RepositoryInterface::class)) {
47
            throw new NotInstantiableClassException(
48
                $id,
49
                sprintf('Can not instantiate "%s" because it is not a subclass of "%s".', $id, RepositoryInterface::class)
50
            );
51
        }
52
53
        if (!$this->rolesBuilt) {
54
            $this->makeRepositoryList();
55
            $this->rolesBuilt = true;
56
        }
57
58
        return isset($this->roles[$id]);
59
    }
60
61
    private function makeRepositoryList(): void
62
    {
63
        /** @var ORMInterface */
64
        $this->orm = $this->rootContainer->get(ORMInterface::class);
0 ignored issues
show
Bug introduced by
Accessing orm on the interface Cycle\ORM\ORMInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
65
        $schema = $this->orm->getSchema();
66
        $roles = [];
67
        foreach ($schema->getRoles() as $role) {
68
            $repository = $schema->define($role, SchemaInterface::REPOSITORY);
69
            if (is_string($repository)) {
70
                $roles[$repository][] = $role;
71
            }
72
        }
73
        foreach ($roles as $repository => $role) {
74
            if (count($role) === 1) {
75
                $this->roles[$repository] = current($role);
76
            }
77
        }
78
    }
79
80
    private function makeRepository(string $role): RepositoryInterface
81
    {
82
        return $this->orm->getRepository($role);
83
    }
84
}
85