Completed
Push — ezp_30797 ( c9135e...a01256 )
by
unknown
19:32
created

loadMainLocationByContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributd with this source code.
8
 */
9
namespace eZ\Publish\Core\Helper;
10
11
use eZ\Publish\API\Repository\ContentService;
12
use eZ\Publish\API\Repository\LocationService;
13
use eZ\Publish\API\Repository\Values\Content\Content as APIContent;
14
use eZ\Publish\API\Repository\Values\Content\Location as APILocation;
15
use eZ\Publish\Core\Repository\Values\Content\Location;
16
use eZ\Publish\SPI\Persistence\Content\Location\Handler as PersistenceLocationHandler;
17
18
/**
19
 * Provides location(s) for a content. Handles unpublished content that does not have an actual location yet.
20
 */
21
class PreviewLocationProvider
22
{
23
    /** @var \eZ\Publish\API\Repository\LocationService */
24
    private $locationService;
25
26
    /** @var \eZ\Publish\API\Repository\ContentService */
27
    private $contentService;
28
29
    /** @var \eZ\Publish\SPI\Persistence\Content\Location\Handler */
30
    private $locationHandler;
31
32
    /**
33
     * @param \eZ\Publish\API\Repository\LocationService $locationService
34
     * @param \eZ\Publish\API\Repository\ContentService $contentService
35
     * @param \eZ\Publish\SPI\Persistence\Content\Location\Handler $locationHandler
36
     */
37
    public function __construct(
38
        LocationService $locationService,
39
        ContentService $contentService,
40
        PersistenceLocationHandler $locationHandler
41
    ) {
42
        $this->locationService = $locationService;
43
        $this->contentService = $contentService;
44
        $this->locationHandler = $locationHandler;
45
    }
46
47
    /**
48
     * Loads the main location for $contentId.
49
     *
50
     * If the content does not have a location (yet), but has a Location draft, it is returned instead.
51
     * Location drafts do not have an id (it is set to null), and can be tested using the isDraft() method.
52
     *
53
     * If the content doesn't have a location nor a location draft, null is returned.
54
     *
55
     * @deprecated Since 7.5.4, rather use loadMainLocationByContent.
56
     * @see loadMainLocationByContent
57
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
58
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
59
     * @param mixed $contentId
60
     *
61
     * @return \eZ\Publish\API\Repository\Values\Content\Location|null
62
     */
63
    public function loadMainLocation($contentId)
64
    {
65
        return $this->loadMainLocationByContent(
66
            $this->contentService->loadContent($contentId)
67
        );
68
    }
69
70
    /**
71
     * Loads the main location for $content.
72
     *
73
     * If the content does not have a location (yet), but has a Location draft, it is returned instead.
74
     * Location drafts do not have an id (it is set to null), and can be tested using the isDraft() method.
75
     *
76
     * If the content doesn't have a location nor a location draft, null is returned.
77
     *
78
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
79
     *
80
     * @return \eZ\Publish\API\Repository\Values\Content\Location|null
81
     */
82
    public function loadMainLocationByContent(APIContent $content): ?APILocation
83
    {
84
        $location = null;
85
        $contentInfo = $content
86
            ->getVersionInfo()
87
            ->getContentInfo();
88
89
        // mainLocationId already exists, content has been published at least once.
90
        if ($contentInfo->mainLocationId) {
91
            $location = $this->locationService->loadLocation($contentInfo->mainLocationId);
92
        } elseif (!$contentInfo->published) {
93
            // New Content, never published, create a virtual location object.
94
            // In cases content is missing locations this will return empty array
95
            $parentLocations = $this->locationHandler->loadParentLocationsForDraftContent($contentInfo->id);
96
            if (empty($parentLocations)) {
97
                return null;
98
            }
99
100
            // NOTE: Once Repository adds support for draft locations (and draft  location ops), then this can be removed
101
            $location = new Location(
102
                [
103
                    'content' => $content,
104
                    'contentInfo' => $contentInfo,
105
                    'status' => Location::STATUS_DRAFT,
106
                    'parentLocationId' => $parentLocations[0]->id,
107
                    'depth' => $parentLocations[0]->depth + 1,
108
                    'pathString' => $parentLocations[0]->pathString . 'x/',
109
                ]
110
            );
111
        }
112
113
        return $location;
114
    }
115
}
116