Completed
Push — master ( 5fa9fe...31552d )
by André
46:14 queued 23:32
created

RestContent::getStatusString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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