ResolvesDependency::resolve()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 9
nop 3
dl 0
loc 29
ccs 0
cts 16
cp 0
crap 42
rs 9.0111
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Middleware;
10
11
use Auryn\Injector;
12
use Daikon\Interop\Assertion;
13
use Daikon\Interop\RuntimeException;
14
use Psr\Container\ContainerInterface;
15
16
trait ResolvesDependency
17
{
18
    /**
19
     * @param mixed $dependency
20
     * @return callable|object
21
     */
22
    private function resolve(ContainerInterface $container, $dependency, string $stereoType)
23
    {
24
        if (is_string($dependency)) {
25
            Assertion::classExists($dependency, "Given dependency '$dependency' not found.");
26
            $dependency = $container->get($dependency);
27
        } elseif (is_array($dependency) && count($dependency) === 2) {
28
            $fqcn = $dependency[0];
29
            $params = $dependency[1];
30
            Assertion::classExists($fqcn, "Given dependency '$fqcn' not found.");
31
            Assertion::isArray($params, 'Dependency parameters must be an array.');
32
            $dependency = $container->get(Injector::class)->make($fqcn, $params);
33
        }
34
35
        if (is_object($dependency)) {
36
            Assertion::isInstanceOf(
37
                $dependency,
38
                $stereoType,
39
                sprintf("Given dependency '%s' is not a '$stereoType'.", get_class($dependency))
40
            );
41
            return $dependency;
42
        }
43
44
        if (is_callable($dependency)) {
45
            return $dependency;
46
        }
47
48
        //@better debug input
49
        throw new RuntimeException(
50
            sprintf("Given dependency '%s' is not a '$stereoType'.", gettype($dependency))
51
        );
52
    }
53
}
54