Passed
Pull Request — master (#103)
by Dmitriy
08:01
created

RepositoryProvider::getExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
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 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
            'core.di.delegates' => function (ContainerInterface $container, CompositeContainer $delegates) {
36
                /** @var ORMInterface */
37
                $orm = $container->get(ORMInterface::class);
38
                /** @psalm-suppress InaccessibleMethod */
39
                $delegates->attach(new Container($this->getRepositoryFactories($orm)));
40
41
                return $delegates;
42
            },
43
        ];
44
    }
45
46
    /**
47
     * @return array<string, Closure> Repository name as key and factory as value
48
     */
49
    private function getRepositoryFactories(ORMInterface $orm): array
50
    {
51
        $schema = $orm->getSchema();
52
        $result = [];
53
        $roles = [];
54
        foreach ($schema->getRoles() as $role) {
55
            $repository = $schema->define($role, SchemaInterface::REPOSITORY);
56
            if (is_string($repository)) {
57
                $roles[$repository][] = $role;
58
            }
59
        }
60
        foreach ($roles as $repo => $role) {
61
            if (count($role) === 1) {
62
                $result[$repo] = $this->makeRepositoryFactory($orm, current($role));
63
            }
64
        }
65
        return $result;
66
    }
67
68
    /**
69
     * @psalm-pure
70
     */
71
    private function makeRepositoryFactory(ORMInterface $orm, string $role): Closure
72
    {
73
        return static fn (): RepositoryInterface => $orm->getRepository($role);
74
    }
75
}
76