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

Field   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 55
dl 0
loc 128
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getPosition() 0 3 1
A with() 0 3 1
A setTab() 0 30 5
A __construct() 0 7 1
A setGroup() 0 30 5
A getSettings() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace KunicMarko\SonataAnnotationBundle\Grouping;
7
8
use KunicMarko\SonataAnnotationBundle\Annotation;
9
10
/**
11
 * @author Marko Kunic <[email protected]>
12
 */
13
final class Field implements SortableElement
14
{
15
    /**
16
     * @var string
17
     */
18
    private $name;
19
20
    /**
21
     * @var array
22
     */
23
    private $settings;
24
25
    /**
26
     * @var integer|null
27
     */
28
    private $position;
29
30
    /**
31
     * @var string|null
32
     */
33
    private $groupName;
34
35
    /**
36
     * @var string|null
37
     */
38
    private $tabName;
39
40
    /**
41
     * @var Tab|null
42
     */
43
    private $tab;
44
45
    /**
46
     * @var Group|null
47
     */
48
    private $group;
49
50
    private function __construct(string $name, array $settings, ?int $position = null, ?string $groupName = null, ?string $tabName = null)
51
    {
52
        $this->name = $name;
53
        $this->settings = $settings;
54
        $this->position = $position;
55
        $this->groupName = $groupName;
56
        $this->tabName = $tabName;
57
    }
58
59
    public static function with(string $name, array $settings, ?int $position = null, ?string $groupName = null, ?string $tabName = null): self
60
    {
61
        return new self($name, $settings, $position, $groupName, $tabName);
62
    }
63
64
    public function setTab(Tab $tab): void
65
    {
66
        if ($this->tabName !== null && $tab->getName() !== $this->tabName) {
67
            throw new \InvalidArgumentException(sprintf(
68
                'Field "%s" has "%s" as a Tab, trying to add it to "%s" Tab failed.',
69
                $this->name,
70
                $this->tabName,
71
                $tab->getName()
72
            ));
73
        }
74
75
        if ($this->tab !== null) {
76
            throw new \InvalidArgumentException(sprintf(
77
                'Field "%s" already has "%s" as a Tab, unable to add field to "%s" Tab.',
78
                $this->name,
79
                $this->tab->getName(),
80
                $tab->getName()
81
            ));
82
        }
83
84
        if ($this->group !== null) {
85
            throw new \InvalidArgumentException(sprintf(
86
                'Field "%s" already has "%s" as a Group, unable to add field to "%s" Tab.',
87
                $this->name,
88
                $this->group->getName(),
89
                $tab->getName()
90
            ));
91
        }
92
93
        $this->tab = $tab;
94
    }
95
96
    public function setGroup(Group $group): void
97
    {
98
        if ($this->groupName !== null && $group->getName() !== $this->groupName) {
99
            throw new \InvalidArgumentException(sprintf(
100
                'Field "%s" has "%s" as a Group, trying to add it to "%s" Group failed.',
101
                $this->name,
102
                $this->groupName,
103
                $group->getName()
104
            ));
105
        }
106
107
        if ($this->group !== null) {
108
            throw new \InvalidArgumentException(sprintf(
109
                'Field "%s" already has "%s" as a Group, unable to add field to "%s" Group.',
110
                $this->name,
111
                $this->group->getName(),
112
                $group->getName()
113
            ));
114
        }
115
116
        if ($this->tab !== null) {
117
            throw new \InvalidArgumentException(sprintf(
118
                'Field "%s" already has "%s" as a Tab, unable to add field to "%s" Group.',
119
                $this->name,
120
                $this->tab->getName(),
121
                $group->getName()
122
            ));
123
        }
124
125
        $this->group = $group;
126
    }
127
128
    public function getName(): string
129
    {
130
        return $this->name;
131
    }
132
133
    public function getPosition(): ?int
134
    {
135
        return $this->position;
136
    }
137
138
    public function getSettings(): array
139
    {
140
        return $this->settings;
141
    }
142
}