UpdateAuthAssignmentsService::run()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 18
cp 0
rs 9.2408
c 0
b 0
f 0
cc 5
nc 9
nop 0
crap 30
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Service;
13
14
use Da\User\Contracts\ServiceInterface;
15
use Da\User\Model\Assignment;
16
use Da\User\Traits\AuthManagerAwareTrait;
17
18
class UpdateAuthAssignmentsService implements ServiceInterface
19
{
20
    use AuthManagerAwareTrait;
21
22
    protected $model;
23
24
    public function __construct(Assignment $model)
25
    {
26
        $this->model = $model;
27
    }
28
29
    public function run()
30
    {
31
        if (!$this->model->validate()) {
32
            return false;
33
        }
34
35
        if (!is_array($this->model->items)) {
36
            $this->model->items = [];
37
        }
38
39
        $assignedItems = $this->getAuthManager()->getItemsByUser($this->model->user_id);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface yii\rbac\ManagerInterface as the method getItemsByUser() does only exist in the following implementations of said interface: Da\User\Component\AuthDbManagerComponent.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
40
        $assignedItemsNames = array_keys($assignedItems);
41
42
        foreach (array_diff($assignedItemsNames, $this->model->items) as $item) {
43
            $this->model->getAuthManager()->revoke($assignedItems[$item], $this->model->user_id);
44
        }
45
46
        foreach (array_diff($this->model->items, $assignedItemsNames) as $item) {
47
            $this->getAuthManager()->assign($this->getAuthManager()->getItem($item), $this->model->user_id);
48
        }
49
50
        return $this->model->updated = true;
51
    }
52
}
53