Completed
Push — 2.0 ( 3eee89...cc0c3c )
by Andrzej
03:23
created

SetterResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 6
c 3
b 2
f 0
lcom 0
cbo 0
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 8 2
A mergeSetters() 0 13 4
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
/**
18
 * Class SetterResolver
19
 *
20
 * @author Andrzej Kostrzewa <[email protected]>
21
 */
22
class SetterResolver
23
{
24
    /**
25
     * Resolve class setters params
26
     *
27
     * @param $class
28
     * @param $setters
29
     * @param array $mergeSetters
30
     *
31
     * @throws Exception\SetterMethodNotFound
32
     *
33
     * @return mixed
34
     */
35
    public function resolve($class, $setters, $mergeSetters = [])
36
    {
37
        if (!empty($mergeSetters)) {
38
            $this->mergeSetters($class, $setters, $mergeSetters);
39
        }
40
41
        return $setters;
42
    }
43
44
    /**
45
     * Merges the setters with overrides; also invokes Lazy values.
46
     *
47
     * @param string $class        The setters are on this class.
48
     * @param array  $setters      The class setters.
49
     * @param array  $mergeSetters Override with these setters.
50
     *
51
     * @throws Exception\SetterMethodNotFound
52
     *
53
     * @return null
54
     */
55
    protected function mergeSetters($class, &$setters, array $mergeSetters = [])
56
    {
57
        $setters = array_merge($setters, $mergeSetters);
58
        foreach ($setters as $method => $value) {
59
            if (!method_exists($class, $method)) {
60
                throw Exception::setterMethodNotFound($class, $method);
61
            }
62
63
            if ($value instanceof LazyInterface) {
64
                $setters[$method] = $value();
65
            }
66
        }
67
    }
68
}
69