ResourcesCollectionProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 16.22 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 5
dl 12
loc 74
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A requireOwnerCheck() 12 12 3
B get() 0 23 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace DoS\ResourceBundle\AclDecoratedResolver;
4
5
use DoS\ResourceBundle\AclDecoratedResolver\Grid\ResourceOwnerFilter;
6
use Sylius\Component\Rbac\Authorization\AuthorizationCheckerInterface;
7
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
8
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
9
use Sylius\Component\Rbac\Provider\CurrentIdentityProviderInterface;
10
use Sylius\Component\Resource\Repository\RepositoryInterface;
11
12
class ResourcesCollectionProvider implements ResourcesCollectionProviderInterface
13
{
14
    /**
15
     * @var ResourcesCollectionProviderInterface
16
     */
17
    private $decoratedResolver;
18
19
    /**
20
     * @var CurrentIdentityProviderInterface
21
     */
22
    private $currentIdentityProvider;
23
24
    /**
25
     * @var AuthorizationCheckerInterface
26
     */
27
    private $rbacAuthorizationChecker;
28
29
    /**
30
     * @var string
31
     */
32
    private $ignoreWhenHasRbacRole;
33
34
    public function __construct(
35
        ResourcesCollectionProviderInterface $decoratedResolver,
36
        CurrentIdentityProviderInterface $currentIdentityProvider,
37
        AuthorizationCheckerInterface $rbacAuthorizationChecker,
38
        $ignoreWhenHasRbacRole = 'admin'
39
    ) {
40
        $this->decoratedResolver = $decoratedResolver;
41
        $this->currentIdentityProvider = $currentIdentityProvider;
42
        $this->rbacAuthorizationChecker = $rbacAuthorizationChecker;
43
        $this->ignoreWhenHasRbacRole = $ignoreWhenHasRbacRole;
44
    }
45
46 View Code Duplication
    private function requireOwnerCheck(array $vars)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        if ($this->rbacAuthorizationChecker->isGranted($this->ignoreWhenHasRbacRole)) {
49
            return false;
50
        }
51
52
        if (!array_key_exists('acl_owner', $vars)) {
53
            return false;
54
        }
55
56
        return false !== $vars['acl_owner'];
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function get(RequestConfiguration $requestConfiguration, RepositoryInterface $repository)
63
    {
64
        if ($requestConfiguration->hasPermission() && $this->requireOwnerCheck($vars = $requestConfiguration->getVars())) {
65
            $owner = $this->currentIdentityProvider->getIdentity();
66
            $ownerField = is_bool($vars['acl_owner'])
67
                ? ResourceOwnerFilter::FIELD
68
                : $vars['acl_owner']
69
            ;
70
71
            // using in grid param
72
            $requestConfiguration
73
                ->getRequest()
74
                ->query->set(ResourceOwnerFilter::TYPE, $owner)
75
            ;
76
77
            $criteria = $requestConfiguration->getCriteria();
78
            $criteria[$ownerField] = $owner;
79
80
            $requestConfiguration->getParameters()->set('criteria', $criteria);
81
        }
82
83
        return $this->decoratedResolver->get($requestConfiguration, $repository);
84
    }
85
}
86