AclEmbedInterceptor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 77
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A invoke() 0 16 2
B embedded() 0 19 5
1
<?php
2
/**
3
 * This file is part of the BEAR.AclResourceModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\AclResourceModule;
8
9
use BEAR\AclResourceModule\Exception\InvalidResourceException;
10
use BEAR\AclResourceModule\Exception\NotFoundResourceException;
11
use BEAR\Resource\Exception\BadRequestException;
12
use BEAR\Resource\ResourceInterface;
13
use BEAR\Resource\ResourceObject;
14
use Ray\Aop\MethodInterceptor;
15
use Ray\Aop\MethodInvocation;
16
use Ray\Di\Di\Named;
17
use Ray\RoleModule\RoleProviderInterface;
18
use Zend\Permissions\Acl\AclInterface;
19
use Zend\Permissions\Acl\Exception\InvalidArgumentException;
20
21
final class AclEmbedInterceptor implements MethodInterceptor
22
{
23
    /**
24
     * @var AclInterface
25
     */
26
    private $acl;
27
28
    /**
29
     * @var array
30
     */
31
    private $resources;
32
33
    /**
34
     * @var RoleProviderInterface
35
     */
36
    private $roleProvider;
37
38
    /**
39
     * @var ResourceInterface
40
     */
41
    private $resource;
42
43
    /**
44
     * @Named("resources=resources")
45
     */
46 4
    public function __construct(
47
        AclInterface $acl,
48
        array $resources,
49
        ResourceInterface $resource,
50
        RoleProviderInterface $provider
51
    ) {
52 4
        $this->acl = $acl;
53 4
        $this->resources = $resources;
54 4
        $this->roleProvider = $provider;
55 4
        $this->resource = $resource;
56 4
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 4
    public function invoke(MethodInvocation $invocation)
62
    {
63 4
        $ro = $invocation->getThis();
64
        /* @var $ro \BEAR\Resource\ResourceObject */
65
66 4
        $isTarget = array_key_exists($ro->uri->path, $this->resources);
67 4
        if (! $isTarget) {
68 1
            return $invocation->proceed();
69
        }
70 4
        $page = $ro->uri->path;
71 4
        $resources = $this->resources[$page];
72 4
        $role = $this->roleProvider->get();
73 4
        $this->embedded($resources, $role, $ro);
74
75 2
        return $ro;
76
    }
77
78 4
    private function embedded(array $resources, string $role, ResourceObject $ro)
79
    {
80 4
        foreach ($resources as $resource) {
81
            try {
82 3
                $isAllowed = $this->acl->isAllowed($role, $resource);
83 1
            } catch (InvalidArgumentException $e) {
84 1
                throw new InvalidResourceException($resource);
85
            }
86 2
            if (! $isAllowed) {
87 1
                continue;
88
            }
89 2
            $uri = sprintf('app://self/%s', $resource);
90
            try {
91 2
                $ro->body[$resource] = clone $this->resource->uri($uri);
92 1
            } catch (BadRequestException $e) {
93 2
                throw new NotFoundResourceException($uri, 500, $e);
94
            }
95
        }
96 2
    }
97
}
98