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

ExampleRegistry   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExamples() 0 3 1
A setExample() 0 8 2
A hasExample() 0 3 2
A getExample() 0 7 2
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