Passed
Push — master ( 129074...847d40 )
by Tomas
11:01
created

CachedUserManager::getPermissions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Eziat\PermissionBundle\Manager;
6
7
use Eziat\PermissionBundle\Model\UserManagerAbstract;
8
use Eziat\PermissionBundle\Model\UserManagerInterface;
9
use Eziat\PermissionBundle\Model\UserPermissionInterface;
10
use Eziat\PermissionBundle\Utils\CacheHelperInterface;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
12
13
/**
14
 * @author Tomas Jakl <[email protected]>
15
 */
16
class CachedUserManager extends UserManagerAbstract implements UserManagerInterface
17
{
18
    /**
19
     * @var CacheHelperInterface
20
     */
21
    private $cacheHelper;
22
23
    /**
24
     * @var string
25
     */
26
    private $cachePrefix;
27
28
    public function __construct(CacheHelperInterface $cacheHelper, string $cachePrefix )
29
    {
30
        $this->cacheHelper = $cacheHelper;
31
        $this->cachePrefix = $cachePrefix;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getPermissions(UserPermissionInterface $user = null) : array
38
    {
39
        $cacheID = $this->cachePrefix.$user->getId();
0 ignored issues
show
Bug introduced by
It seems like $user is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
40
        $permissions = $this->cacheHelper->get($cacheID);
41
        if ($permissions === null) {
42
            $permissions = $this->getUserPermissions($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by parameter $user on line 37 can be null; however, Eziat\PermissionBundle\M...t::getUserPermissions() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
43
            if ( $permissions !== [] ){
44
                $this->cacheHelper->save($cacheID, $permissions);
45
            }
46
        }
47
48
        return $permissions;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function invalidatePermissions(UserPermissionInterface $user) : void
55
    {
56
        $cacheID = $this->cachePrefix.$user->getId();
57
        $this->cacheHelper->delete($cacheID);
58
    }
59
}