MapperAbstract   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 57
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B get() 0 29 5
A has() 0 14 3
getFactoryClassName() 0 1 ?
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