Completed
Push — 2.x ( 98b57b...8b2aa1 )
by Alexander
05:24
created

IntroductionAspectExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 7.41%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 7
dl 0
loc 82
ccs 2
cts 27
cp 0.0741
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTarget() 0 4 1
A getKind() 0 4 1
A supports() 0 6 2
B load() 0 29 4
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\Core;
12
13
use Go\Aop\Advisor;
14
use Go\Aop\Aspect;
15
use Go\Aop\Framework;
16
use Go\Aop\Pointcut;
17
use Go\Aop\Support;
18
use Go\Lang\Annotation;
19
20
/**
21
 * Introduction aspect extension
22
 */
23
class IntroductionAspectExtension extends AbstractAspectLoaderExtension
24
{
25
26
    /**
27
     * Introduction aspect loader works with annotations from aspect
28
     *
29
     * For extension that works with annotations additional metaInformation will be passed
30
     *
31
     * @return string
32
     */
33
    public function getKind()
34
    {
35
        return self::KIND_ANNOTATION;
36
    }
37
38
    /**
39
     * Introduction aspect loader works only with properties of aspect
40
     *
41
     * @return string|array
42
     */
43 2
    public function getTarget()
44
    {
45 2
        return self::TARGET_PROPERTY;
46
    }
47
48
    /**
49
     * Checks if loader is able to handle specific point of aspect
50
     *
51
     * @param Aspect $aspect Instance of aspect
52
     * @param mixed|\ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflection Reflection of point
53
     * @param mixed|null $metaInformation Additional meta-information, e.g. annotation for method
54
     *
55
     * @return boolean true if extension is able to create an advisor from reflection and metaInformation
56
     */
57
    public function supports(Aspect $aspect, $reflection, $metaInformation = null)
58
    {
59
        return
60
            ($metaInformation instanceof Annotation\DeclareParents) ||
61
            ($metaInformation instanceof Annotation\DeclareError);
62
    }
63
64
    /**
65
     * Loads definition from specific point of aspect into the container
66
     *
67
     * @param Aspect $aspect Instance of aspect
68
     * @param mixed|\ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflection Reflection of point
69
     * @param mixed|null $metaInformation Additional meta-information
70
     *
71
     * @throws \UnexpectedValueException
72
     *
73
     * @return array|Pointcut[]|Advisor[]
74
     */
75
    public function load(Aspect $aspect, $reflection, $metaInformation = null)
76
    {
77
        $loadedItems = [];
78
        $pointcut    = $this->parsePointcut($aspect, $reflection, $metaInformation);
79
        $propertyId  = $reflection->class . '->' . $reflection->name;
80
81
        switch (true) {
82
            case ($metaInformation instanceof Annotation\DeclareParents):
83
                $implement = $metaInformation->defaultImpl;
84
                $interface = $metaInformation->interface;
85
                $advice    = new Framework\TraitIntroductionInfo($implement, $interface);
86
                $advisor   = new Support\DeclareParentsAdvisor($pointcut->getClassFilter(), $advice);
87
                $loadedItems[$propertyId] = $advisor;
88
                break;
89
90
            case (($metaInformation instanceof Annotation\DeclareError) && ($pointcut instanceof Pointcut)):
91
                $reflection->setAccessible(true);
92
                $message = $reflection->getValue($aspect);
93
                $level   = $metaInformation->level;
94
                $advice  = new Framework\DeclareErrorInterceptor($message, $level, $metaInformation->value);
95
                $loadedItems[$propertyId] = new Support\DefaultPointcutAdvisor($pointcut, $advice);
96
                break;
97
98
            default:
99
                throw new \UnexpectedValueException('Unsupported pointcut class: ' . get_class($pointcut));
100
        }
101
102
        return $loadedItems;
103
    }
104
}
105