Test Failed
Push — master ( ffc71e...1a0a2f )
by Koen
03:32
created

Reset::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 23
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Controllers\Api\Password;
6
7
use App\Http\Requests\Api\Password\ResetRequest;
8
use App\Models\User;
9
use Illuminate\Auth\Events\PasswordReset;
0 ignored issues
show
Bug introduced by
The type Illuminate\Auth\Events\PasswordReset was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Illuminate\Auth\Passwords\PasswordBrokerManager;
0 ignored issues
show
Bug introduced by
The type Illuminate\Auth\Passwords\PasswordBrokerManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Illuminate\Contracts\Auth\PasswordBroker;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Auth\PasswordBroker was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Illuminate\Contracts\Events\Dispatcher;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Events\Dispatcher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Illuminate\Contracts\Hashing\Hasher;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Hashing\Hasher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Illuminate\Contracts\Routing\ResponseFactory;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Routing\ResponseFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Illuminate\Contracts\Translation\Translator;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Translation\Translator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Illuminate\Support\Str;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Str was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
final class Reset
19
{
20
    /**
21
     * @var \Illuminate\Auth\Passwords\PasswordBrokerManager
22
     */
23
    protected $passwordBrokerManager;
24
25
    /**
26
     * @var \Illuminate\Contracts\Hashing\Hasher
27
     */
28
    protected $hasher;
29
30
    /**
31
     * @var \Illuminate\Contracts\Events\Dispatcher
32
     */
33
    protected $eventDispatcher;
34
35
    /**
36
     * @var \Illuminate\Contracts\Routing\ResponseFactory
37
     */
38
    protected $responseFactory;
39
40
    /**
41
     * @var \Illuminate\Contracts\Translation\Translator
42
     */
43
    protected $translator;
44
45
    /**
46
     * Reset constructor.
47
     *
48
     * @param  \Illuminate\Auth\Passwords\PasswordBrokerManager $passwordBrokerManager
49
     * @param  \Illuminate\Contracts\Hashing\Hasher             $hasher
50
     * @param  \Illuminate\Contracts\Events\Dispatcher          $eventDispatcher
51
     * @param  \Illuminate\Contracts\Routing\ResponseFactory    $responseFactory
52
     * @param  \Illuminate\Contracts\Translation\Translator     $translator
53
     */
54
    public function __construct(
55
        PasswordBrokerManager $passwordBrokerManager,
56
        Hasher $hasher,
57
        Dispatcher $eventDispatcher,
58
        ResponseFactory $responseFactory,
59
        Translator $translator
60
    ) {
61
        $this->passwordBrokerManager = $passwordBrokerManager;
62
        $this->hasher = $hasher;
63
        $this->eventDispatcher = $eventDispatcher;
64
        $this->responseFactory = $responseFactory;
65
        $this->translator = $translator;
66
    }
67
68
    /**
69
     * @param  \App\Http\Requests\Api\Password\ResetRequest $request
70
     * @return \Illuminate\Http\JsonResponse
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
     */
72
    public function __invoke(ResetRequest $request)
73
    {
74
        $credentials = $request->only(['token', 'email', 'password', 'password_confirmation']);
75
76
        $response = $this->getPasswordBroker()->reset($credentials, function (User $user, string $password) {
77
            $user->setAttribute('password', $this->hasher->make($password));
78
79
            $user->setRememberToken(Str::random(60));
80
81
            $user->save();
82
83
            $this->eventDispatcher->dispatch(new PasswordReset($user));
84
        });
85
86
        if (PasswordBroker::PASSWORD_RESET === $response) {
87
            return $this->responseFactory->json([
88
                'message' => $this->translator->trans($response),
89
            ], 200);
90
        }
91
92
        return $this->responseFactory->json([
93
            'message' => $this->translator->trans(PasswordBroker::INVALID_TOKEN),
94
        ], 400);
95
    }
96
97
    /**
98
     * @return \Illuminate\Contracts\Auth\PasswordBroker
99
     */
100
    protected function getPasswordBroker(): PasswordBroker
101
    {
102
        return $this->passwordBrokerManager->broker('users');
103
    }
104
}
105