SectionBase   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 34
eloc 63
c 1
b 0
f 0
dl 0
loc 208
rs 9.68

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A setHandle() 0 5 1
A setName() 0 5 1
A getVersion() 0 3 1
A setUpdated() 0 5 1
A addApplication() 0 9 2
A setConfig() 0 5 1
A addField() 0 9 2
A removeFields() 0 9 2
A getCreated() 0 3 1
A setCreated() 0 5 1
A getHandle() 0 3 1
A setVersion() 0 5 1
A getUpdated() 0 3 1
A removeApplication() 0 8 2
A onPreUpdate() 0 3 1
A onPrePersist() 0 4 1
A getIdValueObject() 0 3 1
A getId() 0 3 1
A setId() 0 5 1
A getConfig() 0 3 1
A getCreatedValueObject() 0 3 1
A removeField() 0 8 2
A getName() 0 3 1
A getApplications() 0 3 1
A getUpdatedValueObject() 0 3 1
A getFields() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\Entity;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Tardigrades\SectionField\ValueObject\Created;
19
use Tardigrades\SectionField\ValueObject\Handle;
20
use Tardigrades\SectionField\ValueObject\Id;
21
use Tardigrades\SectionField\ValueObject\Name;
22
use Tardigrades\SectionField\ValueObject\SectionConfig;
23
use Tardigrades\SectionField\ValueObject\Updated;
24
use Tardigrades\SectionField\ValueObject\Version;
25
26
abstract class SectionBase implements SectionInterface
27
{
28
    /** @var int */
29
    protected $id;
30
31
    /** @var string **/
32
    protected $name;
33
34
    /** @var string */
35
    protected $handle;
36
37
    /** @var ArrayCollection */
38
    protected $fields;
39
40
    /** @var array */
41
    protected $config;
42
43
    /** @var ArrayCollection */
44
    protected $applications;
45
46
    /** @var \DateTime */
47
    protected $created;
48
49
    /** @var \DateTime */
50
    protected $updated;
51
52
    /** @var int */
53
    protected $version;
54
55
    public function __construct(
56
        Collection $fields = null,
57
        Collection $applications = null
58
    ) {
59
        $this->fields = is_null($fields) ? new ArrayCollection() : $fields;
60
        $this->applications = is_null($applications) ? new ArrayCollection() : $applications;
61
    }
62
63
    public function setId(int $id): SectionInterface
64
    {
65
        $this->id = $id;
66
67
        return $this;
68
    }
69
70
    public function getId(): ?int
71
    {
72
        return $this->id;
73
    }
74
75
    public function getIdValueObject(): Id
76
    {
77
        return Id::fromInt($this->id);
78
    }
79
80
    public function getName(): Name
81
    {
82
        return Name::fromString($this->name);
83
    }
84
85
    public function setName(string $name): SectionInterface
86
    {
87
        $this->name = $name;
88
89
        return $this;
90
    }
91
92
    public function getHandle(): Handle
93
    {
94
        return Handle::fromString($this->handle);
95
    }
96
97
    public function setHandle(string $handle): SectionInterface
98
    {
99
        $this->handle = $handle;
100
101
        return $this;
102
    }
103
104
    public function addField(FieldInterface $field): SectionInterface
105
    {
106
        if ($this->fields->contains($field)) {
107
            return $this;
108
        }
109
        $this->fields->add($field);
110
        $field->addSection($this);
111
112
        return $this;
113
    }
114
115
    public function removeField(FieldInterface $field): SectionInterface
116
    {
117
        if (!$this->fields->contains($field)) {
118
            return $this;
119
        }
120
        $this->fields->removeElement($field);
121
122
        return $this;
123
    }
124
125
    public function removeFields(): SectionInterface
126
    {
127
        /** @var FieldInterface $field */
128
        foreach ($this->fields as $field) {
129
            $field->removeSection($this);
130
        }
131
        $this->fields->clear();
132
133
        return $this;
134
    }
135
136
    public function getFields(): Collection
137
    {
138
        return $this->fields;
139
    }
140
141
    public function setConfig(array $config): SectionInterface
142
    {
143
        $this->config = $config;
144
145
        return $this;
146
    }
147
148
    public function getConfig(): SectionConfig
149
    {
150
        return SectionConfig::fromArray($this->config);
151
    }
152
153
    public function addApplication(ApplicationInterface $application): SectionInterface
154
    {
155
        if ($this->applications->contains($application)) {
156
            return $this;
157
        }
158
        $this->applications->add($application);
159
        $application->addSection($this);
160
161
        return $this;
162
    }
163
164
    public function getApplications(): Collection
165
    {
166
        return $this->applications;
167
    }
168
169
    public function removeApplication(ApplicationInterface $application): SectionInterface
170
    {
171
        if (!$this->applications->contains($application)) {
172
            return $this;
173
        }
174
        $this->applications->removeElement($application);
175
176
        return $this;
177
    }
178
179
    public function setVersion(int $version): SectionInterface
180
    {
181
        $this->version = $version;
182
183
        return $this;
184
    }
185
186
    public function getVersion(): Version
187
    {
188
        return Version::fromInt($this->version);
189
    }
190
191
    public function setCreated(\DateTime $created): SectionInterface
192
    {
193
        $this->created = $created;
194
195
        return $this;
196
    }
197
198
    public function getCreated(): \DateTime
199
    {
200
        return $this->created;
201
    }
202
203
    public function getCreatedValueObject(): Created
204
    {
205
        return Created::fromDateTime($this->created);
206
    }
207
208
    public function setUpdated(\DateTime $updated): SectionInterface
209
    {
210
        $this->updated = $updated;
211
212
        return $this;
213
    }
214
215
    public function getUpdated(): \DateTime
216
    {
217
        return $this->updated;
218
    }
219
220
    public function getUpdatedValueObject(): Updated
221
    {
222
        return Updated::fromDateTime($this->updated);
223
    }
224
225
    public function onPrePersist(): void
226
    {
227
        $this->created = new \DateTime("now");
228
        $this->updated = new \DateTime("now");
229
    }
230
231
    public function onPreUpdate(): void
232
    {
233
        $this->updated = new \DateTime("now");
234
    }
235
}
236