MapperAbstract::get()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 2
crap 5
1
<?php
2
declare(strict_types=1);
3
4
namespace WShafer\PSR11FlySystem;
5
6
use Psr\Container\ContainerInterface;
7
use WShafer\PSR11FlySystem\Exception\InvalidConfigException;
8
9
abstract class MapperAbstract implements MapperInterface
10
{
11
    /** @var ContainerInterface */
12
    protected $container;
13
14 13
    public function __construct(ContainerInterface $container)
15
    {
16 13
        $this->container = $container;
17 13
    }
18
19 4
    public function get(string $type, array $options)
20
    {
21 4
        if ($this->container->has($type)) {
22 1
            return $this->container->get($type);
23
        }
24
25 3
        $className = $this->getFactoryClassName($type);
26
27 3
        if (!$className) {
28 1
            throw new InvalidConfigException(
29 1
                'Unable to locate a factory by the name of: '.$type
30
            );
31
        }
32
33
        /** @var FactoryInterface $factory */
34 2
        $factory = new $className();
35
36 2
        if (!$factory instanceof FactoryInterface) {
37 1
            throw new InvalidConfigException(
38 1
                'Class '.$className.' must be an instance of '.FactoryInterface::class
39
            );
40
        }
41
42 1
        if ($factory instanceof ContainerAwareInterface) {
43 1
            $factory->setContainer($this->container);
44
        }
45
46 1
        return $factory($options);
47
    }
48
49 3
    public function has(string $type)
50
    {
51 3
        if ($this->container->has($type)) {
52 1
            return true;
53
        }
54
55 2
        $className = $this->getFactoryClassName($type);
56
57 2
        if (!$className) {
58 1
            return false;
59
        }
60
61 1
        return true;
62
    }
63
64
    abstract public function getFactoryClassName(string $type);
65
}
66