Skip to content

Commit 8485007

Browse files
authored
Merge pull request #311 from terzinnorbert/master
Controller cleaning
2 parents e88b1eb + e481e63 commit 8485007

File tree

4 files changed

+51
-79
lines changed

4 files changed

+51
-79
lines changed

app/Http/Controllers/ThemesManagementController.php

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -78,72 +78,37 @@ public function store(Request $request)
7878
/**
7979
* Display the specified resource.
8080
*
81-
* @param int $id
82-
*
81+
* @param Theme $theme
8382
* @return \Illuminate\Http\Response
8483
*/
85-
public function show($id)
84+
public function show(Theme $theme)
8685
{
87-
$theme = Theme::find($id);
88-
$users = User::all();
89-
$themeUsers = [];
90-
91-
foreach ($users as $user) {
92-
if ($user->profile && $user->profile->theme_id === $theme->id) {
93-
$themeUsers[] = $user;
94-
}
95-
}
96-
97-
$data = [
98-
'theme' => $theme,
99-
'themeUsers' => $themeUsers,
100-
];
101-
102-
return view('themesmanagement.show-theme')->with($data);
86+
return view('themesmanagement.show-theme')->with($this->getThemeData($theme));
10387
}
10488

10589
/**
10690
* Show the form for editing the specified resource.
10791
*
108-
* @param int $id
109-
*
92+
* @param Theme $theme
11093
* @return \Illuminate\Http\Response
11194
*/
112-
public function edit($id)
95+
public function edit(Theme $theme)
11396
{
114-
$theme = Theme::find($id);
115-
$users = User::all();
116-
$themeUsers = [];
117-
118-
foreach ($users as $user) {
119-
if ($user->profile && $user->profile->theme_id === $theme->id) {
120-
$themeUsers[] = $user;
121-
}
122-
}
123-
124-
$data = [
125-
'theme' => $theme,
126-
'themeUsers' => $themeUsers,
127-
];
128-
129-
return view('themesmanagement.edit-theme')->with($data);
97+
return view('themesmanagement.edit-theme')->with($this->getThemeData($theme));
13098
}
13199

132100
/**
133101
* Update the specified resource in storage.
134102
*
135103
* @param \Illuminate\Http\Request $request
136-
* @param int $id
137-
*
104+
* @param Theme $theme
138105
* @return \Illuminate\Http\Response
139106
*/
140-
public function update(Request $request, $id)
107+
public function update(Request $request, Theme $theme)
141108
{
142-
$theme = Theme::find($id);
143-
144109
$input = $request->only('name', 'link', 'notes', 'status');
145110

146-
$validator = Validator::make($input, Theme::rules($id));
111+
$validator = Validator::make($input, Theme::rules($theme->id));
147112

148113
if ($validator->fails()) {
149114
return back()->withErrors($validator)->withInput();
@@ -157,14 +122,12 @@ public function update(Request $request, $id)
157122
/**
158123
* Remove the specified resource from storage.
159124
*
160-
* @param int $id
161-
*
125+
* @param Theme $theme
162126
* @return \Illuminate\Http\Response
163127
*/
164-
public function destroy($id)
128+
public function destroy(Theme $theme)
165129
{
166-
$default = Theme::findOrFail(1);
167-
$theme = Theme::findOrFail($id);
130+
$default = Theme::findOrFail(Theme::default);
168131

169132
if ($theme->id != $default->id) {
170133
$theme->delete();
@@ -174,4 +137,26 @@ public function destroy($id)
174137

175138
return back()->with('error', trans('themes.deleteSelfError'));
176139
}
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+
}
177162
}

app/Http/Controllers/UsersManagementController.php

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function __construct()
3030
*/
3131
public function index()
3232
{
33-
$pagintaionEnabled = config('usersmanagement.enablePagination');
34-
if ($pagintaionEnabled) {
33+
$paginationEnabled = config('usersmanagement.enablePagination');
34+
if ($paginationEnabled) {
3535
$users = User::paginate(config('usersmanagement.paginateListSize'));
3636
} else {
3737
$users = User::all();
@@ -50,11 +50,7 @@ public function create()
5050
{
5151
$roles = Role::all();
5252

53-
$data = [
54-
'roles' => $roles,
55-
];
56-
57-
return view('usersmanagement.create-user')->with($data);
53+
return view('usersmanagement.create-user', compact('roles'));
5854
}
5955

6056
/**
@@ -118,31 +114,26 @@ public function store(Request $request)
118114
/**
119115
* Display the specified resource.
120116
*
121-
* @param int $id
122-
*
117+
* @param User $user
123118
* @return \Illuminate\Http\Response
124119
*/
125-
public function show($id)
120+
public function show(User $user)
126121
{
127-
$user = User::find($id);
128-
129-
return view('usersmanagement.show-user')->withUser($user);
122+
return view('usersmanagement.show-user', compact('user'));
130123
}
131124

132125
/**
133126
* Show the form for editing the specified resource.
134127
*
135-
* @param int $id
136-
*
128+
* @param User $user
137129
* @return \Illuminate\Http\Response
138130
*/
139-
public function edit($id)
131+
public function edit(User $user)
140132
{
141-
$user = User::findOrFail($id);
142133
$roles = Role::all();
143134

144-
foreach ($user->roles as $user_role) {
145-
$currentRole = $user_role;
135+
foreach ($user->roles as $userRole) {
136+
$currentRole = $userRole;
146137
}
147138

148139
$data = [
@@ -158,14 +149,11 @@ public function edit($id)
158149
* Update the specified resource in storage.
159150
*
160151
* @param \Illuminate\Http\Request $request
161-
* @param int $id
162-
*
152+
* @param User $user
163153
* @return \Illuminate\Http\Response
164154
*/
165-
public function update(Request $request, $id)
155+
public function update(Request $request, User $user)
166156
{
167-
$currentUser = Auth::user();
168-
$user = User::find($id);
169157
$emailCheck = ($request->input('email') != '') && ($request->input('email') != $user->email);
170158
$ipAddress = new CaptureIpTrait();
171159

@@ -177,7 +165,7 @@ public function update(Request $request, $id)
177165
]);
178166
} else {
179167
$validator = Validator::make($request->all(), [
180-
'name' => 'required|max:255|unique:users,name,'.$id,
168+
'name' => 'required|max:255|unique:users,name,' . $user->id,
181169
'password' => 'nullable|confirmed|min:6',
182170
]);
183171
}
@@ -224,14 +212,12 @@ public function update(Request $request, $id)
224212
/**
225213
* Remove the specified resource from storage.
226214
*
227-
* @param int $id
228-
*
215+
* @param User $user
229216
* @return \Illuminate\Http\Response
230217
*/
231-
public function destroy($id)
218+
public function destroy(User $user)
232219
{
233220
$currentUser = Auth::user();
234-
$user = User::findOrFail($id);
235221
$ipAddress = new CaptureIpTrait();
236222

237223
if ($user->id != $currentUser->id) {

app/Http/ViewComposers/ThemeComposer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function compose(View $view)
3939
$theme = Theme::find($user->profile->theme_id);
4040

4141
if ($theme->status == 0) {
42-
$theme = Theme::find(1);
42+
$theme = Theme::find(Theme::default);
4343
}
4444
}
4545
}

app/Models/Theme.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class Theme extends Model
99
{
1010
use SoftDeletes;
11+
const default = 1;
1112

1213
/**
1314
* The database table used by the model.

0 commit comments

Comments
 (0)