Completed
Push — EZP-31584 ( 18fefe )
by
unknown
19:33
created

PermissionAwareLocationResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
nc 1
nop 1
rs 10
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\LocationResolver;
10
11
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
12
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
13
use eZ\Publish\API\Repository\LocationService;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\API\Repository\Values\Content\Location;
16
17
final class PermissionAwareLocationResolver implements LocationResolver
18
{
19
    /** @var \eZ\Publish\API\Repository\LocationService */
20
    private $locationService;
21
22
    public function __construct(LocationService $locationService)
23
    {
24
        $this->locationService = $locationService;
25
    }
26
27
    /**
28
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
29
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
30
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
31
     */
32
    public function resolveLocation(ContentInfo $contentInfo): Location
33
    {
34
        try {
35
            $location = $this->locationService->loadLocation($contentInfo->mainLocationId);
36
        } catch (NotFoundException | UnauthorizedException $e) {
37
            // try different locations if main location is not accessible for the user
38
            $locations = $this->locationService->loadLocations($contentInfo);
39
            if (empty($locations)) {
40
                throw $e;
41
            }
42
43
            // foreach to keep forward compatibility with a type of returned loadLocations() result
44
            foreach ($locations as $location) {
45
                return $location;
46
            }
47
        }
48
49
        return $location;
50
    }
51
}
52