Completed
Push — master ( 821e5b...8cc443 )
by Vitaly
24s
created

CollectionPropertyResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 50
ccs 18
cts 18
cp 1
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C resolve() 0 41 7
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 29.07.2016
6
 * Time: 21:38.
7
 */
8
namespace samsonframework\container\collection;
9
10
use samsonframework\container\annotation\AnnotationPropertyResolver;
11
use samsonframework\container\configurator\PropertyConfiguratorInterface;
12
use samsonframework\container\metadata\ClassMetadata;
13
use samsonframework\container\metadata\PropertyMetadata;
14
15
/**
16
 * Array property resolver class.
17
 * @author Vitaly Iegorov <[email protected]>
18
 */
19
class CollectionPropertyResolver extends AbstractCollectionResolver implements CollectionResolverInterface
20
{
21
    /** Collection class key */
22
    const KEY = 'properties';
23
24
    /**
25
     * {@inheritDoc}
26
     */
27 1
    public function resolve(array $classDataArray, ClassMetadata $classMetadata)
28
    {
29
        // Iterate collection
30 1
        if (array_key_exists(self::KEY, $classDataArray)) {
31 1
            $reflectionClass = new \ReflectionClass($classMetadata->className);
32
            // Iterate configured properties
33 1
            foreach ($classDataArray[self::KEY] as $propertyName => $propertyDataArray) {
34 1
                $propertyReflection = $reflectionClass->getProperty($propertyName);
35
36
                // Create method metadata instance
37
                // TODO This code are identical with AnnotationPropertyResolver
38 1
                $propertyMetadata = new PropertyMetadata($classMetadata);
39 1
                $propertyMetadata->name = $propertyReflection->getName();
40 1
                $propertyMetadata->modifiers = $propertyReflection->getModifiers();
41 1
                $propertyMetadata->isPublic = $propertyReflection->isPublic();
42
43
                // Parse property type hint if present
44 1
                if (preg_match(AnnotationPropertyResolver::P_PROPERTY_TYPE_HINT, $propertyReflection->getDocComment(), $matches)) {
45 1
                    list(, $propertyMetadata->typeHint) = $matches;
46
                }
47
48
                // Iterate collection
49 1
                if (array_key_exists('@attributes', $propertyDataArray)) {
50
                    // Iterate collection attribute configurators
51 1
                    foreach ($this->collectionConfigurators as $key => $collectionConfigurator) {
52
                        // If this is supported collection configurator
53 1
                        if (array_key_exists($key, $propertyDataArray['@attributes'])) {
54
                            /** @var PropertyConfiguratorInterface $configurator Create instance */
55 1
                            $configurator = new $collectionConfigurator($propertyDataArray['@attributes'][$key]);
56
                            // Fill in class metadata
57 1
                            $configurator->toPropertyMetadata($propertyMetadata);
58
                        }
59
                    }
60
                }
61
                // Save property metadata
62 1
                $classMetadata->propertiesMetadata[$propertyMetadata->name] = $propertyMetadata;
63
            }
64
        }
65
66 1
        return $classMetadata;
67
    }
68
}
69