DefinitionResolver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 110
ccs 37
cts 37
cp 1
rs 10
c 3
b 1
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolve() 0 4 1
A setResolver() 0 5 1
A getResolver() 0 9 2
A getResolverType() 0 15 2
A getDefinitionResolverByType() 0 15 2
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\Resolver;
26
27
use Brickoo\Component\IoC\DIContainer;
28
use Brickoo\Component\IoC\Definition\DependencyDefinition;
29
use Brickoo\Component\IoC\Resolver\Exception\DefinitionTypeUnknownException;
30
use Brickoo\Component\Common\Assert;
31
32
/**
33
 * DefinitionResolver
34
 *
35
 * Defines an abstract dependency definition resolver.
36
 * @author Celestino Diaz <[email protected]>
37
 */
38
class DefinitionResolver {
39
40
    /** Unsupported definition type. */
41
    const TYPE_UNSUPPORTED = "unsupported";
42
43
    /** Dependency is of type class */
44
    const TYPE_CLASS = "class";
45
46
    /** Dependency is of type closure */
47
    const TYPE_CLOSURE = "closure";
48
49
    /** Dependency is of type object */
50
    const TYPE_OBJECT = "object";
51
52
    /** Dependency is of type callable */
53
    const TYPE_CALLABLE = "callable";
54
55
    /** @var array */
56
    private $resolvers;
57
58 1
    public function __construct() {
59 1
        $this->resolvers = array();
60 1
    }
61
62
    /**
63
     * Resolves a dependency definition.
64
     * @param \Brickoo\Component\IoC\DIContainer $container
65
     * @param \Brickoo\Component\IoC\Definition\DependencyDefinition
66
     * @throws \Brickoo\Component\IoC\Resolver\Exception\DefinitionTypeUnknownException
67
     * @return mixed the resolved definition result
68
     */
69 5
    public function resolve(DIContainer $container, DependencyDefinition $dependencyDefinition) {
70 5
        $dependency = $dependencyDefinition->getDependency();
71 5
        return $this->getResolver($this->getResolverType($dependency), $container)->resolve($dependencyDefinition);
72
    }
73
74
    /**
75
     * Sets a resolver for an explicit type.
76
     * @param string $resolverType
77
     * @param \Brickoo\Component\IoC\Resolver\DependencyResolver $resolver
78
     * @return \Brickoo\Component\IoC\Resolver\DefinitionResolver
79
     */
80 1
    public function setResolver($resolverType, DependencyResolver $resolver) {
81 1
        Assert::isString($resolverType);
82 1
        $this->resolvers[$resolverType] = $resolver;
83 1
        return $this;
84
    }
85
86
    /**
87
     * Returns the corresponding type resolver.
88
     * @param string $definitionType
89
     * @param \Brickoo\Component\IoC\DIContainer $diContainer
90
     * @throws \Brickoo\Component\IoC\Resolver\Exception\DefinitionTypeUnknownException
91
     * @return \Brickoo\Component\IoC\Resolver\DependencyResolver
92
     */
93 5
    private function getResolver($definitionType, DIContainer $diContainer) {
94 5
        if (array_key_exists($definitionType, $this->resolvers)) {
95 1
            return $this->resolvers[$definitionType];
96
        }
97
98 5
        $resolver = $this->getDefinitionResolverByType($definitionType, $diContainer);
99 4
        $this->resolvers[$definitionType] = $resolver;
100 4
        return $resolver;
101
    }
102
103
    /**
104
     * Returns the corresponding type resolver.
105
     * @param mixed $dependency
106
     * @return string
107
     */
108 5
    private function getResolverType($dependency) {
109 5
        $matchingTypes = array_filter(
110
            [
111 5
                self::TYPE_CLOSURE => ($dependency instanceof \Closure),
112 5
                self::TYPE_OBJECT => is_object($dependency),
113 5
                self::TYPE_CALLABLE => is_callable($dependency),
114 5
                self::TYPE_CLASS => (is_string($dependency) && class_exists($dependency)),
115 5
                self::TYPE_UNSUPPORTED => true
116 5
            ],
117
            function($value) {return $value === true;}
118 5
        );
119
120 5
        $types = array_keys($matchingTypes);
121 5
        return array_shift($types);
122
    }
123
124
    /**
125
     * Return the corresponding resolver by type.
126
     * @param string $definitionType
127
     * @param DIContainer $diContainer
128
     * @throws \Brickoo\Component\IoC\Resolver\Exception\DefinitionTypeUnknownException
129
     * @return \Brickoo\Component\IoC\Resolver\DependencyResolver
130
     */
131 5
    private function getDefinitionResolverByType($definitionType, DIContainer $diContainer) {
132
        $resolvers = [
133 5
            self::TYPE_CLOSURE => "\\DependencyClosureResolver",
134 5
            self::TYPE_OBJECT => "\\DependencyObjectResolver",
135 5
            self::TYPE_CALLABLE => "\\DependencyCallableResolver",
136 5
            self::TYPE_CLASS => "\\DependencyClassResolver"
137 5
        ];
138
139 5
        if (!isset($resolvers[$definitionType])) {
140 1
            throw new DefinitionTypeUnknownException($definitionType);
141
        }
142
143 4
        $className = __NAMESPACE__.$resolvers[$definitionType];
144 4
        return new $className($diContainer);
145
    }
146
147
}
148