Form   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 29
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 231
ccs 0
cts 151
cp 0
rs 10

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getElements() 0 4 1
A getElement() 0 12 2
A setElements() 0 6 1
A addElement() 0 6 1
A getModel() 0 4 1
A setModel() 0 8 1
A setElementModel() 0 10 1
A validate() 0 11 1
A getValidationRules() 0 4 1
A getValidationMessages() 0 4 1
A getValidationTitles() 0 4 1
A getElementsValidationRules() 0 11 2
A getElementsValidationMessages() 0 11 2
A getElementsValidationTitles() 0 11 2
A saveElements() 0 6 1
A finishSaveElements() 0 6 1
A finishSave() 0 4 1
A save() 0 13 2
A getValues() 0 8 1
A toArray() 0 7 1
A jsonSerialize() 0 4 1
A toJson() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace Sco\Admin\Form;
4
5
use Illuminate\Database\Eloquent\Model;
6
use InvalidArgumentException;
7
use Sco\Admin\Contracts\Form\Elements\ElementInterface;
8
use Sco\Admin\Contracts\Form\FormInterface;
9
use Sco\Admin\Contracts\Validatable;
10
use Validator;
11
12
class Form implements
13
    FormInterface,
14
    Validatable
15
{
16
    /**
17
     * @var \Sco\Admin\Form\ElementsCollection
18
     */
19
    protected $elements;
20
21
    /**
22
     * @var \Illuminate\Database\Eloquent\Model
23
     */
24
    protected $model;
25
26
    public function __construct(array $elements = [])
27
    {
28
        $this->setElements($elements);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getElements()
35
    {
36
        return $this->elements;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getElement($name)
43
    {
44
        $key = $this->getElements()->search(function (ElementInterface $item) use ($name
45
        ) {
46
            return $item->getName() == $name;
47
        });
48
        if ($key === false) {
49
            throw new InvalidArgumentException('Not found element');
50
        }
51
52
        return $this->getElements()->get($key);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function setElements(array $elements)
59
    {
60
        $this->elements = new ElementsCollection($elements);
61
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function addElement(ElementInterface $element)
69
    {
70
        $this->elements->push($element);
71
72
        return $this;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getModel()
79
    {
80
        return $this->model;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function setModel(Model $model)
87
    {
88
        $this->model = $model;
89
90
        $this->setElementModel($model);
91
92
        return $this;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function setElementModel(Model $model)
99
    {
100
        $this->elements->each(function (ElementInterface $element) use ($model) {
101
            //if ($element instanceof WithModel) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
            $element->setModel($model);
103
            //}
104
        });
105
106
        return $this;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function validate()
113
    {
114
        Validator::validate(
115
            request()->all(),
116
            $this->getValidationRules(),
117
            $this->getValidationMessages(),
118
            $this->getValidationTitles()
119
        );
120
121
        return $this;
122
    }
123
124
    public function getValidationRules()
125
    {
126
        return $this->getElementsValidationRules();
127
    }
128
129
    public function getValidationMessages()
130
    {
131
        return $this->getElementsValidationMessages();
132
    }
133
134
    public function getValidationTitles()
135
    {
136
        return $this->getElementsValidationTitles();
137
    }
138
139
    protected function getElementsValidationRules()
140
    {
141
        $rules = [];
142
        $this->elements->each(function (ElementInterface $element) use (&$rules) {
143
            if ($element instanceof Validatable) {
144
                $rules += $element->getValidationRules();
145
            }
146
        });
147
148
        return $rules;
149
    }
150
151
    protected function getElementsValidationMessages()
152
    {
153
        $messages = [];
154
        $this->elements->each(function (ElementInterface $element) use (&$messages) {
155
            if ($element instanceof Validatable) {
156
                $messages += $element->getValidationMessages();
157
            }
158
        });
159
160
        return $messages;
161
    }
162
163
    protected function getElementsValidationTitles()
164
    {
165
        $titles = [];
166
        $this->elements->each(function (ElementInterface $element) use (&$titles) {
167
            if ($element instanceof Validatable) {
168
                $titles += $element->getValidationTitles();
169
            }
170
        });
171
172
        return $titles;
173
    }
174
175
    protected function saveElements()
176
    {
177
        $this->getElements()->each(function (ElementInterface $element) {
178
            $element->save();
179
        });
180
    }
181
182
    protected function finishSaveElements()
183
    {
184
        $this->getElements()->each(function (ElementInterface $element) {
185
            $element->finishSave();
186
        });
187
    }
188
189
    protected function finishSave()
190
    {
191
        $this->finishSaveElements();
192
    }
193
194
    public function save()
195
    {
196
        $this->saveElements();
197
198
        $model = $this->getModel();
199
        $saved = $model->save();
200
201
        if ($saved) {
202
            $this->finishSave();
203
        }
204
205
        return true;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function getValues()
212
    {
213
        return $this->elements->mapWithKeys(function (ElementInterface $element) {
214
            return [
215
                $element->getName() => $element->getValue(),
216
            ];
217
        });
218
    }
219
220
    public function toArray()
221
    {
222
        return [
223
            'elements' => $this->getElements(),
224
            'values'   => $this->getValues(),
225
        ];
226
    }
227
228
    public function jsonSerialize()
229
    {
230
        return $this->toArray();
231
    }
232
233
    public function toJson($options = 0)
234
    {
235
        return json_encode($this->jsonSerialize(), $options);
236
    }
237
238
    public function __toString()
239
    {
240
        return $this->toJson();
241
    }
242
}
243