EditFieldCommandHandler::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 12
nc 2
nop 1
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\FieldGenerator\Handler;
4
5
use Hechoenlaravel\JarvisFoundation\FieldGenerator\Events\FieldWasEdited;
6
use Hechoenlaravel\JarvisFoundation\FieldGenerator\FieldModel;
7
8
class EditFieldCommandHandler
9
{
10
    /**
11
     * Handle the creation of the Field in the Database
12
     * @param $command
13
     * @return static
14
     */
15
    public function handle($command)
16
    {
17
        $fields = (array) $command;
18
        $model = FieldModel::find($command->id);
19
        $rename = false;
20
        $oldSlug = $model->slug;
21
        if ($model->slug !== $command->slug) {
22
            $rename = true;
23
        }
24
        unset($fields['id']);
25
        $model->fill($fields);
26
        $model->save();
27
        event(new FieldWasEdited($model, $rename, $oldSlug));
28
        return $model;
29
    }
30
}
31