Passed
Push — master ( 3d52a7...3a32e2 )
by Edward
02:11
created

LocatorBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 33
ccs 14
cts 14
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocator() 0 7 2
A addReference() 0 10 2
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Pointer\Parser;
5
6
use Remorhaz\JSON\Pointer\Locator\Locator;
7
use Remorhaz\JSON\Pointer\Locator\LocatorInterface;
8
use Remorhaz\JSON\Pointer\Locator\ReferenceFactoryInterface;
9
10
final class LocatorBuilder implements LocatorBuilderInterface
11
{
12
13
    private $locator;
14
15
    private $referenceFactory;
16
17
    private $references = [];
18
19 5
    public function __construct(ReferenceFactoryInterface $referenceFactory)
20
    {
21 5
        $this->referenceFactory = $referenceFactory;
22 5
    }
23
24 3
    public function addReference(string $text): void
25
    {
26 3
        if (isset($this->locator)) {
27 1
            throw new Exception\LocatorAlreadyBuiltException;
28
        }
29
30
        $reference = $this
31 2
            ->referenceFactory
32 2
            ->createReference($text);
33 2
        $this->references[] = $reference;
34 2
    }
35
36 4
    public function getLocator(): LocatorInterface
37
    {
38 4
        if (!isset($this->locator)) {
39 4
            $this->locator = new Locator(...$this->references);
40
        }
41
42 4
        return $this->locator;
43
    }
44
}
45