Passed
Push — master ( 8a61d1...5fad71 )
by Koen
03:53
created

Store::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 26
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Controllers\Api\Registration;
6
7
use App\Auth\RegistrationDispensary;
8
use App\Contracts\Http\Responses\ResponseFactory;
9
use App\Http\Requests\Api\Registration\StoreRequest;
10
use App\Mail\Registration\AlreadyExists;
11
use App\Mail\Registration\Verify;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Http\Controllers\Api\Registration\Verify. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use App\Models\User;
13
use Illuminate\Contracts\Hashing\Hasher;
14
use Illuminate\Contracts\Mail\Mailer;
15
use Illuminate\Http\Response;
16
17
final class Store
18
{
19
    private ResponseFactory $responseFactory;
20
21
    private Mailer $mailer;
22
23
    private Hasher $hasher;
24
25
    private RegistrationDispensary $dispensary;
26
27
    public function __construct(
28
        ResponseFactory $responseFactory,
29
        RegistrationDispensary $dispensary,
30
        Mailer $mailer,
31
        Hasher $hasher
32
    ) {
33
        $this->responseFactory = $responseFactory;
34
        $this->mailer = $mailer;
35
        $this->hasher = $hasher;
36
        $this->dispensary = $dispensary;
37
    }
38
39
    public function __invoke(StoreRequest $request): Response
40
    {
41
        $email = $request->input('email');
42
43
        if (User::query()->where('email', $email)->exists()) {
44
            $this->mailer->send(
45
                (new AlreadyExists())->to($email)
46
            );
47
48
            return $this->responseFactory->noContent(Response::HTTP_OK);
49
        }
50
51
        $userData = array_merge($request->validated(), [
52
            'password' => 'not-logged-in-yet',
53
        ]);
54
55
        /** @var User $user */
56
        $user = User::query()->create($userData);
57
58
        $token = $this->dispensary->dispense($user);
59
60
        $this->mailer->send(
61
            (new Verify($token, $email))->to($email)
62
        );
63
64
        return $this->responseFactory->noContent(Response::HTTP_OK);
65
    }
66
}
67