Completed
Pull Request — master (#320)
by Alexander
03:17
created

DynamicInvocationMatcherInterceptor::invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
declare(strict_types = 1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2013, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\Aop\Framework;
13
14
use Go\Aop\Intercept\Interceptor;
15
use Go\Aop\Intercept\Invocation;
16
use Go\Aop\Intercept\Joinpoint;
17
use Go\Aop\PointFilter;
18
19
/**
20
 * Dynamic invocation matcher combines a pointcut and interceptor.
21
 *
22
 * For each invocation interceptor asks the pointcut if it matches the invocation.
23
 * Matcher will receive reflection point, object instance and invocation arguments to make a decision
24
 */
25
class DynamicInvocationMatcherInterceptor implements Interceptor
26
{
27
    /**
28
     * Instance of pointcut to dynamically match joinpoints with args
29
     *
30
     * @var PointFilter
31
     */
32
    protected $pointFilter;
33
34
    /**
35
     * Overloaded property for storing instance of Interceptor
36
     *
37
     * @var Interceptor
38
     */
39
    protected $interceptor;
40
41
    /**
42
     * Dynamic matcher constructor
43
     *
44
     * @param PointFilter $pointFilter Instance of dynamic matcher
45
     * @param Interceptor $interceptor Instance of interceptor to invoke
46
     */
47
    public function __construct(PointFilter $pointFilter, Interceptor $interceptor)
48
    {
49
        $this->pointFilter = $pointFilter;
50
        $this->interceptor = $interceptor;
51
    }
52
53
    /**
54
     * Method invoker
55
     *
56
     * @param Joinpoint $joinpoint the method invocation joinpoint
57
     *
58
     * @return mixed the result of the call to {@see Joinpoint->proceed()}
59
     */
60
    final public function invoke(Joinpoint $joinpoint)
61
    {
62
        if ($joinpoint instanceof Invocation) {
63
            $point    = $joinpoint->getStaticPart();
64
            $instance = $joinpoint->getThis();
65
            $context  = new \ReflectionClass($instance);
66
            if ($this->pointFilter->matches($point, $context, $instance, $joinpoint->getArguments())) {
67
                return $this->interceptor->invoke($joinpoint);
68
            }
69
        }
70
71
        return $joinpoint->proceed();
72
    }
73
}
74