Passed
Pull Request — 1.x (#35)
by Marko
05:54
created

Tab   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 27
dl 0
loc 80
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getName() 0 3 1
A addField() 0 11 2
A with() 0 3 1
A getGroupCollection() 0 3 1
A getPosition() 0 3 1
A addGroup() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Grouping;
6
7
/**
8
 * @author Marko Kunic <[email protected]>
9
 */
10
final class Tab implements SortableElement
11
{
12
    /**
13
     * @var string
14
     */
15
    private $name;
16
17
    /**
18
     * @var array
19
     */
20
    private $options;
21
22
    /**
23
     * @var int|null
24
     */
25
    private $position;
26
27
    /**
28
     * @var GroupCollection
29
     */
30
    private $groupCollection;
31
32
    /**
33
     * @var FieldCollection
34
     */
35
    private $fieldsCollection;
36
37
    private function __construct(string $name, array $options = [], ?int $position = null)
38
    {
39
        $this->name = $name;
40
        $this->options = $options;
41
        $this->position = $position;
42
        $this->groupCollection = GroupCollection::create();
43
        $this->fieldsCollection = FieldCollection::create();
44
    }
45
46
    public static function with(string $name, array $options = [], ?int $position = null): self
47
    {
48
        return new self($name, $options, $position);
49
    }
50
51
    public function getName(): string
52
    {
53
        return $this->name;
54
    }
55
56
    public function addGroup(Group $group): void
57
    {
58
        if (!$this->fieldsCollection->isEmpty()) {
59
            throw new \InvalidArgumentException(sprintf(
60
                'Tab "%s" already has field(s), it can either have groups with fields or just field.',
61
                $this->name
62
            ));
63
        }
64
65
        $group->setTab($this);
66
        $this->groupCollection->add($group);
67
    }
68
69
    public function addField(Field $field): void
70
    {
71
        if (!$this->groupCollection->isEmpty()) {
72
            throw new \InvalidArgumentException(sprintf(
73
                'Tab "%s" already has group(s), it can either have groups with fields or just field.',
74
                $this->name
75
            ));
76
        }
77
78
        $field->setTab($this);
79
        $this->fieldsCollection->add($field);
80
    }
81
82
    public function getPosition(): ?int
83
    {
84
        return $this->position;
85
    }
86
87
    public function getGroupCollection(): GroupCollection
88
    {
89
        return $this->groupCollection;
90
    }
91
}