Completed
Pull Request — 1.x (#286)
by Alexander
02:43
created

DeclareErrorInterceptor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
c 3
b 1
f 0
lcom 1
cbo 1
dl 0
loc 105
ccs 0
cts 47
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A serialize() 0 7 1
A unserialize() 0 8 2
A getDeclareErrorAdvice() 0 16 3
A invoke() 0 13 3
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2013, 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
use Go\Aop\Intercept\Joinpoint;
14
use ReflectionMethod;
15
use ReflectionProperty;
16
17
/**
18
 * Interceptor to dynamically trigger an user notice/warning/error on method call
19
 *
20
 * This interceptor can be used as active replacement for the "deprecated" tag or to notify about
21
 * probable issues with specific method.
22
 */
23
class DeclareErrorInterceptor extends BaseInterceptor
24
{
25
26
    /**
27
     * Error message to show for this interceptor
28
     *
29
     * @var string
30
     */
31
    private $message = '';
32
33
    /**
34
     * Default level of error
35
     *
36
     * @var int
37
     */
38
    private $level = E_USER_NOTICE;
39
40
    /**
41
     * Default constructor for interceptor
42
     *
43
     * @param string $message Text message for error
44
     * @param int $level Level of error
45
     * @param string|null $pointcutExpression Pointcut expression
46
     */
47
    public function __construct($message, $level, $pointcutExpression)
48
    {
49
        $adviceMethod  = self::getDeclareErrorAdvice();
50
        $this->message = $message;
51
        $this->level   = $level;
52
        parent::__construct($adviceMethod, -256, $pointcutExpression);
53
    }
54
55
    /**
56
     * Serializes an interceptor into string representation
57
     *
58
     * @return string the string representation of the object or null
59
     */
60
    public function serialize()
61
    {
62
        $vars = array_filter(get_object_vars($this));
63
        unset($vars['adviceMethod']);
64
65
        return serialize($vars);
66
    }
67
68
    /**
69
     * Unserialize an interceptor from the string
70
     *
71
     * @param string $serialized The string representation of the object.
72
     * @return void
73
     */
74
    public function unserialize($serialized)
75
    {
76
        $vars = unserialize($serialized);
77
        $vars['adviceMethod'] = self::getDeclareErrorAdvice();
78
        foreach ($vars as $key=>$value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
79
            $this->$key = $value;
80
        }
81
    }
82
83
    /**
84
     * Returns an advice
85
     *
86
     * @return \Closure
87
     */
88
    private static function getDeclareErrorAdvice()
89
    {
90
        static $adviceMethod = null;
91
        if (!$adviceMethod) {
92
            $adviceMethod = function($object, $reflectorName, $message, $level = E_USER_NOTICE) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
93
                $class   = is_string($object) ? $object : get_class($object);
94
                $message = vsprintf('[AOP Declare Error]: %s has an error: "%s"', array(
95
                    $class . '->' . $reflectorName,
96
                    $message
97
                ));
98
                trigger_error($message, $level);
99
            };
100
        }
101
102
        return $adviceMethod;
103
    }
104
105
    /**
106
     * Implement this method to perform extra treatments before and
107
     * after the invocation. Polite implementations would certainly
108
     * like to invoke {@link Joinpoint::proceed()}.
109
     *
110
     * @param Joinpoint $joinpoint the method invocation joinpoint
111
     *
112
     * @return mixed the result of the call to {@link Joinpoint::proceed()}
113
     */
114
    public function invoke(Joinpoint $joinpoint)
115
    {
116
        /** @var ReflectionMethod|ReflectionProperty $reflection */
117
        $reflection    = $joinpoint->getStaticPart();
118
        $reflectorName = 'unknown';
119
        if ($reflection && method_exists($reflection, 'getName')) {
120
            $reflectorName = $reflection->getName();
121
        }
122
        $adviceMethod = $this->adviceMethod;
123
        $adviceMethod($joinpoint->getThis(), $reflectorName, $this->message, $this->level);
124
125
        return $joinpoint->proceed();
126
    }
127
}
128