ThemesManagementController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 42
dl 0
loc 157
rs 10
c 3
b 1
f 1
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A store() 0 23 2
A create() 0 3 1
A index() 0 7 1
A edit() 0 3 1
A update() 0 13 2
A getThemeData() 0 17 4
A show() 0 3 1
A destroy() 0 11 2
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'));
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');
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();
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'));
76
    }
77
78
    /**
79
     * Display the specified resource.
80
     *
81
     * @param Theme $theme
82
     *
83
     * @return \Illuminate\Http\Response
84
     */
85
    public function show(Theme $theme)
86
    {
87
        return view('themesmanagement.show-theme')->with($this->getThemeData($theme));
88
    }
89
90
    /**
91
     * Show the form for editing the specified resource.
92
     *
93
     * @param Theme $theme
94
     *
95
     * @return \Illuminate\Http\Response
96
     */
97
    public function edit(Theme $theme)
98
    {
99
        return view('themesmanagement.edit-theme')->with($this->getThemeData($theme));
100
    }
101
102
    /**
103
     * Update the specified resource in storage.
104
     *
105
     * @param \Illuminate\Http\Request $request
106
     * @param Theme                    $theme
107
     *
108
     * @return \Illuminate\Http\Response
109
     */
110
    public function update(Request $request, Theme $theme)
111
    {
112
        $input = $request->only('name', 'link', 'notes', 'status');
113
114
        $validator = Validator::make($input, Theme::rules($theme->id));
115
116
        if ($validator->fails()) {
117
            return back()->withErrors($validator)->withInput();
118
        }
119
120
        $theme->fill($input)->save();
121
122
        return redirect('themes/'.$theme->id)->with('success', trans('themes.updateSuccess'));
123
    }
124
125
    /**
126
     * Remove the specified resource from storage.
127
     *
128
     * @param Theme $theme
129
     *
130
     * @return \Illuminate\Http\Response
131
     */
132
    public function destroy(Theme $theme)
133
    {
134
        $default = Theme::findOrFail(Theme::default);
135
136
        if ($theme->id !== $default->id) {
137
            $theme->delete();
138
139
            return redirect('themes')->with('success', trans('themes.deleteSuccess'));
140
        }
141
142
        return back()->with('error', trans('themes.deleteSelfError'));
143
    }
144
145
    /**
146
     * @param Theme $theme
147
     *
148
     * @return array
149
     */
150
    protected function getThemeData(Theme $theme): array
151
    {
152
        $users = User::all();
153
        $themeUsers = [];
154
155
        foreach ($users as $user) {
156
            if ($user->profile && $user->profile->theme_id === $theme->id) {
157
                $themeUsers[] = $user;
158
            }
159
        }
160
161
        $data = [
162
            'theme'      => $theme,
163
            'themeUsers' => $themeUsers,
164
        ];
165
166
        return $data;
167
    }
168
}
169