Completed
Push — master ( 21ab66...244a83 )
by Jacques
19s queued 12s
created

UiElementRegistry::getUiElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace MonsieurBiz\SyliusRichEditorPlugin\UiElement;
5
6
use MonsieurBiz\SyliusRichEditorPlugin\Exception\UiElementNotFoundException;
7
use Webmozart\Assert\Assert;
8
9
final class UiElementRegistry implements UiElementRegistryInterface
10
{
11
    /**
12
     * @var UiElementInterface[]
13
     */
14
    private $uiElements = [];
15
16
    /**
17
     * @inheritDoc
18
     */
19
    public function addUiElement(UiElementInterface $uiElement): void
20
    {
21
        Assert::keyNotExists($this->uiElements, $uiElement->getType(), 'UiElement with type "%s" is already registered.');
22
23
        $this->uiElements[$uiElement->getType()] = $uiElement;
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function hasUiElement(string $type): bool
30
    {
31
        return array_key_exists($type, $this->uiElements);
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function getUiElement(string $type): UiElementInterface
38
    {
39
        if (!$this->hasUiElement($type)) {
40
            throw new UiElementNotFoundException($type);
41
        }
42
43
        return $this->uiElements[$type];
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function getUiElements(): array
50
    {
51
        return $this->uiElements;
52
    }
53
}
54