DocumentPartDIFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 27
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPart() 0 18 4
A __construct() 0 3 1
1
<?php
2
3
namespace kalanis\Pohoda\DI;
4
5
use kalanis\Pohoda\Document;
6
use kalanis\PohodaException;
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
10
final class DocumentPartDIFactory implements DocumentPartFactoryInterface
11
{
12 4
    public function __construct(
13
        protected readonly ContainerInterface $container,
14 4
    ) {}
15
16
    /**
17
     * {@inheritDoc}
18
     */
19 4
    public function getPart(string $parentClass, string $name): Document\AbstractPart
20
    {
21 4
        $className = $parentClass . '\\' . $name;
22 4
        if (!$this->container->has($className)) {
23 1
            throw new PohodaException('Entity does not exists: ' . $name);
24
        }
25
26
        try {
27 3
            $instance = $this->container->get($className);
28 1
        } catch (ContainerExceptionInterface $ex) {
29 1
            throw new PohodaException('Entity cannot be initialized: ' . $name, 0, $ex);
30
        }
31
32 2
        if ($instance instanceof Document\AbstractPart) {
33 1
            return $instance;
34
        }
35
36 1
        throw new PohodaException('Entity is not an instance of AbstractPart: ' . $name);
37
    }
38
}
39