Passed
Push — 5.0.0 ( f10804...44b42e )
by Fèvre
15:04 queued 07:40
created

PasswordController::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 21
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Http\Controllers\User;
6
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Hash;
10
use Xetaravel\Http\Controllers\Controller;
11
use Xetaravel\Http\Requests\CreatePasswordRequest;
12
use Xetaravel\Http\Requests\User\UpdatePasswordRequest;
13
14
class PasswordController extends Controller
15
{
16
    /**
17
     * Create the user's password for users registered with Socialite.
18
     *
19
     * @param CreatePasswordRequest $request
20
     *
21
     * @return RedirectResponse
22
     */
23
    public static function create(CreatePasswordRequest $request): RedirectResponse
24
    {
25
        $user = Auth::user();
26
27
        if (!is_null($user->password)) {
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
28
            return redirect()
29
                ->route('user.setting.index')
0 ignored issues
show
Bug introduced by
The method route() does not exist on Illuminate\Routing\Redirector. ( Ignorable by Annotation )

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

29
                ->/** @scrutinizer ignore-call */ route('user.setting.index')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
                ->error('You have already set a password.');
31
        }
32
33
        $user->password = Hash::make($request->password);
34
35
        if ($user->save()) {
36
            return redirect()
37
                ->route('user.setting.index')
38
                ->success('Your Password has been created successfully !');
39
        }
40
41
        return redirect()
42
            ->route('user.setting.index')
43
            ->error('An error occurred while creating your Password !');
44
    }
45
46
    /**
47
     * Updates the authenticated user's password.
48
     *
49
     * @param UpdatePasswordRequest $request
50
     *
51
     * @return RedirectResponse
52
     */
53
    public function update(UpdatePasswordRequest $request): RedirectResponse
54
    {
55
        $user = Auth::user();
56
57
        if (!Hash::check($request->current_password, $user->password)) {
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
58
            return redirect()
59
                ->route('user.setting.index')
60
                ->with('danger', 'Your current Password does not match !');
61
        }
62
63
        $user->password = Hash::make($request->password);
64
65
        if ($user->save()) {
66
            return redirect()
67
                ->route('user.setting.index')
68
                ->success('Your Password has been updated successfully !');
69
        }
70
71
        return redirect()
72
            ->route('user.setting.index')
73
            ->error('An error occurred while saving your Password !');
74
    }
75
}
76