ConfigFileServiceFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 10
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Gr8abbasi\Container\Factory;
4
5
use Gr8abbasi\Container\Container;
6
use Gr8abbasi\Container\Exception\NotFoundException;
7
use Gr8abbasi\Container\Factory\ServiceFactoryInterface;
8
9
/**
10
 * ConfigFileServiceFactory
11
 */
12
class ConfigFileServiceFactory implements ServiceFactoryInterface
13
{
14
    /**
15
     * Resolve the service arguments
16
     *
17
     * @param array $serviceArguments
18
     * @param Container $container
19
     *
20
     * @return mixed $service
21
     */
22 3
    private function resolveArguments($serviceArguments, Container $container)
23
    {
24 3
        $arguments = [];
25
26 3
        foreach ($serviceArguments as $argument) {
27 3
            $arguments[] = $container->get($argument);
28 2
        }
29
30 2
        return $arguments;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36 4
    public function create($service, Container $container)
37
    {
38 4
        if (!class_exists($service['class'])) {
39 1
            throw new NotFoundException(
40 1
                'Class does not exists: ' . $service['class']
41 1
            );
42
        }
43
44 3
        $class = new \ReflectionClass($service['class']);
45 3
        $arguments = isset($service['arguments']) ? $service['arguments'] : [];
46
47 3
        return $class->newInstanceArgs(
48 3
            $this->resolveArguments(
49 3
                $arguments,
50
                $container
51 3
            )
52 2
        );
53
    }
54
}
55