|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Containers\Authorization\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use App\Containers\Authorization\Data\Repositories\RoleRepository; |
|
6
|
|
|
use App\Containers\Authorization\Tasks\GetRoleTask; |
|
7
|
|
|
use App\Containers\Authorization\Tasks\SyncUserRolesTask; |
|
8
|
|
|
use App\Containers\User\Models\User; |
|
9
|
|
|
use App\Containers\User\Tasks\FindUserByIdTask; |
|
10
|
|
|
use App\Ship\Parents\Actions\Action; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class SyncUserRolesAction. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class SyncUserRolesAction extends Action |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \App\Containers\Authorization\Tasks\SyncUserRolesTask |
|
22
|
|
|
*/ |
|
23
|
|
|
private $syncUserRolesTask; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \App\Containers\Authorization\Data\Repositories\RoleRepository |
|
27
|
|
|
*/ |
|
28
|
|
|
private $roleRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \App\Containers\User\Tasks\FindUserByIdTask |
|
32
|
|
|
*/ |
|
33
|
|
|
private $findUserByIdTask; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \App\Containers\Authorization\Tasks\GetRoleTask |
|
37
|
|
|
*/ |
|
38
|
|
|
private $getRoleTask; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* SyncUserRolesTask constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \App\Containers\Authorization\Data\Repositories\RoleRepository $roleRepository |
|
44
|
|
|
* @param \App\Containers\User\Tasks\FindUserByIdTask $findUserByIdTask |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( |
|
47
|
|
|
SyncUserRolesTask $syncUserRolesTask, |
|
48
|
|
|
RoleRepository $roleRepository, |
|
49
|
|
|
FindUserByIdTask $findUserByIdTask, |
|
50
|
|
|
GetRoleTask $getRoleTask |
|
51
|
|
|
) { |
|
52
|
|
|
$this->syncUserRolesTask = $syncUserRolesTask; |
|
53
|
|
|
$this->roleRepository = $roleRepository; |
|
54
|
|
|
$this->findUserByIdTask = $findUserByIdTask; |
|
55
|
|
|
$this->getRoleTask = $getRoleTask; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $user |
|
61
|
|
|
* @param $rolesIds |
|
62
|
|
|
* |
|
63
|
|
|
* @return \App\Containers\User\Models\User |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function run($user, $rolesIds) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
if (!$user instanceof User) { |
|
68
|
|
|
$user = $this->call(FindUserByIdTask::class, [$user]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (!is_array($rolesIds)) { |
|
72
|
|
|
$rolesIds = [$rolesIds]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
foreach ($rolesIds as $roleId) { |
|
76
|
|
|
$roles[] = $this->call(GetRoleTask::class, [$roleId]); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->call(SyncUserRolesTask::class, [$user, $roles]); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.