Completed
Push — master ( 949e77...a1f0e2 )
by
unknown
120:09 queued 86:45
created

SudoMainLocationLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Publish\Core\Helper\ContentInfoLocationLoader;
7
8
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
9
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
10
use eZ\Publish\Core\Helper\ContentInfoLocationLoader;
11
use eZ\Publish\API\Repository\Repository;
12
use Exception;
13
14
/**
15
 * Loads the main location of a given ContentInfo using sudo().
16
 */
17
class SudoMainLocationLoader implements ContentInfoLocationLoader
18
{
19
    /**
20
     * @var \eZ\Publish\API\Repository\Repository|\eZ\Publish\Core\Repository\Repository
21
     */
22
    private $repository;
23
24
    public function __construct(Repository $repository)
25
    {
26
        $this->repository = $repository;
27
    }
28
29
    public function loadLocation(ContentInfo $contentInfo)
30
    {
31
        if (is_null($contentInfo->mainLocationId)) {
32
            throw new NotFoundException('main location of content', $contentInfo->id);
33
        }
34
35
        try {
36
            return $this->repository->sudo(
37
                function (Repository $repository) use ($contentInfo) {
38
                    return $repository->getLocationService()->loadLocation($contentInfo->mainLocationId);
39
                }
40
            );
41
        } catch (Exception $e) {
42
            throw new NotFoundException('main location of content', $contentInfo->id);
43
        }
44
    }
45
}
46