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

RelationProvider::prepareRelationMaps()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
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\ORMInterface;
9
use Cycle\ORM\Service\RelationProviderInterface;
10
use Cycle\ORM\RelationMap;
11
12
/**
13
 * @internal
14
 */
15
final class RelationProvider implements RelationProviderInterface
16
{
17
    /** @var array<non-empty-string, RelationMap> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, RelationMap> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, RelationMap>.
Loading history...
18
    private array $relMaps = [];
19
20 7418
    public function __construct(
21
        private ?ORMInterface $orm,
22
    ) {
23
    }
24
25
    /**
26
     * Get relation map associated with the given class.
27
     */
28 6874
    public function getRelationMap(string $entity): RelationMap
29
    {
30 6874
        if (isset($this->relMaps[$entity])) {
31 5396
            return $this->relMaps[$entity];
32
        }
33 6874
        if ($this->orm === null) {
34
            throw new ORMException('Relation Map is not prepared.');
35
        }
36
37 6874
        return $this->relMaps[$entity] = RelationMap::build($this->orm, $entity);
38
    }
39
40 34
    public function prepareRelationMaps(): void
41
    {
42 34
        if ($this->orm === null) {
43 2
            return;
44
        }
45 34
        foreach ($this->orm->getSchema()->getRoles() as $role) {
46 32
            $this->getRelationMap($role);
47
        }
48 34
        $this->orm = null;
49
    }
50
}
51