Completed
Push — master ( 0476dc...911884 )
by Mahmoud
03:48
created

AssignUserToRoleTask::assignRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$roles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $roles = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key 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.

Loading history...
59
        }
60
61
        $user = $user->assignRole($roles);
0 ignored issues
show
Bug introduced by
The variable $roles does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
62
63
        return $user;
64
    }
65
66
}
67