ThemesController::edit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Backend;
4
5
use App\Models\Theme;
6
use Illuminate\Http\Request;
7
use App\Http\Controllers\Controller;
8
use Lloople\Notificator\Notificator;
9
use App\Http\Resources\ThemeResource;
10
use App\Http\Requests\ThemeFormRequest;
11
use App\ViewModels\ThemeDetailViewModel;
12
13
class ThemesController extends Controller
14
{
15
    private $pagination = 50;
16
17
    /**
18
     * Display a listing of the resource.
19
     *
20
     * @param \Illuminate\Http\Request $request
21
     *
22
     * @return \Illuminate\Http\Response
23
     */
24
    public function index(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
25
    {
26
        return view('backend.themes.index');
27
    }
28
29
    /**
30
     * Show the form for creating a new resource.
31
     *
32
     * @return \Illuminate\Http\Response
33
     */
34
    public function create()
35
    {
36
        return view('backend.themes.edit', ['view' => new ThemeDetailViewModel(new Theme())]);
37
    }
38
39
    /**
40
     * Store a newly created resource in storage.
41
     *
42
     * @param  ThemeFormRequest $request
43
     *
44
     * @return \Illuminate\Http\RedirectResponse
45
     */
46 View Code Duplication
    public function store(ThemeFormRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $theme = new Theme();
49
50
        $theme->name = $request->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in App\Http\Requests\ThemeFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
51
52
        $theme->selected = $request->has('selected');
53
54
        $theme->updateColors($request->all());
55
56
        $theme->save();
57
58
        if ($theme->selected) {
59
            $theme->disableOtherThemes();
60
        }
61
62
        Notificator::success('Theme created successfully');
63
64
        return redirect()->route('backend.themes.edit', $theme);
0 ignored issues
show
Documentation introduced by
$theme is of type object<App\Models\Theme>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
    }
66
67
    /**
68
     * Show the form for editing the specified resource.
69
     *
70
     * @param \App\Models\Theme $theme
71
     *
72
     * @return void
73
     */
74
    public function edit(Theme $theme)
75
    {
76
        return view('backend.themes.edit', ['view' => new ThemeDetailViewModel($theme)]);
77
    }
78
79
    /**
80
     * Update the specified resource in storage.
81
     *
82
     * @param  \App\Http\Requests\ThemeFormRequest $request
83
     * @param  \App\Models\Theme $theme
84
     *
85
     * @return \Illuminate\Http\RedirectResponse
86
     */
87 View Code Duplication
    public function update(ThemeFormRequest $request, Theme $theme)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $theme->name = $request->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in App\Http\Requests\ThemeFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
90
91
        $theme->selected = $request->has('selected');
92
93
        $theme->updatecolors($request->all());
94
95
        $theme->save();
96
97
        if ($theme->selected) {
98
            $theme->disableOtherThemes();
99
        }
100
101
        Notificator::success('Theme edited successfully');
102
103
        return redirect()->route('backend.themes.edit', $theme);
0 ignored issues
show
Documentation introduced by
$theme is of type object<App\Models\Theme>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
    }
105
106
    /**
107
     * Remove the specified resource from storage.
108
     *
109
     * @param \Illuminate\Http\Request $request
110
     * @param  \App\Models\Theme $theme
111
     *
112
     * @return array
113
     * @throws \Exception
114
     */
115
    public function destroy(Request $request, Theme $theme)
116
    {
117
        if ($theme->selected) {
118
            if ($request->ajax()) {
119
                return [
120
                    'result' => false,
121
                    'message' => 'Default theme cannot be deleted.',
122
                ];
123
            }
124
125
            Notificator::error('Default theme cannot be deleted.');
126
127
            return back();
128
        }
129
130
        $theme->delete();
131
132
        if ($request->ajax()) {
133
            return [
134
                'result'  => true,
135
                'message' => 'Theme deleted successfully.',
136
            ];
137
        }
138
139
        Notificator::success('Theme deleted successfully.');
140
141
        return redirect()->route('backend.themes.index');
142
    }
143
144 View Code Duplication
    public function resource(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        $themes = Theme::select('*');
147
148
        if ($request->has('q')) {
149
            $themes->where('name', 'like', "%{$request->q}%");
150
        }
151
152
        return ThemeResource::collection($themes->orderBy('selected', 'DESC')->orderBy('name')->paginate($this->pagination));
153
    }
154
}
155