Completed
Branch job-vuejs (8df734)
by Adam
17:32
created

FirmForm   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 283
rs 10
c 1
b 0
f 0
wmc 14
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 36 5
B buildForm() 0 184 1
A messages() 0 4 1
A toJson() 0 14 2
A setDefaultOptions() 0 6 3
A forget() 0 6 2
1
<?php
2
3
namespace Coyote\Http\Forms\Job;
4
5
use Coyote\Firm;
6
use Coyote\Services\FormBuilder\Form;
7
use Coyote\Services\FormBuilder\FormEvents;
8
9
class FirmForm extends Form
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $theme = self::THEME_INLINE;
15
16
    /**
17
     * @var \Coyote\Firm
18
     */
19
    protected $data;
20
21
    /**
22
     * It's public so we can use use attr from twig
23
     *
24
     * @var array
25
     */
26
    public $attr = [
27
        'method' => self::POST
28
    ];
29
30
    public function __construct()
31
    {
32
        parent::__construct();
33
34
        $this->addEventListener(FormEvents::POST_SUBMIT, function (FirmForm $form) {
35
            $this->forget($this->data->benefits);
0 ignored issues
show
Unused Code introduced by
The call to the method Coyote\Http\Forms\Job\FirmForm::forget() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
36
37
            $data = $form->all();
38
            $data['benefits'] = array_filter(array_unique(array_map('trim', $data['benefits'])));
39
40
            // if agency - set null value. we don't to show them with agencies offers
41
            if ($form->get('is_agency')->getValue()) {
42
                foreach (['employees', 'founded', 'headline', 'latitude', 'longitude', 'street', 'city', 'house', 'postcode'] as $column) {
43
                    $this->data->{$column} = null;
44
                }
45
46
                $data['benefits'] = [];
47
            }
48
49
            foreach ($data['benefits'] as $benefit) {
50
                $this->data->benefits->add(new Firm\Benefit(['name' => $benefit]));
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->data->benefits (of type array<integer,object<Coyote\Firm\Benefit>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
51
            }
52
53
            $this->data->fill($data);
54
55
            // new firm has empty ID.
56
            if (empty($data['id'])) {
57
                $this->data->exists = false;
58
59
                unset($this->data->id);
60
            } else {
61
                // assign firm id. id is not fillable - that's why we must set it directly.
62
                $this->data->id = (int) $data['id'];
63
            }
64
        });
65
    }
66
67
    public function buildForm()
68
    {
69
        $this
70
            ->setAttr(['id' => 'firm-form', 'class' => 'submit-form', 'v-cloak' => 'v-cloak'])
71
            ->setUrl(route('job.submit.firm'))
72
            ->add('id', 'hidden', [
73
                'rules' => 'sometimes|integer',
74
                'attr' => [
75
                    'v-model' => 'firm.id'
76
                ]
77
            ])
78
            ->add('is_private', 'choice', [
79
                'multiple' => false,
80
                'rules' => 'boolean',
81
                'choices' => [
82
                    true => 'Jestem osobą prywatną',
83
                    false => 'Reprezentuje firmę'
84
                ],
85
                'child_attr' => [
86
                    'attr' => [
87
                        'v-model' => 'firm.is_private'
88
                    ]
89
                ]
90
            ])
91
            ->add('name', 'text', [
92
                'rules' => 'required_if:is_private,0|max:60',
93
                'label' => 'Nazwa firmy',
94
                'help' => 'Podając nazwę firmy, oferta staje się bardziej wiarygodna i wartościowa.',
95
                'attr' => [
96
                    'v-model' => 'firm.name',
97
                    '@keydown.once' => 'changeFirm'
98
                ]
99
            ])
100
            ->add('is_agency', 'choice', [
101
                'multiple' => false,
102
                'rules' => 'boolean',
103
                'choices' => [
104
                    0 => 'Bezpośredni pracodawca',
105
                    1 => 'Agencja pośrednictwa / IT outsourcing'
106
                ],
107
                'row_attr' => [
108
                    'class' => 'form-group-border'
109
                ],
110
                'child_attr' => [
111
                    'attr' => [
112
                        'v-model' => 'firm.is_agency'
113
                    ]
114
                ]
115
            ])
116
            ->add('website', 'text', [
117
                'rules' => 'url',
118
                'label' => 'Strona WWW',
119
                'help' => 'Firmowa strona WWW. Będzie ona wyświetlana przy ofercie.',
120
                'row_attr' => [
121
                    'class' => 'form-group-border',
122
                    ':class' => "{'has-error': isInvalid(['name'])}"
123
                ],
124
                'attr' => [
125
                    'v-model' => 'firm.website'
126
                ]
127
            ])
128
            ->add('logo', 'hidden', [
129
                'rules' => 'string',
130
                'attr' => [
131
                    'v-model' => 'firm.logo'
132
                ]
133
            ])
134
            ->add('description', 'textarea', [
135
                'label' => 'Opis firmy',
136
                'rules' => 'string',
137
                'help' => 'Czym zajmuje się firma, w jakich branżach działa oraz jakie technologie wykorzystuje?',
138
                'attr' => [
139
                    'v-model' => 'firm.description'
140
                ]
141
            ])
142
            ->add('employees', 'select', [
143
                'label' => 'Liczba pracowników w firmie',
144
                'rules' => 'integer',
145
                'help' => 'Pozwala ocenić jak duża jest firma. Czy jest to korporacja, czy mała rodzinna firma?',
146
                'choices' => Firm::getEmployeesList(),
147
                'empty_value' => '--',
148
                'row_attr' => [
149
                    'class' => 'form-group-border',
150
                    'v-show' => 'firm.is_agency == 0'
151
                ],
152
                'attr' => [
153
                    'v-model' => 'firm.employees'
154
                ]
155
            ])
156
            ->add('founded', 'select', [
157
                'label' => 'Rok powstania',
158
                'help' => 'Pozwala ocenić jak duża jest firma. Czy jest to korporacja, czy mała rodzinna firma?',
159
                'rules' => 'integer',
160
                'choices' => Firm::getFoundedList(),
161
                'empty_value' => '--',
162
                'row_attr' => [
163
                    'class' => 'form-group-border',
164
                    ':class' => "{'has-error': isInvalid(['founded'])}",
165
                    'v-show' => 'firm.is_agency == 0'
166
                ],
167
                'attr' => [
168
                    'v-model' => 'firm.founded'
169
                ]
170
            ])
171
            ->add('headline', 'text', [
172
                'rules' => 'string|max:100',
173
                'label' => 'Motto lub nagłówek',
174
                'help' => 'Pozostało <strong>${ charCounter(\'firm.headline\', 100) }</strong> znaków',
175
                'attr' => [
176
                    'maxlength' => 100,
177
                    'v-model' => 'firm.headline'
178
                ],
179
                'row_attr' => [
180
                    ':class' => "{'has-error': isInvalid(['headline'])}",
181
                    'v-show' => 'firm.is_agency == 0'
182
                ]
183
            ])
184
            ->add('latitude', 'hidden', [
185
                'rules' => 'numeric',
186
                'attr' => [
187
                    'id' => 'latitude',
188
                    'v-model' => 'firm.latitude'
189
                ]
190
            ])
191
            ->add('longitude', 'hidden', [
192
                'rules' => 'numeric',
193
                'attr' => [
194
                    'id' => 'longitude',
195
                    'v-model' => 'firm.longitude'
196
                ]
197
            ])
198
            ->add('street', 'hidden', [
199
                'rules' => 'string|max:255',
200
                'attr' => [
201
                    'v-model' => 'firm.street'
202
                ]
203
            ])
204
            ->add('city', 'hidden', [
205
                'rules' => 'string|max:255',
206
                'attr' => [
207
                    'v-model' => 'firm.city'
208
                ]
209
            ])
210
            ->add('country', 'hidden', [
211
                'attr' => [
212
                    'v-model' => 'firm.country'
213
                ]
214
            ])
215
            ->add('postcode', 'hidden', [
216
                'rules' => 'string|max:50',
217
                'attr' => [
218
                    'v-model' => 'firm.postcode'
219
                ]
220
            ])
221
            ->add('house', 'hidden', [
222
                'rules' => 'string|max:50',
223
                'attr' => [
224
                    'v-model' => 'firm.house'
225
                ]
226
            ])
227
            ->add('address', 'text', [
228
                'label' => 'Adres',
229
                'help' => 'Wpisz adres i naciśnij Enter lub kliknij na mapę. Adres firmy będzie wyświetlany przy ofercie.',
230
                'attr' => [
231
                    'id' => 'address',
232
                    'v-model' => 'address',
233
                    '@keydown.enter.prevent' => 'changeAddress'
234
                ]
235
            ])
236
            ->add('benefits', 'collection', [
237
                'property' => 'name',
238
                'child_attr' => [
239
                    'type' => 'text'
240
                ]
241
            ])
242
            ->add('submit', 'submit', [
243
                'label' => 'Zapisz',
244
                'attr' => [
245
                    'data-submit-state' => 'Wysyłanie...'
246
                ]
247
            ]);
248
249
        $this->setDefaultOptions();
250
    }
251
252
    public function messages()
253
    {
254
        return ['name.required_if' => 'Nazwa firmy jest wymagana'];
255
    }
256
257
    /**
258
     * @inheritdoc
259
     */
260
    public function toJson()
261
    {
262
        $json = json_decode(parent::toJson());
263
264
        $json->thumbnail = null;
265
        $json->logo = null;
266
267
        if ($this->get('logo')->getValue()->getFilename()) {
268
            $json->thumbnail = (string) $this->get('logo')->getValue()->url();
269
            $json->logo = $this->get('logo')->getValue()->getFilename();
270
        }
271
272
        return json_encode($json);
273
    }
274
275
    private function setDefaultOptions()
276
    {
277
        if ($this->data instanceof Firm && !$this->isSubmitted()) {
278
            $this->get('benefits')->setValue($this->data->benefits->all());
0 ignored issues
show
Bug introduced by
The method all cannot be called on $this->data->benefits (of type array<integer,object<Coyote\Firm\Benefit>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
279
        }
280
    }
281
282
    /**
283
     * @param mixed $collection
284
     */
285
    private function forget($collection)
286
    {
287
        foreach ($collection as $key => $model) {
288
            unset($collection[$key]);
289
        }
290
    }
291
}
292