Completed
Push — 2.0 ( a1ad31...edb14a )
by Andrzej
02:52
created

SetterResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 0
cbo 0
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 6 1
B mergeSetters() 0 17 5
1
<?php
2
/**
3
 * This file is part of the Stack package.
4
 *
5
 * (c) Andrzej Kostrzewa <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Stack\DI\Resolver;
12
13
14
use Stack\DI\Exception;
15
use Stack\DI\Injection\LazyInterface;
16
17
class SetterResolver
18
{
19
    public function resolve($class, $setters, $mergeSetters = [])
20
    {
21
        $this->mergeSetters($class, $setters, $mergeSetters);
22
23
        return $setters;
24
    }
25
26
    /**
27
     * Merges the setters with overrides; also invokes Lazy values.
28
     *
29
     * @param string $class        The setters are on this class.
30
     * @param array  $setters      The class setters.
31
     * @param array  $mergeSetters Override with these setters.
32
     *
33
     * @throws Exception\SetterMethodNotFound
34
     *
35
     * @return null
36
     */
37
    protected function mergeSetters($class, &$setters, array $mergeSetters = [])
38
    {
39
        if (empty($mergeSetters)) {
40
            return;
41
        }
42
43
        $setters = array_merge($setters, $mergeSetters);
44
        foreach ($setters as $method => $value) {
45
            if (!method_exists($class, $method)) {
46
                throw Exception::setterMethodNotFound($class, $method);
47
            }
48
49
            if ($value instanceof LazyInterface) {
50
                $setters[$method] = $value();
51
            }
52
        }
53
    }
54
}