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

Update   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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