Completed
Pull Request — 2.x (#349)
by Alexander
02:20
created

DynamicClosureMethodInvocation::proceed()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 9
cts 11
cp 0.8182
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 11
nc 5
nop 0
crap 4.0961
1
<?php
2
declare(strict_types = 1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2015, 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
/**
15
 * Dynamic closure method invocation is responsible to call dynamic methods via closure
16
 */
17
final class DynamicClosureMethodInvocation extends AbstractMethodInvocation
18
{
19
    /**
20
     * Closure to use
21
     *
22
     * @var \Closure
23
     */
24
    protected $closureToCall;
25
26
    /**
27
     * Previous instance of invocation
28
     *
29
     * @var null|object|string
30
     */
31
    protected $previousInstance;
32
33
    /**
34
     * Invokes original method and return result from it
35
     *
36
     * @return mixed
37
     */
38 7
    public function proceed()
39
    {
40 7
        if (isset($this->advices[$this->current])) {
41 1
            $currentInterceptor = $this->advices[$this->current++];
42
43 1
            return $currentInterceptor->invoke($this);
44
        }
45
46
        // Fill the closure only once if it's empty
47 7
        if ($this->closureToCall === null) {
48 7
            $this->closureToCall    = $this->reflectionMethod->getClosure($this->instance);
49 7
            $this->previousInstance = $this->instance;
50
        }
51
52
        // Rebind the closure if instance was changed since last time
53 7
        if ($this->previousInstance !== $this->instance) {
54
            $this->closureToCall    = $this->closureToCall->bindTo($this->instance, $this->reflectionMethod->class);
55
            $this->previousInstance = $this->instance;
56
        }
57
58 7
        return ($this->closureToCall)(...$this->arguments);
59
    }
60
}
61