Test Failed
Push — master ( 0864dc...48ec37 )
by Petr
12:45
created

DocumentPartDIFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 27
rs 10

2 Methods

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