Update::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 25
rs 9.8333
ccs 11
cts 11
cp 1
crap 2
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