DocumentPartDIFactory::getPart()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.9332
cc 4
nc 4
nop 2
crap 4
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