Passed
Push — master ( ff69b2...47bc7c )
by Edward
04:03
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 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Pointer\Locator;
5
6
final class LocatorBuilder implements LocatorBuilderInterface
7
{
8
9
    private $locator;
10
11
    private $referenceFactory;
12
13
    private $references = [];
14
15 1
    public static function create(): LocatorBuilderInterface
16
    {
17 1
        return new self(new ReferenceFactory);
18
    }
19
20 6
    public function __construct(ReferenceFactoryInterface $referenceFactory)
21
    {
22 6
        $this->referenceFactory = $referenceFactory;
23 6
    }
24
25 3
    public function addReference(string $text): void
26
    {
27 3
        if (isset($this->locator)) {
28 1
            throw new Exception\LocatorAlreadyBuiltException;
29
        }
30
31
        $reference = $this
32 2
            ->referenceFactory
33 2
            ->createReference($text);
34 2
        $this->references[] = $reference;
35 2
    }
36
37 4
    public function getLocator(): LocatorInterface
38
    {
39 4
        if (!isset($this->locator)) {
40 4
            $this->locator = new Locator(...$this->references);
41
        }
42
43 4
        return $this->locator;
44
    }
45
}
46