Test Failed
Push — master ( a52ae2...22d245 )
by Hannes
01:58
created

ExampleRegistry::hasExample()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester\Example;
6
7
use hanneskod\readmetester\Name\NameInterface;
8
9
final class ExampleRegistry implements RegistryInterface
10
{
11
    /** @var ExampleInterface[] */
12
    private $examples = [];
13
14
    public function setExample(ExampleInterface $example): void
15
    {
16
        if ($example->getName()->isUnnamed()) {
17
            $this->examples[] = $example;
18
            return;
19
        }
20
21
        $this->examples[$example->getName()->getCompleteName()] = $example;
22
    }
23
24
    public function hasExample(NameInterface $name): bool
25
    {
26
        return !$name->isUnnamed() && isset($this->examples[$name->getCompleteName()]);
27
    }
28
29
    public function getExample(NameInterface $name): ExampleInterface
30
    {
31
        if (!isset($this->examples[$name->getCompleteName()])) {
32
            throw new \RuntimeException("Example '{$name->getShortName()}' does not exist");
33
        }
34
35
        return $this->examples[$name->getCompleteName()];
36
    }
37
38
    public function getExamples(): array
39
    {
40
        return array_values($this->examples);
41
    }
42
}
43