Update   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 43
rs 10
ccs 16
cts 16
cp 1
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __invoke() 0 25 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