Completed
Push — master ( 6b6eaf...aae0c4 )
by Fèvre
21s queued 14s
created

UserController::createPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace Xetaravel\Http\Controllers;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Hash;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Support\Str;
10
use Illuminate\View\View;
11
use Xetaravel\Models\Badge;
12
use Xetaravel\Models\Repositories\UserRepository;
13
use Xetaravel\Models\User;
14
use Xetaravel\Models\Validators\UserValidator;
15
use Xetaravel\Utility\UserUtility;
16
17
class UserController extends Controller
18
{
19
    /**
20
     * Constructor
21
     */
22
    public function __construct()
23
    {
24
        parent::__construct();
25
26
        $action = Route::getFacadeRoot()->current()->getActionMethod();
27
28
        if (in_array($action, ['show'])) {
29
            $this->breadcrumbs->addCrumb('Profile', route('page.index'));
30
        }
31
    }
32
33
    /**
34
     * Show the user profile page.
35
     *
36
     * @param string $slug The slug of the user.
37
     *
38
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
39
     */
40
    public function show(Request $request, string $slug)
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

40
    public function show(/** @scrutinizer ignore-unused */ Request $request, string $slug)

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...
41
    {
42
        $user = User::with('articles', 'comments')
43
            ->where('slug', Str::lower($slug))
44
            ->first();
45
46
        if (is_null($user)) {
47
            return redirect()
48
                ->route('page.index')
49
                ->with('danger', 'This user doesn\'t exist or has been deleted !');
50
        }
51
        $articles = $user->articles()
52
            ->latest()
53
            ->take(config('xetaravel.pagination.user.articles_profile_page'))
54
            ->get();
55
56
        $comments = $user->comments()
57
            ->with('article')
58
            ->latest()
59
            ->take(config('xetaravel.pagination.user.comments_profile_page'))
60
            ->get();
61
62
        $discussPosts = $user->discussPosts()
63
            ->join('discuss_conversations', 'discuss_posts.conversation_id', '=', 'discuss_conversations.id')
64
            ->where('discuss_conversations.first_post_id', '!=', 'discuss_posts.id')
65
            ->select(
66
                'discuss_posts.*',
67
                'discuss_conversations.first_post_id AS conversation_first_post_id',
68
                'discuss_conversations.title AS conversation_title',
69
                'discuss_conversations.slug AS conversation_slug',
70
                'discuss_conversations.id AS conversation_id'
71
            )
72
            ->orderBy('discuss_posts.created_at', 'DESC')
73
            ->take(config('xetaravel.pagination.user.posts_profile_page'))
74
            ->get();
75
76
        $breadcrumbs = $this->breadcrumbs->addCrumb(
77
            e($user->username),
78
            $user->profile_url
79
        );
80
81
        $badges = Badge::all();
82
83
        //dd($discussPosts);
84
85
        $articles = collect($articles);
86
87
        $activities = $articles->merge($comments)->merge($discussPosts)->sortBy('created_at', SORT_NATURAL, true);
88
        //dd($activities);
89
90
        //$level = UserUtility::getLevel($user->experiences_total);
91
        $level = UserUtility::getLevel($user->experiences_total);
92
93
        if ($level['maxLevel'] == true) {
94
            $level['currentProgression'] = 100;
95
        } elseif ($level['matchExactXPLevel'] == true) {
96
            $level['currentProgression'] = 0;
97
        } else {
98
            $level['currentProgression'] = ($level['currentUserExperience'] / $level['nextLevelExperience'])*100;
99
        }
100
101
        return view(
102
            'user.show',
103
            compact('user', 'activities', 'articles', 'comments', 'breadcrumbs', 'level', 'badges')
104
        );
105
    }
106
107
    /**
108
     * Show the settings form.
109
     *
110
     * @return \Illuminate\View\View
111
     */
112
    public function showSettingsForm(): View
113
    {
114
        $this->breadcrumbs->addCrumb('Settings', route('users.user.settings'));
115
116
        return view('user.settings', ['breadcrumbs' => $this->breadcrumbs]);
117
    }
118
119
    /**
120
     * Handle an update request for the user.
121
     *
122
     * @param \Illuminate\Http\Request $request
123
     *
124
     * @return \Illuminate\Http\RedirectResponse
125
     */
126
    public function update(Request $request): RedirectResponse
127
    {
128
        $type = $request->input('type');
129
130
        switch ($type) {
131
            case 'email':
132
                return $this->updateEmail($request);
133
134
            case 'password':
135
                return $this->updatePassword($request);
136
137
            case 'newpassword':
138
                return $this->createPassword($request);
139
140
            default:
141
                return back()
142
                    ->withInput()
143
                    ->with('danger', 'Invalid type.');
144
        }
145
    }
146
147
    /**
148
     * Handle the delete request for the user.
149
     *
150
     * @param \Illuminate\Http\Request $request
151
     *
152
     * @return \Illuminate\Http\RedirectResponse
153
     */
154
    public function delete(Request $request): RedirectResponse
155
    {
156
        $user = Auth::user();
157
158
        if (!Hash::check($request->input('password'), $user->password)) {
159
            return redirect()
160
                ->route('users.user.settings')
161
                ->with('danger', 'Your Password does not match !');
162
        }
163
        Auth::logout();
164
165
        if ($user->delete()) {
166
            return redirect()
167
                ->route('page.index')
168
                ->with('success', 'Your Account has been deleted successfully !');
169
        }
170
171
        return redirect()
172
            ->route('page.index')
173
            ->with('danger', 'An error occurred while deleting your account !');
174
    }
175
176
    /**
177
     * Handle a E-mail update request for the user.
178
     *
179
     * @param \Illuminate\Http\Request $request
180
     *
181
     * @return \Illuminate\Http\RedirectResponse
182
     */
183
    protected function updateEmail(Request $request): RedirectResponse
184
    {
185
        UserValidator::updateEmail($request->all())->validate();
186
        UserRepository::updateEmail($request->all(), Auth::user());
0 ignored issues
show
Bug introduced by
Illuminate\Support\Facades\Auth::user() of type App\User|null is incompatible with the type Xetaravel\Models\User expected by parameter $user of Xetaravel\Models\Reposit...pository::updateEmail(). ( Ignorable by Annotation )

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

186
        UserRepository::updateEmail($request->all(), /** @scrutinizer ignore-type */ Auth::user());
Loading history...
187
188
        return redirect()
189
            ->route('users.user.settings')
190
            ->with('success', 'Your E-mail has been updated successfully !');
191
    }
192
193
    /**
194
     * Handle a Password update request for the user.
195
     *
196
     * @param \Illuminate\Http\Request $request
197
     *
198
     * @return \Illuminate\Http\RedirectResponse
199
     */
200
    protected function updatePassword(Request $request): RedirectResponse
201
    {
202
        $user = Auth::user();
203
204
        if (!Hash::check($request->input('oldpassword'), $user->password)) {
205
            return redirect()
206
                ->route('users.user.settings')
207
                ->with('danger', 'Your current Password does not match !');
208
        }
209
210
        UserValidator::updatePassword($request->all())->validate();
211
        UserRepository::updatePassword($request->all(), $user);
0 ignored issues
show
Bug introduced by
$user of type App\User|null is incompatible with the type Xetaravel\Models\User expected by parameter $user of Xetaravel\Models\Reposit...itory::updatePassword(). ( Ignorable by Annotation )

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

211
        UserRepository::updatePassword($request->all(), /** @scrutinizer ignore-type */ $user);
Loading history...
212
213
        return redirect()
214
            ->route('users.user.settings')
215
            ->with('success', 'Your Password has been updated successfully !');
216
    }
217
218
    /**
219
     * Handle a Password create request for the user.
220
     *
221
     * @param \Illuminate\Http\Request $request
222
     *
223
     * @return \Illuminate\Http\RedirectResponse
224
     */
225
    protected function createPassword(Request $request): RedirectResponse
226
    {
227
        $user = Auth::user();
228
229
        if (!is_null($user->password)) {
0 ignored issues
show
introduced by
The condition is_null($user->password) is always false.
Loading history...
230
            return redirect()
231
                ->route('users.user.settings')
232
                ->with('danger', 'You have already set a password.');
233
        }
234
235
        UserValidator::createPassword($request->all())->validate();
236
        UserRepository::createPassword($request->all(), $user);
237
238
        return redirect()
239
            ->route('users.user.settings')
240
            ->with('success', 'Your password has been created successfully!');
241
    }
242
}
243