Completed
Push — master ( 3a5e1f...635326 )
by
03:34
created

SingleResourceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 17.39 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 1
cbo 4
dl 12
loc 69
rs 10

3 Methods

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

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 Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
6
use Sylius\Bundle\ResourceBundle\Controller\SingleResourceProviderInterface;
7
use Sylius\Component\Rbac\Authorization\AuthorizationCheckerInterface as SyliusAuthorizationCheckerInterface;
8
use Sylius\Component\Resource\Repository\RepositoryInterface;
9
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
10
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
11
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
12
13
class SingleResourceProvider implements SingleResourceProviderInterface
14
{
15
    /**
16
     * @var SingleResourceProviderInterface
17
     */
18
    private $singleResourceDecorator;
19
20
    /**
21
     * @var AuthorizationCheckerInterface
22
     */
23
    private $authorizationChecker;
24
25
    /**
26
     * @var SyliusAuthorizationCheckerInterface
27
     */
28
    private $rbacAuthorizationChecker;
29
30
    /**
31
     * @var string
32
     */
33
    private $ignoreWhenHasRbacRole;
34
35
    public function __construct(
36
        SingleResourceProviderInterface $singleResourceDecorator,
37
        AuthorizationCheckerInterface $authorizationChecker,
38
        SyliusAuthorizationCheckerInterface $rbacAuthorizationChecker,
39
        $ignoreWhenHasRbacRole = 'admin'
40
    ) {
41
        $this->singleResourceDecorator = $singleResourceDecorator;
42
        $this->authorizationChecker = $authorizationChecker;
43
        $this->rbacAuthorizationChecker = $rbacAuthorizationChecker;
44
        $this->ignoreWhenHasRbacRole = $ignoreWhenHasRbacRole;
45
    }
46
47 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...
48
    {
49
        if ($this->rbacAuthorizationChecker->isGranted($this->ignoreWhenHasRbacRole)) {
50
            return false;
51
        }
52
53
        if (!array_key_exists('acl_owner', $vars)) {
54
            return false;
55
        }
56
57
        return false !== $vars['acl_owner'];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function get(RequestConfiguration $requestConfiguration, RepositoryInterface $repository)
64
    {
65
        $resource = $this->singleResourceDecorator->get($requestConfiguration, $repository);
66
67
        if ($resource && $this->requireOwnerCheck($requestConfiguration->getVars())) {
68
            try {
69
                if ($this->authorizationChecker->isGranted('OWNER', $resource)) {
70
                    return $resource;
71
                }
72
            } catch (AuthenticationCredentialsNotFoundException $e) {
73
                throw new AccessDeniedException();
74
            }
75
76
            throw new AccessDeniedException();
77
        }
78
79
        return $resource;
80
    }
81
}
82