RepositoryFactoryConfigurator::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Neo4jBundle\Configurator;
5
6
use Innmind\Neo4j\ONM\{
7
    Metadatas,
8
    Repository,
9
    RepositoryFactory
10
};
11
use Innmind\Immutable\Map;
12
13
final class RepositoryFactoryConfigurator
14
{
15
    private $metadatas;
16
    private $repositories;
17
18 4
    public function __construct(Metadatas $metadatas)
19
    {
20 4
        $this->metadatas = $metadatas;
21 4
        $this->repositories = new Map('string', Repository::class);
22 4
    }
23
24
    /**
25
     * Register a newrepository for the given entity class
26
     *
27
     * @param string $class
28
     * @param Repository $repository
29
     *
30
     * @return self
31
     */
32 2
    public function register(string $class, Repository $repository): self
33
    {
34 2
        $this->repositories = $this->repositories->put($class, $repository);
35
36 2
        return $this;
37
    }
38
39
    /**
40
     * Inject registered repositories into the given repository factory
41
     *
42
     * @param RepositoryFactory $factory
43
     *
44
     * @return RepositoryFactory
45
     */
46 4
    public function configure(RepositoryFactory $factory): RepositoryFactory
47
    {
48
        $this
49 4
            ->repositories
50 4
            ->foreach(function(
51
                string $class,
52
                Repository $repository
53
            ) use (
54 2
                $factory
55
            ) {
56 2
                $factory->register(
57 2
                    $this->metadatas->get($class),
58 2
                    $repository
59
                );
60 4
            });
61
62 4
        return $factory;
63
    }
64
}
65