MethodInjector::inject()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
rs 8.439
cc 5
eloc 15
nc 8
nop 3
1
<?php
2
3
namespace DICIT\Injectors;
4
5
use DICIT\Injector;
6
use DICIT\Container;
7
8
class MethodInjector implements Injector
9
{
10
11
    public function inject(Container $container, $service, array $serviceConfig)
12
    {
13
        $callConfig = array();
14
15
        if (array_key_exists('call', $serviceConfig)) {
16
            $callConfig = $serviceConfig['call'];
17
        }
18
19
        foreach($callConfig as $methodName => $parameters) {
20
            if (false !== strpos($methodName, '[')) {
21
                if (preg_match('`^([^\[]*)\[[0-9]*\]$`i', $methodName, $matches)) {
22
                    $methodToCall = $matches[1];
23
                }
24
                else {
25
                    throw new \RuntimeException(sprintf("Invalid method name '%s'", $methodName));
26
                }
27
            }
28
            else {
29
                $methodToCall = $methodName;
30
            }
31
32
            $convertedParameters = $container->resolveMany($parameters);
33
            call_user_func_array(array($service, $methodToCall), $convertedParameters);
34
        }
35
36
        return true;
37
    }
38
}
39