InstanceHandler::handle()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 16
rs 9.6111
1
<?php
2
3
namespace Tleckie\Di\Definition\Handler;
4
5
use ReflectionException;
6
use Tleckie\Di\Exception\ContainerException;
7
8
/**
9
 * Class InstanceHandler
10
 *
11
 * @package Tleckie\Di\Handler\Handler
12
 * @author  Teodoro Leckie Westberg <[email protected]>
13
 */
14
class InstanceHandler extends Handler
15
{
16
    /**
17
     * @param $value
18
     * @return mixed
19
     * @throws ContainerException
20
     */
21
    public function handle($value): mixed
22
    {
23
        if (is_array($value) && isset($value['className'])) {
24
            try {
25
                $object = $this->createInstance($value['className'], $value['arguments'] ?? []);
26
                foreach ($value['methods'] ?? [] as $method) {
27
                    $this->callMethod($object, $method['methodName'], $method['arguments'] ?? []);
28
                }
29
            } catch (ReflectionException $exception) {
30
                throw new ContainerException($exception->getMessage());
31
            }
32
33
            return $object;
34
        }
35
36
        return parent::handle($value);
37
    }
38
}
39