Completed
Pull Request — master (#256)
by Alexander
06:46
created

StaticClosureMethodInvocation::proceed()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 8.7972
cc 4
eloc 11
nc 4
nop 0
crap 4
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Aop\Framework;
12
13
/**
14
 * Static closure method invocation is responsible to call static methods via closure
15
 */
16
final class StaticClosureMethodInvocation extends AbstractMethodInvocation
17
{
18
    /**
19
     * Closure to use
20
     *
21
     * @var \Closure
22
     */
23
    protected $closureToCall = null;
24
25
    /**
26
     * Previous scope of invocation
27
     *
28
     * @var null|object|string
29
     */
30
    protected $previousScope = null;
31
32
    /**
33
     * Invokes original method and return result from it
34
     *
35
     * @return mixed
36
     */
37 13
    public function proceed()
38
    {
39 13
        if (isset($this->advices[$this->current])) {
40
            /** @var $currentInterceptor \Go\Aop\Intercept\Interceptor */
41 1
            $currentInterceptor = $this->advices[$this->current++];
42
43 1
            return $currentInterceptor->invoke($this);
44
        }
45
46
        // Rebind the closure if scope (class name) was changed since last time
47 13
        if ($this->previousScope !== $this->instance) {
48 13
            if ($this->closureToCall === null) {
49 13
                $this->closureToCall = $this->getStaticInvoker($this->className, $this->reflectionMethod->name);
50
            }
51 13
            $this->closureToCall = $this->closureToCall->bindTo(null, $this->instance);
52 13
            $this->previousScope = $this->instance;
53
        }
54
55 13
        $closureToCall = $this->closureToCall;
56
57 13
        return $closureToCall($this->arguments);
58
59
    }
60
61
    /**
62
     * Returns static method invoker
63
     *
64
     * @param string $className Class name to forward request
65
     * @param string $method Method name to call
66
     *
67
     * @return \Closure
68
     */
69
    protected static function getStaticInvoker($className, $method)
70
    {
71 13
        return function(array $args) use ($className, $method) {
72 13
            return forward_static_call_array([$className, $method], $args);
73 13
        };
74
    }
75
}
76