Forgotten::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Controllers\Api\Password;
6
7
use App\Contracts\Http\Responses\ResponseFactory;
8
use App\Http\Requests\Api\Password\ForgottenRequest;
9
use App\Models\User;
10
use App\Notifications\User\PasswordReset;
11
use Illuminate\Auth\Passwords\PasswordBrokerManager;
12
use Illuminate\Contracts\Auth\PasswordBroker;
13
use Illuminate\Contracts\Notifications\Dispatcher;
14
use Illuminate\Contracts\Translation\Translator;
15
use Illuminate\Http\JsonResponse;
16
17
final class Forgotten
18
{
19
    private PasswordBrokerManager $passwordBrokerManager;
20
21
    private ResponseFactory $responseFactory;
22
23
    private Translator $translator;
24
25
    private Dispatcher $notificationDispatcher;
26
27 3
    public function __construct(
28
        PasswordBrokerManager $passwordBrokerManager,
29
        ResponseFactory $responseFactory,
30
        Translator $translator,
31
        Dispatcher $notificationDispatcher
32
    ) {
33 3
        $this->passwordBrokerManager = $passwordBrokerManager;
34 3
        $this->responseFactory = $responseFactory;
35 3
        $this->translator = $translator;
36 3
        $this->notificationDispatcher = $notificationDispatcher;
37 3
    }
38
39 2
    public function __invoke(ForgottenRequest $request): JsonResponse
40
    {
41 2
        $user = User::query()->where('email', $request->input('email'))->first();
42
43 2
        if ($user instanceof User) {
44 1
            $this->notificationDispatcher->send($user,
45 1
                new PasswordReset($this->getPasswordBroker()->createToken($user))
46
            );
47
        }
48
49
        // We'll always send a successful response so that an attack can't find
50
        // what email addresses exist in the application.
51 2
        return $this->responseFactory->json([
52 2
            'message' => $this->translator->get(PasswordBroker::RESET_LINK_SENT),
53
        ]);
54
    }
55
56 1
    private function getPasswordBroker(): PasswordBroker
57
    {
58 1
        return $this->passwordBrokerManager->broker('users');
59
    }
60
}
61