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

LocatorBuilder::addReference()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
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