Completed
Push — master ( 8f61de...6beb52 )
by Vitaly
02:52
created

CollectionMethodResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 3
nc 1
nop 2
crap 1
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\configurator\MethodConfiguratorInterface;
11
use samsonframework\container\metadata\ClassMetadata;
12
use samsonframework\container\resolver\MethodResolverTrait;
13
14
/**
15
 * Collection method resolver class.
16
 * @author Vitaly Iegorov <[email protected]>
17
 */
18
class CollectionMethodResolver extends AbstractCollectionResolver implements CollectionResolverInterface
19
{
20
    use MethodResolverTrait;
21
22
    /** Collection method key */
23
    const KEY = 'methods';
24
25
    /**
26
     * @var  CollectionParameterResolver Parameter resolver
27
     */
28
    protected $parameterResolver;
29
30
    /**
31
     * CollectionMethodResolver constructor.
32
     *
33
     * @param array $collectionConfigurators
34
     * @param CollectionParameterResolver $parameterResolver
35
     *
36
     * @throws \InvalidArgumentException
37
     */
38 1
    public function __construct(array $collectionConfigurators, CollectionParameterResolver $parameterResolver)
39
    {
40 1
        parent::__construct($collectionConfigurators);
41
42 1
        $this->parameterResolver = $parameterResolver;
43 1
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48 1
    public function resolve(array $configurationArray, ClassMetadata $classMetadata)
49
    {
50
        // Iterate collection
51 1
        if (array_key_exists(self::KEY, $configurationArray)) {
52 1
            $reflectionClass = new \ReflectionClass($classMetadata->className);
53
54
            // Iterate configured methods
55 1
            foreach ($configurationArray[self::KEY] as $methodName => $methodDataArray) {
56 1
                $methodMetadata = $this->resolveMethodMetadata(
57 1
                    $reflectionClass->getMethod($methodName),
58
                    $classMetadata
59
                );
60
61
                // Check if methods inject any instances
62 1
                if (array_key_exists(CollectionParameterResolver::KEY, $methodDataArray)) {
63 1
                    foreach ($methodMetadata->parametersMetadata as $parameterMetadata) {
64
                        // If config has parameter
65 1
                        if (array_key_exists($parameterMetadata->name, $methodDataArray[CollectionParameterResolver::KEY])) {
66 1
                            $this->parameterResolver->resolve(
67 1
                                $methodDataArray[CollectionParameterResolver::KEY][$parameterMetadata->name],
68
                                $parameterMetadata
69
                            );
70
71
                            // Store method dependencies
72 1
                            $methodMetadata->dependencies[$parameterMetadata->name] = $parameterMetadata->dependency;
73
                        }
74
                    }
75
                }
76
77
                // Process attributes
78 1
                foreach ($this->getAttributeConfigurator($methodDataArray) as $configurator) {
79
                    /** @var MethodConfiguratorInterface $configurator Parse method metadata */
80 1
                    $configurator->toMethodMetadata($methodMetadata);
81
                }
82
            }
83
        }
84
85 1
        return $classMetadata;
86
    }
87
}
88