Completed
Push — location_references ( 47d67d )
by
unknown
13:55
created

LocationReference::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace eZ\Publish\Core\LocationReference;
6
7
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
8
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
9
use eZ\Publish\API\Repository\Values\Content\Location;
10
11
final class LocationReference
12
{
13
    /** @var \eZ\Publish\Core\LocationReference\LocationReferenceResolverInterface */
14
    private $resolver;
15
16
    /** @var string */
17
    private $reference;
18
19
    public function __construct(LocationReferenceResolverInterface $resolver, string $reference)
20
    {
21
        $this->reference = $reference;
22
        $this->resolver = $resolver;
23
    }
24
25
    public function getLocation(): Location
26
    {
27
        return $this->resolver->resolve($this->reference);
28
    }
29
30
    public function getLocationOrNull(): ?Location
31
    {
32
        try {
33
            return $this->resolver->resolve($this->reference);
34
        } catch (NotFoundException | UnauthorizedException $e) {
35
            return null;
36
        }
37
    }
38
39
    public function getLocationOrDefault(Location $default): Location
40
    {
41
        try {
42
            return $this->resolver->resolve($this->reference);
43
        } catch (NotFoundException | UnauthorizedException $e) {
44
            return $default;
45
        }
46
    }
47
48
    public function __invoke(): Location
49
    {
50
        return $this->getLocation();
51
    }
52
53
    public function __toString()
54
    {
55
        return $this->reference;
56
    }
57
}
58