Passed
Push — master ( 5431c9...e4edfb )
by Anton
01:36
created

RelationMap   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 11
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 2
A getAll() 0 3 1
A get() 0 7 2
A has() 0 3 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\Schema\Definition\Map;
11
12
use Cycle\Schema\Definition\Relation;
13
use Cycle\Schema\Exception\RelationException;
14
15
class RelationMap
16
{
17
    /** @var Relation[] */
18
    private $fields = [];
19
20
    /**
21
     * @param string $name
22
     * @return bool
23
     */
24
    public function has(string $name): bool
25
    {
26
        return isset($this->fields[$name]);
27
    }
28
29
    /**
30
     * @param string $name
31
     * @return Relation
32
     */
33
    public function get(string $name): Relation
34
    {
35
        if (!$this->has($name)) {
36
            throw new RelationException("Undefined relation `{$name}`");
37
        }
38
39
        return $this->fields[$name];
40
    }
41
42
    /**
43
     * @param string   $name
44
     * @param Relation $relation
45
     * @return RelationMap
46
     */
47
    public function set(string $name, Relation $relation): self
48
    {
49
        if ($this->has($name)) {
50
            throw new RelationException("Relation `{$name}` already exists");
51
        }
52
53
        $this->fields[$name] = $relation;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Get named list of all fields.
60
     *
61
     * @return array
62
     */
63
    public function getAll(): array
64
    {
65
        return $this->fields;
66
    }
67
}