|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Containers\Authorization\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use App\Containers\Authorization\Data\Repositories\RoleRepository; |
|
6
|
|
|
use App\Containers\User\Models\User; |
|
7
|
|
|
use App\Containers\User\Tasks\FindUserByIdTask; |
|
8
|
|
|
use App\Port\Task\Abstracts\Task; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class AssignUserToRoleTask. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class AssignUserToRoleTask extends Task |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \App\Containers\Authorization\Data\Repositories\RoleRepository |
|
20
|
|
|
*/ |
|
21
|
|
|
private $roleRepository; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \App\Containers\User\Tasks\FindUserByIdTask |
|
25
|
|
|
*/ |
|
26
|
|
|
private $findUserByIdTask; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* AssignUserToRoleTask constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \App\Containers\Authorization\Data\Repositories\RoleRepository $roleRepository |
|
32
|
|
|
* @param \App\Containers\User\Tasks\FindUserByIdTask $findUserByIdTask |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(RoleRepository $roleRepository, FindUserByIdTask $findUserByIdTask) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->roleRepository = $roleRepository; |
|
37
|
|
|
$this->findUserByIdTask = $findUserByIdTask; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $user |
|
42
|
|
|
* @param $rolesIds |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed|\Spatie\Permission\Contracts\Role |
|
45
|
|
|
*/ |
|
46
|
|
|
public function run($user, $rolesIds) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!$user instanceof User) { |
|
49
|
|
|
$user = $this->findUserByIdTask->run($user); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!is_array($rolesIds)) { |
|
53
|
|
|
$rolesIds = [$rolesIds]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// TODO: run 1 query to get them all, let a task do that and call it from the action before (passing roles as param here) |
|
57
|
|
|
foreach ($rolesIds as $roleId) { |
|
58
|
|
|
$roles[] = $this->roleRepository->findWhere(['id' => $roleId])->first(); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$user = $user->assignRole($roles); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
return $user; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.