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

UiElementRegistry   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUiElements() 0 3 1
A addUiElement() 0 5 1
A getUiElement() 0 7 2
A hasUiElement() 0 3 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