Passed
Pull Request — master (#101)
by Dmitriy
08:30
created

RepositoryProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 55
ccs 0
cts 24
cp 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRepositoryFactories() 0 17 5
A getDefinitions() 0 3 1
A makeRepositoryFactory() 0 3 1
A getExtensions() 0 13 1
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 function is_string;
17
18
/**
19
 * This provider provides factories to the container for creating Cycle entity repositories.
20
 * Repository list is compiled based on data from the database schema.
21
 */
22
final class RepositoryProvider implements ServiceProviderInterface
23
{
24
    public function getDefinitions(): array
25
    {
26
        return [];
27
    }
28
29
    /**
30
     * @return array<Closure>
31
     */
32
    public function getExtensions(): array
33
    {
34
        return [
35
            ContainerInterface::class => function (ContainerInterface $container, ContainerInterface $extended) {
36
                /** @var ORMInterface */
37
                $orm = $extended->get(ORMInterface::class);
38
                /** @psalm-suppress InaccessibleMethod */
39
                $repositoryContainer = new Container($this->getRepositoryFactories($orm));
40
                $compositeContainer = new CompositeContainer();
41
                $compositeContainer->attach($repositoryContainer);
42
                $compositeContainer->attach($extended);
43
44
                return $compositeContainer;
45
            }
46
        ];
47
    }
48
49
    /**
50
     * @return array<string, Closure> Repository name as key and factory as value
51
     */
52
    private function getRepositoryFactories(ORMInterface $orm): array
53
    {
54
        $schema = $orm->getSchema();
55
        $result = [];
56
        $roles = [];
57
        foreach ($schema->getRoles() as $role) {
58
            $repository = $schema->define($role, SchemaInterface::REPOSITORY);
59
            if (is_string($repository)) {
60
                $roles[$repository][] = $role;
61
            }
62
        }
63
        foreach ($roles as $repo => $role) {
64
            if (count($role) === 1) {
65
                $result[$repo] = $this->makeRepositoryFactory($orm, current($role));
66
            }
67
        }
68
        return $result;
69
    }
70
71
    /**
72
     * @psalm-pure
73
     */
74
    private function makeRepositoryFactory(ORMInterface $orm, string $role): Closure
75
    {
76
        return static fn(): RepositoryInterface => $orm->getRepository($role);
77
    }
78
}
79