DependencyAnnotation   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 92
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAnnotationDefinition() 0 3 1
A getDependencyDefinition() 0 12 3
A getInjectionDefinition() 0 12 2
A getInjectionTarget() 0 9 1
A getInjectionTargetName() 0 14 3
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\IoC\Annotation;
26
27
use Brickoo\Component\Annotation\Annotation;
28
use Brickoo\Component\Annotation\AnnotationReaderResult;
29
use Brickoo\Component\Annotation\Definition\AnnotationDefinition;
30
use Brickoo\Component\IoC\Definition\ArgumentDefinition;
31
use Brickoo\Component\IoC\Definition\Container\ArgumentDefinitionContainer;
32
use Brickoo\Component\IoC\Definition\Container\InjectionDefinitionContainer;
33
use Brickoo\Component\IoC\Definition\DependencyDefinition;
34
use Brickoo\Component\IoC\Definition\InjectionDefinition;
35
36
/**
37
 * DependencyAnnotation
38
 *
39
 * Implements a dependency annotation for dependency injection usage.
40
 * @author Celestino Diaz <[email protected]>
41
 */
42
class DependencyAnnotation {
43
44
    /** Dependency annotation identifier */
45
    const DEPENDENCY_ANNOTATION = "Dependency";
46
47
    /** Regular expression for matching the class target name */
48
    const TARGET_NAME_REGEX = "~^\\\\?[a-zA-Z_\\x7f-\\xff][\\w\\x7f-\\xff\\\\]*\\:\\:(?<target>([a-zA-Z_\\x7f-\\xff]+))$~";
49
50
    /**
51
     * Returns the dependency annotation definition.
52
     * @param integer $target annotation target
53
     * @param boolean $required require annotation
54
     * @return \Brickoo\Component\Annotation\Definition\AnnotationDefinition
55
     */
56 1
    public function getAnnotationDefinition($target = Annotation::TARGET_CLASS, $required = true) {
57 1
        return new AnnotationDefinition(self::DEPENDENCY_ANNOTATION, $target, $required);
58
    }
59
60
    /**
61
     * Returns the dependency definition.
62
     * @param $dependency
63
     * @param integer $scope 1:SINGLETON 2:PROTOTYPE
64
     * @param \Brickoo\Component\Annotation\AnnotationReaderResult $readerResults
65
     * @return \Brickoo\Component\IoC\Definition\DependencyDefinition
66
     */
67 1
    public function getDependencyDefinition($dependency, $scope, AnnotationReaderResult $readerResults) {
68 1
        $injectionDefinitions = [];
69 1
        foreach ($readerResults as $annotation) {
70 1
            if ($annotation->getName() == self::DEPENDENCY_ANNOTATION) {
71 1
                $injectionDefinitions[] = $this->getInjectionDefinition($annotation);
72 1
            }
73 1
        }
74
75 1
        return new DependencyDefinition($dependency, $scope, null,
76 1
            new InjectionDefinitionContainer($injectionDefinitions)
77 1
        );
78
    }
79
80
    /**
81
     * Returns the injection definition from the annotation.
82
     * @param \Brickoo\Component\Annotation\Annotation $annotation
83
     * @return \Brickoo\Component\IoC\Definition\InjectionDefinition
84
     */
85 1
    private function getInjectionDefinition(Annotation $annotation) {
86 1
        $argumentsDefinitions = [];
87 1
        foreach ($annotation->getValues() as $argument) {
88 1
            $argumentsDefinitions[] = new ArgumentDefinition($argument);
89 1
        }
90
91 1
        return new InjectionDefinition(
92 1
            $this->getInjectionTarget($annotation),
93 1
            $this->getInjectionTargetName($annotation),
94 1
            new ArgumentDefinitionContainer($argumentsDefinitions)
95 1
        );
96
    }
97
98
    /**
99
     * Returns the injection target.
100
     * @param \Brickoo\Component\Annotation\Annotation $annotation
101
     * @return string the injection target
102
     */
103 1
    private function getInjectionTarget(Annotation $annotation) {
104
        $annotationTargetsMapping = [
105 1
            Annotation::TARGET_CLASS => InjectionDefinition::TARGET_CONSTRUCTOR,
106 1
            Annotation::TARGET_METHOD => InjectionDefinition::TARGET_METHOD,
107 1
            Annotation::TARGET_PROPERTY => InjectionDefinition::TARGET_PROPERTY
108 1
        ];
109
110 1
        return $annotationTargetsMapping[$annotation->getTarget()];
111
    }
112
113
    /**
114
     * Returns the injection target (method,property) name.
115
     * @param \Brickoo\Component\Annotation\Annotation $annotation
116
     * @return string|null the target name otherwise null on failure
117
     */
118 1
    private function getInjectionTargetName(Annotation $annotation) {
119 1
        if ($annotation->getTarget() == Annotation::TARGET_CLASS) {
120 1
            return $annotation->getTargetLocation();
121
        }
122
123 1
        $targetLocation = null;
124 1
        if (preg_match(
125 1
            self::TARGET_NAME_REGEX,
126 1
            $annotation->getTargetLocation(),
127 1
            $matches)) {
128 1
                $targetLocation = $matches["target"];
129 1
        }
130 1
        return $targetLocation;
131
    }
132
133
}
134