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

LocatorBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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