Completed
Push — master ( f706eb...84b009 )
by Antonio
01:40
created

AuthRuleEditionService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Rule;
16
use Da\User\Traits\AuthManagerAwareTrait;
17
use Da\User\Traits\ContainerAwareTrait;
18
use Exception;
19
20
class AuthRuleEditionService implements ServiceInterface
21
{
22
    use AuthManagerAwareTrait;
23
    use ContainerAwareTrait;
24
25
    protected $model;
26
27
    public function __construct(Rule $model)
28
    {
29
        $this->model = $model;
30
    }
31
32
    public function run()
33
    {
34
        if (!$this->model->validate() || (!in_array($this->model->scenario, ['create', 'update']))) {
35
            return false;
36
        }
37
38
        $rule = $this->make($this->model->className, [], ['name' => $this->model->name]);
39
40
        try {
41
            if ($this->model->scenario == 'create') {
42
                $this->getAuthManager()->add($rule);
43
            } else {
44
                $this->getAuthManager()->update($this->model->previousName, $rule);
45
            }
46
            $this->getAuthManager()->invalidateCache();
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 invalidateCache() does only exist in the following implementations of said interface: Da\User\Component\AuthDbManagerComponent, yii\rbac\DbManager.

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...
47
        } catch (Exception $e) {
48
            return false;
49
        }
50
51
        return true;
52
    }
53
}
54