Completed
Pull Request — master (#240)
by Alexander
05:40
created

AnnotationPointcut   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 43.75%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 111
wmc 5
lcom 1
cbo 1
ccs 7
cts 16
cp 0.4375
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A matches() 0 15 2
A getKind() 0 4 1
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2012, 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\Pointcut;
12
13
use Doctrine\Common\Annotations\Reader;
14
use Go\Aop\Pointcut;
15
use ReflectionClass;
16
use ReflectionMethod;
17
use ReflectionProperty;
18
19
/**
20
 * Annotation property pointcut checks property annotation
21
 */
22
class AnnotationPointcut implements Pointcut
23
{
24
    use PointcutClassFilterTrait;
25
    /**
26
     * Annotation class to match
27
     *
28
     * @var string
29
     */
30
    protected $annotationName = '';
31
32
    /**
33
     * Annotation reader
34
     *
35
     * @var null|Reader
36
     */
37
    protected $annotationReader = null;
38
39
    /**
40
     * Kind of current filter, can be KIND_CLASS, KIND_METHOD, KIND_PROPERTY, KIND_TRAIT
41
     *
42
     * @var int
43
     */
44
    protected $filterKind = 0;
45
46
    /**
47
     * Specifies name of the expected class to receive
48
     *
49
     * @var string
50
     */
51
    protected $expectedClass = '';
52
53
    /**
54
     * Method to call for annotation reader
55
     *
56
     * @var string
57
     */
58
    protected $annotationMethod = '';
59
60
    /**
61
     * Static mappings of kind to expected class and method name
62
     *
63
     * @var array
64
     */
65
    protected static $mappings = array(
66
        self::KIND_CLASS => array(
67
            ReflectionClass::class,
68
            'getClassAnnotation'
69
        ),
70
        self::KIND_TRAIT => array(
71
            ReflectionClass::class,
72
            'getClassAnnotation'
73
        ),
74
        self::KIND_METHOD => array(
75
            ReflectionMethod::class,
76
            'getMethodAnnotation'
77
        ),
78
        self::KIND_PROPERTY => array(
79
            ReflectionProperty::class,
80
            'getPropertyAnnotation'
81
        )
82
    );
83
84
    /**
85
     * Annotation matcher constructor
86
     *
87
     * @param integer $filterKind Kind of filter, e.g. KIND_CLASS
88
     * @param Reader $reader Reader of annotations
89
     * @param string $annotationName Annotation class name to match
90
     */
91 3
    public function __construct($filterKind, Reader $reader, $annotationName)
92
    {
93 3
        if (!isset(self::$mappings[$filterKind])) {
94
            throw new \InvalidArgumentException("Unsupported filter kind {$filterKind}");
95
        }
96 3
        $this->filterKind       = $filterKind;
97 3
        $this->annotationName   = $annotationName;
98 3
        $this->annotationReader = $reader;
99
100 3
        list($this->expectedClass, $this->annotationMethod) = self::$mappings[$filterKind];
101 3
    }
102
103
    /**
104
     * @param ReflectionClass|ReflectionMethod|ReflectionProperty $point
105
     * {@inheritdoc}
106
     */
107
    public function matches($point)
108
    {
109
        $expectedClass = $this->expectedClass;
110
        if (!$point instanceof $expectedClass) {
111
            return false;
112
        }
113
114
        // TODO: $aliases = $point->getNamespaceAliases();
115
        // $aliases = array();
116
        // $this->annotationReader->setImports($aliases);
117
118
        $annotation = $this->annotationReader->{$this->annotationMethod}($point, $this->annotationName);
119
120
        return (bool) $annotation;
121
    }
122
123
    /**
124
     * Returns the kind of point filter
125
     *
126
     * @return integer
127
     */
128
    public function getKind()
129
    {
130
        return $this->filterKind;
131
    }
132
}
133