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

MapperProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 33
ccs 13
cts 14
cp 0.9286
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMapper() 0 10 3
A prepareMappers() 0 10 3
A __construct() 0 4 1
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