Passed
Pull Request — master (#47)
by
unknown
17:52
created

PopupEditController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace Uccello\Core\Http\Controllers\Core;
3
4
use Kris\LaravelFormBuilder\FormBuilder;
0 ignored issues
show
Bug introduced by
The type Kris\LaravelFormBuilder\FormBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Uccello\Core\Forms\PopupEditForm;
7
use Uccello\Core\Events\BeforeSaveEvent;
8
use Uccello\Core\Events\AfterSaveEvent;
9
use Uccello\Core\Models\Domain;
10
use Uccello\Core\Models\Module;
11
use Uccello\Core\Models\Relatedlist;
12
13
class PopupEditController extends Controller
14
{
15
    protected $viewName = 'edit.popup';
16
    protected $formBuilder;
17
    /**
18
     * Check user permissions
19
     */
20
    protected function checkPermissions()
21
    {
22
        $this->middleware('uccello.permissions:create');
23
    }
24
    public function __construct(FormBuilder $formBuilder)
25
    {
26
        $this->formBuilder = $formBuilder;
27
        parent::__construct();
28
    }
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function process(?Domain $domain, Module $module, Request $request)
33
    {
34
        // Pre-process
35
        $this->preProcess($domain, $module, $request);
36
        // Retrieve record or get a new empty instance
37
        $record = $this->getRecordFromRequest();
38
        // Get form
39
        $form = $this->getForm($domain, $module, $request);
40
        // Get mode
41
        $mode = 'create';
42
        return $this->autoView([
43
            'form' => $form,
44
            'record' => $record,
45
            'mode' => $mode
46
        ]);
47
    }
48
    /**
49
     * Create or update record into database
50
     *
51
     * @param Domain|null $domain
52
     * @param Module $module
53
     * @param boolean $redirect
54
     * @return void
55
     */
56
    public function save(?Domain $domain, Module $module, Request $request, bool $redirect = false)
57
    {
58
        // Pre-process
59
        $this->preProcess($domain, $module, $request);
60
        // Retrieve record or get a new empty instance
61
        $record = $this->getRecordFromRequest();
62
        // Get form
63
        $form = $this->getForm($domain, $module, $request, $record);
64
        $values = $form->getFieldValues();
65
        $record = $form->getModel();
66
        $request = $form->getRequest();
67
        //Add fields value to record
68
        foreach ($values as $fieldName => $value) {
69
            $field = $module->getField($fieldName);
70
            // If the field exists format the value and store it in the good model column
71
            if (!is_null($field)) {
72
                $column = $field->column;
73
                $form->getModel()->$column = uitype($field->uitype_id)->getFormattedValueToSave($request, $field, $value, $record, $domain, $module);
74
            }
75
        }
76
        // Redirect if form not valid
77
        if (! $form->isValid()) {
78
            ucnotify(uctrans('notification.form.not_valid', $module), 'error');
0 ignored issues
show
Bug introduced by
It seems like uctrans('notification.form.not_valid', $module) can also be of type array; however, parameter $message of ucnotify() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            ucnotify(/** @scrutinizer ignore-type */ uctrans('notification.form.not_valid', $module), 'error');
Loading history...
79
            $response = redirect(route('uccello.popup.edit', ['domain' => $domain->slug, 'module' => $module->name]));
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            $response = redirect(/** @scrutinizer ignore-call */ route('uccello.popup.edit', ['domain' => $domain->slug, 'module' => $module->name]));
Loading history...
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            $response = /** @scrutinizer ignore-call */ redirect(route('uccello.popup.edit', ['domain' => $domain->slug, 'module' => $module->name]));
Loading history...
80
            $response = $response->withErrors($form->getErrors(), $form->getErrorBag())->withInput();
81
            return $response;
82
        }
83
        event(new BeforeSaveEvent($domain, $module, $request, $record, 'create'));
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        /** @scrutinizer ignore-call */ 
84
        event(new BeforeSaveEvent($domain, $module, $request, $record, 'create'));
Loading history...
84
        // Save record
85
        $record->save();
86
        event(new AfterSaveEvent($domain, $module, $request, $record, 'create'));
87
        return $record;
88
    }
89
    public function getForm($domain, $module, $request, $record = null)
90
    {
91
        if ($record === null) {
92
            $modelClass = $module->model_class;
93
            $record = new $modelClass;
94
        }
95
        return $this->formBuilder->create(PopupEditForm::class, [
96
            'model' => $record,
97
            'data' => [
98
                'domain' => $domain,
99
                'module' => $module,
100
                'request' => $request
101
            ]
102
        ]);
103
    }
104
}
105