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

CollectionMethodResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 8
c 5
b 1
f 2
lcom 1
cbo 7
dl 0
loc 70
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C resolve() 0 39 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\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