RelationMap   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 48
ccs 19
cts 20
cp 0.95
rs 10
c 1
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 4 2
A set() 0 9 2
A getIterator() 0 3 1
A get() 0 7 2
A remove() 0 4 1
A has() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Definition\Map;
6
7
use Cycle\Schema\Definition\Relation;
8
use Cycle\Schema\Exception\RelationException;
9
10
/**
11
 * @implements \IteratorAggregate<string, Relation>
12
 */
13
final class RelationMap implements \IteratorAggregate
14
{
15
    /** @var array<string, Relation> */
16 56
    private array $relations = [];
17
18 56
    public function has(string $name): bool
19
    {
20
        return isset($this->relations[$name]);
21 56
    }
22
23 1244
    public function get(string $name): Relation
24
    {
25 1244
        if (!$this->has($name)) {
26
            throw new RelationException("Undefined relation `{$name}`");
27
        }
28 910
29
        return $this->relations[$name];
30 910
    }
31 2
32
    public function set(string $name, Relation $relation): self
33
    {
34 908
        if ($this->has($name)) {
35
            throw new RelationException("Relation `{$name}` already exists");
36
        }
37 1242
38
        $this->relations[$name] = $relation;
39 1242
40 2
        return $this;
41
    }
42
43 1242
    public function remove(string $name): self
44
    {
45 1242
        unset($this->relations[$name]);
46
        return $this;
47
    }
48 280
49
    /**
50 280
     * @return \Traversable<string, Relation>
51 280
     */
52
    public function getIterator(): \Traversable
53
    {
54
        return new \ArrayIterator($this->relations);
55
    }
56
57 1202
    public function __clone()
58
    {
59 1202
        foreach ($this->relations as $name => $relation) {
60
            $this->relations[$name] = clone $relation;
61
        }
62
    }
63
}
64