Passed
Push — master ( 448315...dcb6b1 )
by Antarès
02:46
created

AutoConstructReader::getConstructArguments()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 26
ccs 18
cts 18
cp 1
rs 8.5806
cc 4
eloc 17
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Accessible\Reader;
4
5
use \Accessible\Configuration;
6
7
class AutoConstructReader extends Reader
8
{
9
    /**
10
     * The name of the annotation class that define the construct arguments.
11
     *
12
     * @var string
13
     */
14
    private static $constructAnnotationClass = "Accessible\\Annotation\\Construct";
15
16
    /**
17
     * The name of the annotation class that define a property's default value.
18
     *
19
     * @var string
20
     */
21
    private static $initializeAnnotationClass = "Accessible\\Annotation\\Initialize";
22
23
    /**
24
     * The name of the annotation class that define the initial value of an object property.
25
     *
26
     * @var string
27
     */
28
    private static $initializeObjectAnnotationClass = "Accessible\\Annotation\\InitializeObject";
29
30
    /**
31
     * Get the list of needed arguments for given object's constructor.
32
     *
33
     * @param object $object The object to analyze.
34
     *
35
     * @return array The list of arguments.
36
     */
37 29
    public static function getConstructArguments($object)
38
    {
39 29
        $reflectionObject = new \ReflectionObject($object);
40 29
        $cacheId = md5("constructArguments:" . $reflectionObject->getName());
41 29
        $constructArguments = self::getFromCache($cacheId);
42 29
        if ($constructArguments !== null) {
43 1
            return $constructArguments;
44
        }
45
46 28
        $constructArguments = null;
47 28
        $annotationReader = Configuration::getAnnotationReader();
48 28
        $objectClasses = self::getClassesToRead($reflectionObject);
49 28
        array_reverse($objectClasses);
50
51 28
        foreach ($objectClasses as $class) {
52 28
            $annotation = $annotationReader->getClassAnnotation($class, self::$constructAnnotationClass);
53 28
            if ($annotation !== null) {
54 2
                $constructArguments = $annotation->getArguments();
55 2
                break;
56
            }
57 28
        }
58
59 28
        self::saveToCache($cacheId, $constructArguments);
60
61 28
        return $constructArguments;
62
    }
63
64
    /**
65
     * Get the list of properties that have to be initialized automatically
66
     * during the object construction, plus their value.
67
     *
68
     * @param object $object The object to analyze.
69
     *
70
     * @return array The list of properties and values,
71
     *               in the form ["property" => "value"].
72
     */
73 29
    public static function getPropertiesToInitialize($object)
74
    {
75 29
        $reflectionObject = new \ReflectionObject($object);
76 29
        $cacheId = md5("propertiesToInitialize:" . $reflectionObject->getName());
77 29
        $propertiesValues = self::getFromCache($cacheId);
78 29
        if ($propertiesValues !== null) {
79 24
            return $propertiesValues;
80
        }
81
82 6
        $annotationReader = Configuration::getAnnotationReader();
83 6
        $objectClasses = self::getClassesToRead($reflectionObject);
84 6
        array_reverse($objectClasses);
85
86 6
        $propertiesValues = array();
87
88 6
        foreach ($objectClasses as $class) {
89 6
            foreach ($class->getProperties() as $property) {
90 6
                $propertyName = $property->getName();
91
92 6
                $annotation = $annotationReader->getPropertyAnnotation($property, self::$initializeAnnotationClass);
93 6
                $annotationType = "initialize";
94 6
                if ($annotation === null) {
95 6
                    $annotation = $annotationReader->getPropertyAnnotation($property, self::$initializeObjectAnnotationClass);
96 6
                    $annotationType = "initializeObject";
97 6
                }
98
99 6
                if (empty($propertiesValues[$propertyName]) && $annotation !== null) {
100
                    switch ($annotationType) {
101 6
                        case "initialize":
102 6
                            $propertiesValues[$propertyName] = $annotation->getValue();
103 6
                            break;
104 5
                        case "initializeAnnotation":
105
                            $className = $annotation->getClassName();
106
                            $propertiesValues[$propertyName] = new $className();
107
                            break;
108
                    }
109 6
                }
110 6
            }
111 6
        }
112
113 6
        self::saveToCache($cacheId, $propertiesValues);
114
115 6
        return $propertiesValues;
116
    }
117
}
118