Passed
Push — master ( 159b17...86ca6d )
by Rustam
02:24
created

PanelCollection::validatePanels()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Viewer;
6
7
use Yiisoft\Yii\Debug\Viewer\Panels\PanelInterface;
8
9
final class PanelCollection
10
{
11
    /**
12
     * @var array<string, PanelInterface>
13
     */
14
    private array $panels;
15
16
    /**
17
     * PanelCollection constructor.
18
     */
19
    public function __construct(array $panels)
20
    {
21
        $this->validatePanels($panels);
22
        $this->panels = $panels;
23
    }
24
25
    public function addPanel(string $id, PanelInterface $panel, bool $override = false): void
26
    {
27
        if (!$override && isset($this->panels[$id])) {
28
            throw new \RuntimeException('Panel already exists.');
29
        }
30
31
        $this->panels[$id] = $panel;
32
    }
33
34
    public function getPanel(string $id): PanelInterface
35
    {
36
        if (!isset($this->panels[$id])) {
37
            throw new \InvalidArgumentException(sprintf('Panel "%s" doesn\'t exist.', $id));
38
        }
39
        return $this->panels[$id];
40
    }
41
42
    /**
43
     * @return array<string, PanelInterface>
44
     */
45
    public function getPanels(): array
46
    {
47
        return $this->panels;
48
    }
49
50
    private function validatePanels(array $panels): void
51
    {
52
        foreach ($panels as $panel) {
53
            if (!($panel instanceof PanelInterface)) {
54
                throw new \RuntimeException(
55
                    sprintf('Panel must implement PanelInterface. Got: %s.', get_debug_type($panel))
56
                );
57
            }
58
        }
59
    }
60
}
61