|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Containers\User\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use App\Containers\Authentication\Tasks\GetAuthenticatedUserTask; |
|
6
|
|
|
use App\Containers\User\Exceptions\UserNotFoundException; |
|
7
|
|
|
use App\Containers\User\Tasks\FindUserByIdTask; |
|
8
|
|
|
use App\Containers\User\Tasks\FindUserByVisitorIdTask; |
|
9
|
|
|
use App\Port\Action\Abstracts\Action; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class GetUserAction. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class GetUserAction extends Action |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \App\Containers\User\Tasks\FindUserByVisitorIdTask |
|
21
|
|
|
*/ |
|
22
|
|
|
private $findUserByVisitorIdTask; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \App\Containers\User\Tasks\GetAuthenticatedUserTask |
|
26
|
|
|
*/ |
|
27
|
|
|
private $getAuthenticatedUserTask; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* FindUserByAnythingAction constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \App\Containers\User\Tasks\FindUserByVisitorIdTask $findUserByVisitorIdTask |
|
33
|
|
|
* @param \App\Containers\User\Tasks\FindUserByIdTask $findUserByIdTask |
|
34
|
|
|
* @param \App\Containers\User\Tasks\GetAuthenticatedUserTask $getAuthenticatedUserTask |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
|
|
FindUserByVisitorIdTask $findUserByVisitorIdTask, |
|
38
|
|
|
FindUserByIdTask $findUserByIdTask, |
|
39
|
|
|
GetAuthenticatedUserTask $getAuthenticatedUserTask |
|
40
|
|
|
) { |
|
41
|
|
|
$this->findUserByVisitorIdTask = $findUserByVisitorIdTask; |
|
42
|
|
|
$this->findUserByIdTask = $findUserByIdTask; |
|
|
|
|
|
|
43
|
|
|
$this->getAuthenticatedUserTask = $getAuthenticatedUserTask; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $userId |
|
48
|
|
|
* @param $visitorId |
|
49
|
|
|
* @param $token |
|
50
|
|
|
* |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
*/ |
|
53
|
|
|
public function run($userId, $visitorId = null, $token = null) |
|
54
|
|
|
{ |
|
55
|
|
|
if ($userId) { |
|
56
|
|
|
$user = $this->findUserByIdTask->run($userId)->withToken(); |
|
57
|
|
|
} else { |
|
58
|
|
|
if ($token) { |
|
59
|
|
|
$user = $this->getAuthenticatedUserTask->run()->withToken(); |
|
60
|
|
|
} else { |
|
61
|
|
|
if ($visitorId) { |
|
62
|
|
|
$user = $this->findUserByVisitorIdTask->run($visitorId); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!$user) { |
|
|
|
|
|
|
68
|
|
|
throw new UserNotFoundException(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $user; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: