Completed
Push — master ( e88b1e...848500 )
by Jeremy
45s queued 12s
created

ThemesManagementController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Theme;
6
use App\Models\User;
7
use Illuminate\Http\Request;
8
use Validator;
9
10
class ThemesManagementController extends Controller
11
{
12
    /**
13
     * Create a new controller instance.
14
     *
15
     * @return void
16
     */
17
    public function __construct()
18
    {
19
        $this->middleware('auth');
20
    }
21
22
    /**
23
     * Display a listing of the resource.
24
     *
25
     * @return \Illuminate\Http\Response
26
     */
27
    public function index()
28
    {
29
        $users = User::all();
30
31
        $themes = Theme::orderBy('name', 'asc')->get();
32
33
        return View('themesmanagement.show-themes', compact('themes', 'users'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return View('themesmanag...act('themes', 'users')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
34
    }
35
36
    /**
37
     * Show the form for creating a new resource.
38
     *
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function create()
42
    {
43
        return view('themesmanagement.add-theme');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('themesmanagement.add-theme') returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
44
    }
45
46
    /**
47
     * Store a newly created resource in storage.
48
     *
49
     * @param \Illuminate\Http\Request $request
50
     *
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function store(Request $request)
54
    {
55
        $input = $request->only('name', 'link', 'notes', 'status');
56
57
        $validator = Validator::make($input, Theme::rules());
58
59
        if ($validator->fails()) {
60
            return back()->withErrors($validator)->withInput();
0 ignored issues
show
Bug Best Practice introduced by
The expression return back()->withError...validator)->withInput() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
61
        }
62
63
        $theme = Theme::create([
64
            'name'          => $request->input('name'),
65
            'link'          => $request->input('link'),
66
            'notes'         => $request->input('notes'),
67
            'status'        => $request->input('status'),
68
            'taggable_id'   => 0,
69
            'taggable_type' => 'theme',
70
        ]);
71
72
        $theme->taggable_id = $theme->id;
73
        $theme->save();
74
75
        return redirect('themes/'.$theme->id)->with('success', trans('themes.createSuccess'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('themes/...themes.createSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
76
    }
77
78
    /**
79
     * Display the specified resource.
80
     *
81
     * @param Theme $theme
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function show(Theme $theme)
85
    {
86
        return view('themesmanagement.show-theme')->with($this->getThemeData($theme));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('themesmanag...->getThemeData($theme)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
87
    }
88
89
    /**
90
     * Show the form for editing the specified resource.
91
     *
92
     * @param Theme $theme
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function edit(Theme $theme)
96
    {
97
        return view('themesmanagement.edit-theme')->with($this->getThemeData($theme));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('themesmanag...->getThemeData($theme)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
98
    }
99
100
    /**
101
     * Update the specified resource in storage.
102
     *
103
     * @param \Illuminate\Http\Request $request
104
     * @param Theme                    $theme
105
     * @return \Illuminate\Http\Response
106
     */
107
    public function update(Request $request, Theme $theme)
108
    {
109
        $input = $request->only('name', 'link', 'notes', 'status');
110
111
        $validator = Validator::make($input, Theme::rules($theme->id));
112
113
        if ($validator->fails()) {
114
            return back()->withErrors($validator)->withInput();
0 ignored issues
show
Bug Best Practice introduced by
The expression return back()->withError...validator)->withInput() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
115
        }
116
117
        $theme->fill($input)->save();
118
119
        return redirect('themes/'.$theme->id)->with('success', trans('themes.updateSuccess'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('themes/...themes.updateSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
120
    }
121
122
    /**
123
     * Remove the specified resource from storage.
124
     *
125
     * @param Theme $theme
126
     * @return \Illuminate\Http\Response
127
     */
128
    public function destroy(Theme $theme)
129
    {
130
        $default = Theme::findOrFail(Theme::default);
131
132
        if ($theme->id != $default->id) {
133
            $theme->delete();
134
135
            return redirect('themes')->with('success', trans('themes.deleteSuccess'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('themes'...themes.deleteSuccess')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
136
        }
137
138
        return back()->with('error', trans('themes.deleteSelfError'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return back()->with('err...emes.deleteSelfError')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
139
    }
140
141
    /**
142
     * @param Theme $theme
143
     * @return array
144
     */
145
    protected function getThemeData(Theme $theme): array
146
    {
147
        $users = User::all();
148
        $themeUsers = [];
149
150
        foreach ($users as $user) {
151
            if ($user->profile && $user->profile->theme_id === $theme->id) {
152
                $themeUsers[] = $user;
153
            }
154
        }
155
156
        $data = [
157
            'theme' => $theme,
158
            'themeUsers' => $themeUsers,
159
        ];
160
        return $data;
161
    }
162
}
163