Passed
Push — task/talent-dot-test ( 0ef1e8...074f28 )
by Grant
08:57 queued 03:14
created

SkillCrudController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 4
eloc 46
dl 0
loc 100
ccs 33
cts 42
cp 0.7856
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 3 1
A update() 0 3 1
A setup() 0 67 2
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use Backpack\CRUD\app\Http\Controllers\CrudController;
6
use Illuminate\Support\Facades\App;
7
use App\Models\Lookup\SkillType;
8
// Validation
9
use App\Http\Requests\SkillCrudRequest as StoreRequest;
10
use App\Http\Requests\SkillCrudRequest as UpdateRequest;
11
12
class SkillCrudController extends CrudController
13
{
14
    /**
15
     * Prepare the admin interface by setting the associated
16
     * model, setting the route, and adding custom columns/fields.
17
     *
18
     * @return void
19
     */
20 1
    public function setup() : void
21
    {
22
        // Workaround for how the unique_translation validation
23
        // works in App\Http\Requests\SkillCrudRequest
24 1
        $locale = 'en';
25 1
        if (null !== $this->request->input('locale')) {
26
            $locale = $this->request->input('locale');
27
        }
28 1
        App::setLocale($locale);
29
30
        // Eloquent model to associate with this collection
31
        // of views and controller actions.
32 1
        $this->crud->setModel("App\Models\Skill");
33
        // Custom backpack route.
34 1
        $this->crud->setRoute("admin/skill");
35
        // Custom strings to display within the backpack UI,
36
        // things like Create Skill, Delete Skills, etc.
37 1
        $this->crud->setEntityNameStrings('Skill', 'Skills');
38
39
        // Add custom columns to the Skill index view
40 1
        $this->crud->addColumn([
41 1
            'name' => 'name',
42 1
            'type' => 'text',
43 1
            'label' => 'Name',
44
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
45
                $query->orWhere('name->' . $locale, 'like', "%$searchTerm%");
46 1
            },
47
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
48
                return $query->orderBy('name->' . $locale, $columnDirection)->select('*');
49 1
            }
50
        ]);
51 1
        $this->crud->addColumn([
52 1
            'name' => 'description',
53 1
            'type' => 'text',
54 1
            'label' => 'Description',
55
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
56
                $query->orWhere('description->' . $locale, 'like', "%$searchTerm%");
57 1
            },
58
            'orderable' => false,
59
        ]);
60 1
        $this->crud->addColumn([
61 1
            'name' => 'skill_type.name',
62 1
            'type' => 'text',
63 1
            'label' => 'Type',
64
            'orderable' => true,
65
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
0 ignored issues
show
Unused Code introduced by
The import $locale is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
66
                return $query->orderBy('skill_type_id', $columnDirection)->select('*');
67 1
            }
68
        ]);
69
70
        // Add custom fields to the create/update views
71 1
        $this->crud->addField([
72 1
            'name' => 'name',
73
            'type' => 'text',
74
            'label' => "Name",
75
        ]);
76 1
        $this->crud->addField([
77 1
            'name' => 'description',
78
            'type' => 'textarea',
79
            'label' => "Description"
80
        ]);
81 1
        $this->crud->addField([
82 1
            'name' => 'skill_type_id',
83 1
            'label' => "Type",
84 1
            'type' => 'select_from_array',
85 1
            'options' => SkillType::all()->pluck('name', 'id')->toArray(),
86
            'allow_null' => false,
87
        ]);
88 1
    }
89
90
    /**
91
     * Action for creating a new Skill in the database.
92
     *
93
     * @param SkillCrudRequest $request Incoming form request.
94
     *
95
     * @return \Illuminate\Http\RedirectResponse
96
     */
97
    public function store(StoreRequest $request) // phpcs:ignore
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

97
    public function store(/** @scrutinizer ignore-unused */ StoreRequest $request) // phpcs:ignore

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        return parent::storeCrud();
100
    }
101
102
    /**
103
     * Action for creating a new Skill in the database.
104
     *
105
     * @param SkillCrudRequest $request Incoming form request.
106
     *
107
     * @return \Illuminate\Http\RedirectResponse
108
     */
109
    public function update(UpdateRequest $request) // phpcs:ignore
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

109
    public function update(/** @scrutinizer ignore-unused */ UpdateRequest $request) // phpcs:ignore

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        return parent::updateCrud();
112
    }
113
}
114