Update::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Controllers\Api\Profile\Email;
6
7
use App\Auth\EmailDispensary;
8
use App\Contracts\Http\Responses\ResponseFactory;
9
use App\Http\Requests\Api\Profile\Email\UpdateRequest;
10
use App\Models\User;
11
use App\Notifications\User\Email\CantUpdate;
12
use App\Notifications\User\Email\VerifyUpdate;
13
use Illuminate\Contracts\Auth\Guard;
14
use Illuminate\Contracts\Notifications\Dispatcher;
15
use Illuminate\Http\Response;
16
17
final class Update
18
{
19
    private EmailDispensary $dispensary;
20
21
    private Dispatcher $notificationDispatcher;
22
23
    private ResponseFactory $responseFactory;
24
25 3
    public function __construct(
26
        EmailDispensary $dispensary,
27
        Dispatcher $notificationDispatcher,
28
        ResponseFactory $responseFactory
29
    ) {
30 3
        $this->dispensary = $dispensary;
31 3
        $this->notificationDispatcher = $notificationDispatcher;
32 3
        $this->responseFactory = $responseFactory;
33 3
    }
34
35 2
    public function __invoke(Guard $guard, UpdateRequest $request): Response
36
    {
37
        /** @var \App\Models\User $user */
38 2
        $user = $guard->user();
39
40 2
        $canUpdate = User::query()
41
            ->whereKeyNot($user->getKey())
42 2
            ->where('email', $request->input('email'))
43 1
            ->doesntExist();
44
45 1
        if ($canUpdate) {
46 1
            $token = $this->dispensary->dispense($user, $request->input('email'));
47
48
            $this->notificationDispatcher->send($user,
49 1
                new VerifyUpdate($token, $request->input('email'))
50
            );
51
52 1
            return $this->responseFactory->noContent(Response::HTTP_OK);
53 1
        }
54
55
        $this->notificationDispatcher->send($user,
56 1
            new CantUpdate()
57
        );
58
59
        return $this->responseFactory->noContent(Response::HTTP_OK);
60
    }
61
}
62