1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Controller\User; |
6
|
|
|
|
7
|
|
|
use App\Controller\BaseController; |
8
|
|
|
use App\Exception\UserException; |
9
|
|
|
use App\Service\User\Create; |
|
|
|
|
10
|
|
|
use App\Service\User\Delete; |
|
|
|
|
11
|
|
|
use App\Service\User\Find; |
12
|
|
|
use App\Service\User\Update; |
|
|
|
|
13
|
|
|
use App\Service\User\Login; |
|
|
|
|
14
|
|
|
|
15
|
|
|
abstract class Base extends BaseController |
16
|
|
|
{ |
17
|
7 |
|
protected function getServiceFindUser(): Find |
18
|
|
|
{ |
19
|
7 |
|
return $this->container->get('find_user_service'); |
20
|
|
|
} |
21
|
|
|
|
22
|
6 |
|
protected function getServiceCreateUser(): Create |
23
|
|
|
{ |
24
|
6 |
|
return $this->container->get('create_user_service'); |
25
|
|
|
} |
26
|
|
|
|
27
|
3 |
|
protected function getServiceUpdateUser(): Update |
28
|
|
|
{ |
29
|
3 |
|
return $this->container->get('update_user_service'); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
protected function getServiceDeleteUser(): Delete |
33
|
|
|
{ |
34
|
1 |
|
return $this->container->get('delete_user_service'); |
35
|
|
|
} |
36
|
|
|
|
37
|
6 |
|
protected function getServiceLoginUser(): Login |
38
|
|
|
{ |
39
|
6 |
|
return $this->container->get('login_user_service'); |
40
|
|
|
} |
41
|
|
|
|
42
|
6 |
|
protected function checkUserPermissions(int $userId, int $userIdLogged): void |
43
|
|
|
{ |
44
|
6 |
|
if ($userId !== $userIdLogged) { |
45
|
2 |
|
throw new UserException('User permission failed.', 400); |
46
|
|
|
} |
47
|
4 |
|
} |
48
|
|
|
|
49
|
6 |
|
protected function getAndValidateUserId(array $input): int |
50
|
|
|
{ |
51
|
6 |
|
if (isset($input['decoded']) && isset($input['decoded']->sub)) { |
52
|
6 |
|
return (int) $input['decoded']->sub; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
throw new UserException('Invalid user. Permission failed.', 400); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
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: