CreatePopupOperation   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 27
c 1
b 0
f 0
dl 0
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setupCreatePopupDefaults() 0 11 1
A setupCreatePopupRoutes() 0 12 1
A storePopup() 0 18 1
A createPopup() 0 12 1
1
<?php
2
3
namespace MedianetDev\BackpackForm\Http\Controllers\Operations;
4
5
use Illuminate\Support\Facades\Route;
6
7
trait CreatePopupOperation
8
{
9
    /**
10
     * Define which routes are needed for this operation.
11
     *
12
     * @param  string  $segment  Name of the current entity (singular). Used as first URL segment.
13
     * @param  string  $routeName  Prefix of the route name.
14
     * @param  string  $controller  Name of the current CrudController.
15
     */
16
    protected function setupCreatePopupRoutes($segment, $routeName, $controller)
17
    {
18
        Route::get($segment.'/create', [
19
            'as'        => $routeName.'.create',
20
            'uses'      => $controller.'@create',
21
            'operation' => 'create',
22
        ]);
23
24
        Route::post($segment, [
25
            'as'        => $routeName.'.store',
26
            'uses'      => $controller.'@store',
27
            'operation' => 'create',
28
        ]);
29
    }
30
31
    /**
32
     * Add the default settings, buttons, etc that this operation needs.
33
     */
34
    protected function setupCreatePopupDefaults()
35
    {
36
        $this->crud->allowAccess('create');
37
38
        $this->crud->operation('create', function () {
39
            $this->crud->loadDefaultOperationSettingsFromConfig();
40
            $this->crud->setupDefaultSaveActions();
41
        });
42
43
        $this->crud->operation('list', function () {
44
            $this->crud->addButton('top', 'create', 'view', 'crud::buttons.create');
45
        });
46
    }
47
48
    /**
49
     * Show the form for creating inserting a new row.
50
     *
51
     * @return \Illuminate\Contracts\View\View
52
     */
53
    public function createPopup()
54
    {
55
        $this->crud->hasAccessOrFail('create');
56
57
        // prepare the fields you need to show
58
        $this->data['crud'] = $this->crud;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
        $this->data['saveAction'] = $this->crud->getSaveAction();
60
61
        $this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.add').' '.$this->crud->entity_name;
0 ignored issues
show
Bug introduced by
Are you sure trans('backpack::crud.add') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

61
        $this->data['title'] = $this->crud->getTitle() ?? /** @scrutinizer ignore-type */ trans('backpack::crud.add').' '.$this->crud->entity_name;
Loading history...
62
63
        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
64
        return view("vendor/backpack/crud/create_popup", $this->data);
65
    }
66
67
    /**
68
     * Store a newly created resource in the database.
69
     *
70
     * @return \Illuminate\Http\RedirectResponse
71
     */
72
    public function storePopup()
73
    {
74
        $this->crud->hasAccessOrFail('create');
75
76
        // execute the FormRequest authorization and validation, if one is required
77
        $request = $this->crud->validateRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
78
79
        // insert item in the db
80
        $item = $this->crud->create($this->crud->getStrippedSaveRequest());
81
        $this->data['entry'] = $this->crud->entry = $item;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
82
83
        // show a success message
84
        \Alert::success(trans('backpack::crud.insert_success'))->flash();
0 ignored issues
show
Bug introduced by
The type Alert 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...
85
86
        // save the redirect choice for next time
87
        $this->crud->setSaveAction();
88
89
        return $this->crud->performSaveAction($item->getKey());
90
    }
91
}
92