AuthorizationService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 39
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hasRole() 0 4 1
A isAllowed() 0 4 1
A denyAccessUnlessGranted() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\Auth\Authorization;
16
17
use Acme\App\Core\Port\Auth\Authorization\AccessDeniedException;
18
use Acme\App\Core\Port\Auth\Authorization\AuthorizationServiceInterface;
19
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
20
21
final class AuthorizationService implements AuthorizationServiceInterface
22
{
23
    /**
24
     * @var AuthorizationCheckerInterface
25
     */
26
    private $authorizationChecker;
27
28
    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
29
    {
30
        $this->authorizationChecker = $authorizationChecker;
31
    }
32
33
    public function hasRole(string ...$roleList): bool
34
    {
35
        return $this->authorizationChecker->isGranted($roleList);
36
    }
37
38
    public function isAllowed(string $action, $subject): bool
39
    {
40
        return $this->authorizationChecker->isGranted($action, $subject);
41
    }
42
43
    /**
44
     * Throws an exception unless the specified roles and action are met on the subject.
45
     */
46
    public function denyAccessUnlessGranted(
47
        array $roleList = [],
48
        string $action = '',
49
        string $message = 'Access Denied.',
50
        $subject = null
51
    ): void {
52
        $attributes[] = $action;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
53
        $attributes = \array_filter(\array_merge($roleList, $attributes));
54
55
        if (!$this->authorizationChecker->isGranted($attributes, $subject)) {
56
            throw new AccessDeniedException($roleList, $action, $subject, $message);
57
        }
58
    }
59
}
60