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; |
|
|
|
|
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
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: