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

Group   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 32
dl 0
loc 93
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addField() 0 4 1
A getName() 0 3 1
A with() 0 3 1
A getFieldCollection() 0 3 1
A setTab() 0 21 3
A __construct() 0 7 1
A getTabName() 0 3 1
A getPosition() 0 3 1
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 Group implements SortableElement
11
{
12
    /**
13
     * @var string
14
     */
15
    private $name;
16
17
    /**
18
     * @var array
19
     */
20
    private $options = [];
21
22
    /**
23
     * @var Tab|null
24
     */
25
    private $tab;
26
27
    /**
28
     * @var string|null
29
     */
30
    private $tabName;
31
32
    /**
33
     * @var int|null
34
     */
35
    private $position;
36
37
    /**
38
     * @var FieldCollection
39
     */
40
    private $fieldCollection;
41
42
    private function __construct(string $name, array $options = [], ?string $tabName = null, ?int $position = null)
43
    {
44
        $this->name = $name;
45
        $this->options = $options;
46
        $this->tabName = $tabName;
47
        $this->position = $position;
48
        $this->fieldCollection = FieldCollection::create();
49
    }
50
51
    public static function with(string $name, array $options = [], ?string $tabName = null, ?int $position = null): self
52
    {
53
        return new self($name, $options, $tabName, $position);
54
    }
55
56
    public function setTab(Tab $tab): void
57
    {
58
        if ($tab->getName() !== $this->tabName) {
59
            throw new \InvalidArgumentException(sprintf(
60
                'Group "%s" has "%s" as a Tab, trying to add it to "%s" Tab failed.',
61
                $this->name,
62
                $this->tabName,
63
                $tab->getName()
64
            ));
65
        }
66
67
        if ($this->tab !== null) {
68
            throw new \InvalidArgumentException(sprintf(
69
                'Group "%s" already has "%s" as a Tab, unable to add group to "%s" Tab.',
70
                $this->name,
71
                $this->tab->getName(),
72
                $tab->getName()
73
            ));
74
        }
75
76
        $this->tab = $tab;
77
    }
78
79
    public function getTabName(): ?string
80
    {
81
        return $this->tabName;
82
    }
83
84
    public function getName(): string
85
    {
86
        return $this->name;
87
    }
88
89
    public function addField(Field $field): void
90
    {
91
        $field->setGroup($this);
92
        $this->fieldCollection->add($field);
93
    }
94
95
    public function getPosition(): ?int
96
    {
97
        return $this->position;
98
    }
99
100
    public function getFieldCollection(): FieldCollection
101
    {
102
        return $this->fieldCollection;
103
    }
104
}