Passed
Push — master ( 5c737e...00816c )
by Dmitry
03:19 queued 21s
created

SecurableMethodInvoker::createReflectionFunction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 4
nc 4
nop 1
1
<?php
2
3
namespace Tonic\Component\ApiLayer\JsonRpcExtensions\Security\Method;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Symfony\Component\PropertyAccess\PropertyAccessor;
7
use Tonic\Component\ApiLayer\JsonRpc\Method\MethodInvokerInterface;
8
use Tonic\Component\ApiLayer\JsonRpcExtensions\Security\Annotation\Attribute;
9
use Tonic\Component\ApiLayer\JsonRpcExtensions\Security\Exception\AccessDeniedException;
10
use Tonic\Component\ApiLayer\JsonRpcExtensions\Security\GuardInterface;
11
use Tonic\Component\ApiLayer\JsonRpcExtensions\Security\UserProviderInterface;
12
use Tonic\Component\Reflection\ReflectionFunctionFactory;
13
14
/**
15
 * Decorator for method invoker, which allows to secure method invokation.
16
 */
17
class SecurableMethodInvoker implements MethodInvokerInterface
18
{
19
    /**
20
     * @var MethodInvokerInterface
21
     */
22
    private $methodInvoker;
23
24
    /**
25
     * @var UserProviderInterface
26
     */
27
    private $userProvider;
28
29
    /**
30
     * @var GuardInterface
31
     */
32
    private $guard;
33
34
    /**
35
     * @var Reader
36
     */
37
    private $annotationReader;
38
39
    /**
40
     * @var PropertyAccessor
41
     */
42
    private $propertyAccessor;
43
44
    /**
45
     * @param MethodInvokerInterface $methodInvoker
46
     * @param UserProviderInterface  $userProvider
47
     * @param GuardInterface         $guard
48
     * @param Reader                 $annotationReader
49
     * @param PropertyAccessor       $propertyAccessor
50
     */
51
    public function __construct(
52
        MethodInvokerInterface $methodInvoker,
53
        UserProviderInterface $userProvider,
54
        GuardInterface $guard,
55
        Reader $annotationReader,
56
        PropertyAccessor $propertyAccessor
57
    ) {
58
        $this->methodInvoker = $methodInvoker;
59
        $this->userProvider = $userProvider;
60
        $this->guard = $guard;
61
        $this->annotationReader = $annotationReader;
62
        $this->propertyAccessor = $propertyAccessor;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function invoke(callable $callable, $requestObject)
69
    {
70
        /** @var Attribute $attributeAnnotation */
71
        $attributeAnnotation = $this->annotationReader->getMethodAnnotation(ReflectionFunctionFactory::createFromCallable($callable), Attribute::class);
0 ignored issues
show
Bug introduced by
It seems like \Tonic\Component\Reflect...FromCallable($callable) targeting Tonic\Component\Reflecti...y::createFromCallable() can also be of type object<ReflectionFunction>; however, Doctrine\Common\Annotati...::getMethodAnnotation() does only seem to accept object<ReflectionMethod>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72
        $attributeName = $attributeAnnotation->name;
73
        $attributeValue = $this->propertyAccessor->getValue($requestObject, $attributeAnnotation->valueAt);
74
75
        $userId = $this->userProvider->getUserId();
76
        if ((!is_array($attributeValue)) && (!$this->guard->isGranted($userId, $attributeName, $attributeValue))) {
77
            throw new AccessDeniedException();
78
        }
79
80
        if (is_array($attributeValue)) {
81
            $attributeValue = $this->guard->filterGranted($userId, $attributeName, $attributeValue);
82
            $this->propertyAccessor->setValue($requestObject, $attributeAnnotation->valueAt, $attributeValue);
83
        }
84
85
        return $this->methodInvoker->invoke($callable, $requestObject);
86
    }
87
}
88