Completed
Push — preview_location-getContent ( 01895d )
by André
19:30
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 {@see loadMainLocationByContent}.
56
     *
57
     * @param mixed $contentId
58
     *
59
     * @return \eZ\Publish\API\Repository\Values\Content\Location|null
60
     */
61
    public function loadMainLocation($contentId)
62
    {
63
        return $this->loadMainLocationByContent(
64
            $this->contentService->loadContent($contentId)
65
        );
66
    }
67
68
    /**
69
     * Loads the main location for $content.
70
     *
71
     * If the content does not have a location (yet), but has a Location draft, it is returned instead.
72
     * Location drafts do not have an id (it is set to null), and can be tested using the isDraft() method.
73
     *
74
     * If the content doesn't have a location nor a location draft, null is returned.
75
     *
76
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
77
     *
78
     * @return \eZ\Publish\API\Repository\Values\Content\Location|null
79
     */
80
    public function loadMainLocationByContent(APIContent $content): ?APILocation
81
    {
82
        $location = null;
83
        $contentInfo = $content
84
            ->getVersionInfo()
85
            ->getContentInfo();
86
87
        // mainLocationId already exists, content has been published at least once.
88
        if ($contentInfo->mainLocationId) {
89
            $location = $this->locationService->loadLocation($contentInfo->mainLocationId);
90
        } elseif (!$contentInfo->published) {
91
            // New Content, never published, create a virtual location object.
92
            // In cases content is missing locations this will return empty array
93
            $parentLocations = $this->locationHandler->loadParentLocationsForDraftContent($contentInfo->id);
94
            if (empty($parentLocations)) {
95
                return null;
96
            }
97
98
            // @TODO: Repository needs to add full support for draft Locations to avoid having to do this
99
            $location = new Location(
100
                [
101
                    'content' => $content,
102
                    'contentInfo' => $contentInfo,
103
                    'status' => Location::STATUS_DRAFT,
104
                    'parentLocationId' => $parentLocations[0]->id,
105
                    'depth' => $parentLocations[0]->depth + 1,
106
                    'pathString' => $parentLocations[0]->pathString . 'x/',
107
                ]
108
            );
109
        }
110
111
        return $location;
112
    }
113
}
114