Passed
Push — 2.x ( 5e81a5...d097d1 )
by Aleksei
14:17
created

MapperProvider::getMapper()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Service\Implementation;
6
7
use Cycle\ORM\Exception\ORMException;
8
use Cycle\ORM\FactoryInterface;
9
use Cycle\ORM\MapperInterface;
10
use Cycle\ORM\ORMInterface;
11
use Cycle\ORM\Service\MapperProviderInterface;
12
13
/**
14
 * @internal
15
 */
16
final class MapperProvider implements MapperProviderInterface
17
{
18
    /** @var array<non-empty-string, MapperInterface> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, MapperInterface> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, MapperInterface>.
Loading history...
19
    private array $mappers = [];
20
21 7418
    public function __construct(
22
        private ?ORMInterface $orm,
23
        private FactoryInterface $factory
24
    ) {
25
    }
26
27 6826
    public function getMapper(string $entity): MapperInterface
28
    {
29 6826
        if (isset($this->mappers[$entity])) {
30 5788
            return $this->mappers[$entity];
31
        }
32 6826
        if ($this->orm === null) {
33
            throw new ORMException('Mapper is not prepared.');
34
        }
35
36 6826
        return $this->mappers[$entity] = $this->factory->mapper($this->orm, $entity);
37
    }
38
39 34
    public function prepareMappers(): void
40
    {
41 34
        if ($this->orm === null) {
42 2
            return;
43
        }
44 34
        foreach ($this->orm->getSchema()->getRoles() as $role) {
45 32
            $this->getMapper($role);
46
        }
47 34
        $this->orm = null;
48 34
        unset($this->factory);
49
    }
50
}
51