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

LocationReference   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getLocation() 0 4 1
A getLocationOrNull() 0 8 2
A getLocationOrDefault() 0 8 2
A __invoke() 0 4 1
A __toString() 0 4 1
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