Store::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 15
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Controllers\Api\User;
6
7
use App\Contracts\Http\Responses\ResponseFactory;
8
use App\Http\Requests\Api\User\StoreRequest;
9
use App\Models\User;
10
use App\Notifications\User\Invitation;
11
use Illuminate\Auth\Passwords\PasswordBrokerManager;
12
use Illuminate\Contracts\Auth\PasswordBroker;
13
use Illuminate\Contracts\Notifications\Dispatcher;
14
use Illuminate\Http\Response;
15
use Illuminate\Support\Arr;
16
use Illuminate\Support\Str;
17
18
final class Store
19
{
20
    private Dispatcher $notificationDispatcher;
21
22
    private PasswordBrokerManager $passwordBrokerManager;
23
24
    private ResponseFactory $responseFactory;
25
26 2
    public function __construct(
27
        Dispatcher $notificationDispatcher,
28
        PasswordBrokerManager $passwordBrokerManager,
29
        ResponseFactory $responseFactory
30
    ) {
31 2
        $this->notificationDispatcher = $notificationDispatcher;
32 2
        $this->passwordBrokerManager = $passwordBrokerManager;
33 2
        $this->responseFactory = $responseFactory;
34 2
    }
35
36 1
    public function __invoke(StoreRequest $request): Response
37
    {
38 1
        $attributes = $request->validated();
39
40
        // Don't need an actual password yet as we're going to send an invite.
41 1
        Arr::set($attributes, 'password', Str::random(64));
42
43
        /** @var User $user */
44 1
        $user = User::query()->create($attributes);
45
46 1
        $this->notificationDispatcher->send($user,
47 1
            new Invitation($this->getPasswordBroker()->createToken($user))
48
        );
49
50 1
        return $this->responseFactory->noContent(Response::HTTP_CREATED);
51
    }
52
53 1
    private function getPasswordBroker(): PasswordBroker
54
    {
55 1
        return $this->passwordBrokerManager->broker('user-invitations');
56
    }
57
}
58