Passed
Pull Request — master (#7)
by Koen
04:12
created

Update::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 2
rs 9.9332
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\Http\Requests\Api\Profile\Email\UpdateRequest;
9
use App\Models\User;
10
use App\Notifications\User\Email\CantUpdate;
11
use App\Notifications\User\Email\VerifyUpdate;
12
use Illuminate\Contracts\Auth\Guard;
13
use Illuminate\Contracts\Notifications\Dispatcher;
14
use Illuminate\Contracts\Routing\ResponseFactory;
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()->whereKeyNot($user->getKey())->where('email', $request->input('email'))->doesntExist();
41
42 2
        if ($canUpdate) {
43 1
            $token = $this->dispensary->dispense($user, $request->input('email'));
44
45 1
            $this->notificationDispatcher->send($user,
46 1
                new VerifyUpdate($token)
47
            );
48
49 1
            return $this->responseFactory->noContent(Response::HTTP_OK);
50
        }
51
52 1
        $this->notificationDispatcher->send($user,
53 1
            new CantUpdate()
54
        );
55
56 1
        return $this->responseFactory->noContent(Response::HTTP_OK);
57
    }
58
}
59