Completed
Push — 7.5 ( 5c8d05...441ec5 )
by
unknown
19:22
created

RestContent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the RestContent ValueObjectVisitor class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
10
11
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
12
use eZ\Publish\Core\Base\Exceptions\BadStateException as CoreBadStateException;
13
use eZ\Publish\Core\Helper\TranslationHelper;
14
use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor;
15
use eZ\Publish\Core\REST\Common\Output\Generator;
16
use eZ\Publish\Core\REST\Common\Output\Visitor;
17
use eZ\Publish\Core\REST\Server\Values\Version as VersionValue;
18
19
/**
20
 * RestContent value object visitor.
21
 */
22
class RestContent extends ValueObjectVisitor
23
{
24
    /** @var \eZ\Publish\Core\Helper\TranslationHelper */
25
    private $translationHelper;
26
27
    public function __construct(TranslationHelper $translationHelper)
28
    {
29
        $this->translationHelper = $translationHelper;
30
    }
31
32
    /**
33
     * Visit struct returned by controllers.
34
     *
35
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
36
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
37
     * @param \eZ\Publish\Core\REST\Server\Values\RestContent $data
38
     */
39
    public function visit(Visitor $visitor, Generator $generator, $data)
40
    {
41
        $restContent = $data;
42
        $contentInfo = $restContent->contentInfo;
43
        $translatedContentName = $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo);
44
        $contentType = $restContent->contentType;
45
        $mainLocation = $restContent->mainLocation;
46
        $currentVersion = $restContent->currentVersion;
47
48
        $mediaType = ($restContent->currentVersion === null ? 'ContentInfo' : 'Content');
49
50
        $generator->startObjectElement('Content', $mediaType);
51
52
        $visitor->setHeader('Content-Type', $generator->getMediaType($mediaType));
53
        $visitor->setHeader('Accept-Patch', $generator->getMediaType('ContentUpdate'));
54
55
        $generator->startAttribute(
56
            'href',
57
            $data->path === null ?
58
                $this->router->generate('ezpublish_rest_loadContent', ['contentId' => $contentInfo->id]) :
59
                $data->path
60
        );
61
        $generator->endAttribute('href');
62
63
        $generator->startAttribute('remoteId', $contentInfo->remoteId);
64
        $generator->endAttribute('remoteId');
65
        $generator->startAttribute('id', $contentInfo->id);
66
        $generator->endAttribute('id');
67
68
        $generator->startObjectElement('ContentType');
69
        $generator->startAttribute(
70
            'href',
71
            $this->router->generate(
72
                'ezpublish_rest_loadContentType',
73
                ['contentTypeId' => $contentInfo->contentTypeId]
74
            )
75
        );
76
        $generator->endAttribute('href');
77
        $generator->endObjectElement('ContentType');
78
79
        $generator->startValueElement('Name', $contentInfo->name);
80
        $generator->endValueElement('Name');
81
82
        $generator->startValueElement('TranslatedName', $translatedContentName);
83
        $generator->endValueElement('TranslatedName');
84
85
        $generator->startObjectElement('Versions', 'VersionList');
86
        $generator->startAttribute(
87
            'href',
88
            $this->router->generate('ezpublish_rest_loadContentVersions', ['contentId' => $contentInfo->id])
89
        );
90
        $generator->endAttribute('href');
91
        $generator->endObjectElement('Versions');
92
93
        $generator->startObjectElement('CurrentVersion', 'Version');
94
        $generator->startAttribute(
95
            'href',
96
            $this->router->generate(
97
                'ezpublish_rest_redirectCurrentVersion',
98
                ['contentId' => $contentInfo->id]
99
            )
100
        );
101
        $generator->endAttribute('href');
102
103
        // Embed current version, if available
104
        if ($currentVersion !== null) {
105
            $visitor->visitValueObject(
106
                new VersionValue(
107
                    $currentVersion,
108
                    $contentType,
109
                    $restContent->relations
110
                )
111
            );
112
        }
113
114
        $generator->endObjectElement('CurrentVersion');
115
116
        $generator->startObjectElement('Section');
117
        $generator->startAttribute(
118
            'href',
119
            $this->router->generate('ezpublish_rest_loadSection', ['sectionId' => $contentInfo->sectionId])
120
        );
121
        $generator->endAttribute('href');
122
        $generator->endObjectElement('Section');
123
124
        // Main location will not exist if we're visiting the content draft
125
        if ($data->mainLocation !== null) {
126
            $generator->startObjectElement('MainLocation', 'Location');
127
            $generator->startAttribute(
128
                'href',
129
                $this->router->generate(
130
                    'ezpublish_rest_loadLocation',
131
                    ['locationPath' => trim($mainLocation->pathString, '/')]
132
                )
133
            );
134
            $generator->endAttribute('href');
135
            $generator->endObjectElement('MainLocation');
136
        }
137
138
        $generator->startObjectElement('Locations', 'LocationList');
139
        $generator->startAttribute(
140
            'href',
141
            $this->router->generate(
142
                'ezpublish_rest_loadLocationsForContent',
143
                ['contentId' => $contentInfo->id]
144
            )
145
        );
146
        $generator->endAttribute('href');
147
        $generator->endObjectElement('Locations');
148
149
        $generator->startObjectElement('Owner', 'User');
150
        $generator->startAttribute(
151
            'href',
152
            $this->router->generate('ezpublish_rest_loadUser', ['userId' => $contentInfo->ownerId])
153
        );
154
        $generator->endAttribute('href');
155
        $generator->endObjectElement('Owner');
156
157
        // Modification date will not exist if we're visiting the content draft
158
        if ($contentInfo->modificationDate !== null) {
159
            $generator->startValueElement(
160
                'lastModificationDate',
161
                $contentInfo->modificationDate->format('c')
162
            );
163
            $generator->endValueElement('lastModificationDate');
164
        }
165
166
        // Published date will not exist if we're visiting the content draft
167
        if ($contentInfo->publishedDate !== null) {
168
            $generator->startValueElement(
169
                'publishedDate',
170
                ($contentInfo->publishedDate !== null
171
                    ? $contentInfo->publishedDate->format('c')
172
                    : null)
173
            );
174
            $generator->endValueElement('publishedDate');
175
        }
176
177
        $generator->startValueElement(
178
            'mainLanguageCode',
179
            $contentInfo->mainLanguageCode
180
        );
181
        $generator->endValueElement('mainLanguageCode');
182
183
        $generator->startValueElement(
184
            'currentVersionNo',
185
            $contentInfo->currentVersionNo
186
        );
187
        $generator->endValueElement('currentVersionNo');
188
189
        $generator->startValueElement(
190
            'alwaysAvailable',
191
            $this->serializeBool($generator, $contentInfo->alwaysAvailable)
192
        );
193
        $generator->endValueElement('alwaysAvailable');
194
195
        $generator->startValueElement(
196
            'isHidden',
197
            $this->serializeBool($generator, $contentInfo->isHidden)
198
        );
199
        $generator->endValueElement('isHidden');
200
201
        $generator->startValueElement(
202
            'status',
203
            $this->getStatusString($contentInfo->status)
204
        );
205
        $generator->endValueElement('status');
206
207
        $generator->startObjectElement('ObjectStates', 'ContentObjectStates');
208
        $generator->startAttribute(
209
            'href',
210
            $this->router->generate(
211
                'ezpublish_rest_getObjectStatesForContent',
212
                ['contentId' => $contentInfo->id]
213
            )
214
        );
215
        $generator->endAttribute('href');
216
        $generator->endObjectElement('ObjectStates');
217
218
        $generator->endObjectElement('Content');
219
    }
220
221
    /**
222
     * Maps the given content $status to a representative string.
223
     *
224
     * @param int $status
225
     *
226
     * @throws \eZ\Publish\Core\Base\Exceptions\BadStateException
227
     *
228
     * @return string
229
     */
230
    protected function getStatusString($status)
231
    {
232
        switch ($status) {
233
            case ContentInfo::STATUS_DRAFT:
234
                return 'DRAFT';
235
236
            case ContentInfo::STATUS_PUBLISHED:
237
                return 'PUBLISHED';
238
239
            case ContentInfo::STATUS_TRASHED:
240
                return 'TRASHED';
241
        }
242
243
        throw new CoreBadStateException('status', $status);
244
    }
245
}
246