Completed
Push — master ( 4dc9cd...0d595f )
by Oliver
07:09
created

DocumentCrudController::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Webfactor\Laravel\Backpack\Documents\Controllers;
4
5
use Backpack\CRUD\app\Http\Controllers\CrudController;
6
use Webfactor\Laravel\Backpack\Documents\Requests\DocumentRequest as StoreRequest;
7
use Webfactor\Laravel\Backpack\Documents\Requests\DocumentRequest as UpdateRequest;
8
9
class DocumentCrudController extends CrudController
10
{
11
    public function setup()
12
    {
13
        /*
14
        |--------------------------------------------------------------------------
15
        | BASIC CRUD INFORMATION
16
        |--------------------------------------------------------------------------
17
        */
18
        $this->crud->setModel(config('webfactor.documents.model_class'));
19
        $this->crud->setRoute(config('webfactor.documents.backend.route_prefix').'/'.config('webfactor.documents.backend.route'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
20
        $this->crud->setEntityNameStrings(trans('webfactor::documents.entity_name_singular'),
21
            trans('webfactor::documents.entity_name_plural'));
22
23
        /*
24
        |--------------------------------------------------------------------------
25
        | BASIC CRUD INFORMATION
26
        |--------------------------------------------------------------------------
27
        */
28
29
        // ------ CRUD FIELDS
30
31
        $this->crud->addField([
32
            'name'  => 'type',
33
            'label' => 'Dokumentenart',
34
            'type'  => 'select_from_array',
35
            'options' => $this->getTypeOptions(),
36
            'allows_null' => false,
37
            'allows_multiple' => false
38
        ]);
39
40
        $this->crud->addField([
41
            'name' => 'title',
42
            'label' => trans('webfactor::documents.title')
43
        ]);
44
        
45
        $this->crud->addField([
46
            'name' => 'body',
47
            'label' => trans('webfactor::documents.body'),
48
            'type' => $this->bodyFieldType()
49
        ]);
50
51
        // ------ CRUD COLUMNS
52
53
        $this->crud->addColumns([
54
            [
55
                'name'  => 'translated_type',
56
                'label' => trans('webfactor::documents.type'),
57
            ],
58
            [
59
                'name'  => 'title',
60
                'label' => trans('webfactor::documents.title')
61
            ],
62
            [
63
                'name'  => 'body',
64
                'label' => trans('webfactor::documents.body')
65
            ]
66
        ]);
67
68
        // ------ CRUD BUTTONS
69
70
        // ------ CRUD ACCESS
71
72
        collect(config('webfactor.documents.access', []))->each(function ($enabled, $key) {
73
            if ($enabled) {
74
                $this->crud->allowAccess($key);
75
            } else {
76
                $this->crud->denyAccess($key);
77
            }
78
        });
79
80
        // ------ CRUD REORDER
81
82
        // ------ CRUD DETAILS ROW
83
84
        // ------ REVISIONS
85
86
        // ------ AJAX TABLE VIEW
87
88
        // ------ DATATABLE EXPORT BUTTONS
89
90
        // ------ ADVANCED QUERIES
91
    }
92
93
    public function getTypeOptions()
94
    {
95
        $types = collect(config('webfactor.documents.types'));
96
97
        return $types->mapWithKeys(function ($type) {
98
            return [$type => trans('webfactor::documents.types.'.$type)];
99
        });
100
    }
101
102
    public function bodyFieldType()
103
    {
104
        $types = [
105
            'plaintext' => 'textarea',
106
            'html' => 'summernote',
107
            'md' => 'simplemde'
108
        ];
109
110
        return $types[config('webfactor.documents.body_type', 'plaintext')];
111
    }
112
113
    public function store(StoreRequest $request)
114
    {
115
        // your additional operations before save here
116
        $redirect_location = parent::storeCrud($request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (storeCrud() instead of store()). Are you sure this is correct? If so, you might want to change this to $this->storeCrud().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
117
        // your additional operations after save here
118
        // use $this->data['entry'] or $this->crud->entry
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
119
        return $redirect_location;
120
    }
121
122
    public function update(UpdateRequest $request)
123
    {
124
        // your additional operations before save here
125
        $redirect_location = parent::updateCrud($request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (updateCrud() instead of update()). Are you sure this is correct? If so, you might want to change this to $this->updateCrud().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
126
        // your additional operations after save here
127
        // use $this->data['entry'] or $this->crud->entry
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
128
        return $redirect_location;
129
    }
130
}
131