Completed
Push — master ( cdbc95...331c43 )
by Miloš
01:18
created

FactoryResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 7
cts 11
cp 0.6364
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 19 4
1
<?php
2
3
namespace Laganica\Di\Resolver;
4
5
use InvalidArgumentException;
6
use Laganica\Di\Definition\ClosureDefinition;
7
use Laganica\Di\Definition\DefinitionInterface;
8
use Laganica\Di\Definition\FactoryDefinition;
9
use Laganica\Di\Exception\ContainerException;
10
use Laganica\Di\FactoryInterface;
11
12
/**
13
 * Class FactoryResolver
14
 *
15
 * @package Laganica\Di\Resolver
16
 */
17
class FactoryResolver extends Resolver
18
{
19
    /**
20
     * @inheritDoc
21
     */
22 2
    public function resolve(DefinitionInterface $definition)
23
    {
24 2
        if (!$definition instanceof FactoryDefinition) {
25
            $actualType = gettype($definition);
26
            $expectedType = FactoryDefinition::class;
27
28
            throw new InvalidArgumentException("Argument \$definition must be $expectedType, $actualType given");
29
        }
30
31 2
        $factoryClass = $definition->getFactoryClass();
32 2
        $factoryInterface = FactoryInterface::class;
33 2
        $interfaces = class_implements($factoryClass);
34
35 2
        if (empty($interfaces) || !in_array($factoryInterface, $interfaces, true)) {
36
            throw new ContainerException("$factoryClass must implement $factoryInterface");
37
        }
38
39 2
        return (new $factoryClass)($this->getContainer());
40
    }
41
}
42