Completed
Push — 1.x ( a98302...e42f79 )
by Alexander
07:32
created

FluentInterfaceAspect::aroundMethodExecution()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2014, 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 Demo\Aspect;
12
13
use Go\Aop\Aspect;
14
use Go\Aop\Intercept\MethodInvocation;
15
use Go\Lang\Annotation\Around;
16
17
/**
18
 * Fluent interface aspect provides an easy way to reuse "chain" interface for classes
19
 *
20
 * Basically, it uses around method to intercept all public setters in the class that implements
21
 * special marker interface FluentInterface. Then it checks the return value for setter, if it's null,
22
 * then advice replaces it with reference to the object "$this".
23
 *
24
 * @see http://go.aopphp.com/blog/2013/03/19/implementing-fluent-interface-pattern-in-php/
25
 */
26
class FluentInterfaceAspect implements Aspect
27
{
28
    /**
29
     * Fluent interface advice
30
     *
31
     * @Around("within(Demo\Aspect\FluentInterface+) && execution(public **->set*(*))")
32
     *
33
     * @param MethodInvocation $invocation
34
     * @return mixed|null|object
35
     */
36
    protected function aroundMethodExecution(MethodInvocation $invocation)
37
    {
38
        $result = $invocation->proceed();
39
40
        return $result !== null ? $result : $invocation->getThis();
41
    }
42
}
43