Completed
Push — master ( 0ff500...dd6203 )
by André
25:15 queued 09:04
created

Content   F

Complexity

Total Complexity 62

Size/Duplication

Total Lines 736
Duplicated Lines 16.85 %

Coupling/Cohesion

Components 1
Dependencies 35

Importance

Changes 0
Metric Value
dl 124
loc 736
rs 1.0434
c 0
b 0
f 0
wmc 62
lcom 1
cbo 35

22 Methods

Rating   Name   Duplication   Size   Complexity  
B loadContent() 0 41 5
B updateContentMetadata() 0 42 6
A redirectCurrentVersion() 14 14 1
A redirectContent() 19 19 2
B loadContentInVersion() 0 32 4
B createContent() 0 43 4
A deleteContent() 0 8 1
A copyContent() 0 17 1
A loadContentVersions() 0 9 1
A deleteContentVersion() 17 17 2
A deleteTranslationFromDraft() 0 13 2
A createDraftFromVersion() 19 19 1
B createDraftFromCurrentVersion() 24 24 2
B updateVersion() 0 57 5
A publishVersion() 17 17 2
A redirectCurrentVersionRelations() 14 14 1
B loadVersionRelations() 0 32 6
B loadVersionRelation() 0 24 4
B removeRelation() 0 26 5
B createRelation() 0 36 5
A createView() 0 10 1
A forward() 0 7 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Content often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Content, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * File containing the Content controller 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\Controller;
10
11
use eZ\Publish\Core\REST\Common\Message;
12
use eZ\Publish\Core\REST\Common\Exceptions;
13
use eZ\Publish\Core\REST\Server\Values;
14
use eZ\Publish\Core\REST\Server\Controller as RestController;
15
use eZ\Publish\API\Repository\Values\Content\Relation;
16
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
17
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
18
use eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException;
19
use eZ\Publish\API\Repository\Exceptions\ContentValidationException;
20
use eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException;
21
use eZ\Publish\Core\REST\Server\Exceptions\BadRequestException;
22
use eZ\Publish\Core\REST\Server\Exceptions\ContentFieldValidationException as RESTContentFieldValidationException;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpKernel\HttpKernelInterface;
25
26
/**
27
 * Content controller.
28
 */
29
class Content extends RestController
30
{
31
    /**
32
     * Loads a content info by remote ID.
33
     *
34
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\BadRequestException
35
     *
36
     * @return \eZ\Publish\Core\REST\Server\Values\TemporaryRedirect
37
     */
38 View Code Duplication
    public function redirectContent(Request $request)
39
    {
40
        if (!$request->query->has('remoteId')) {
41
            throw new BadRequestException("'remoteId' parameter is required.");
42
        }
43
44
        $contentInfo = $this->repository->getContentService()->loadContentInfoByRemoteId(
45
            $request->query->get('remoteId')
46
        );
47
48
        return new Values\TemporaryRedirect(
49
            $this->router->generate(
50
                'ezpublish_rest_loadContent',
51
                array(
52
                    'contentId' => $contentInfo->id,
53
                )
54
            )
55
        );
56
    }
57
58
    /**
59
     * Loads a content info, potentially with the current version embedded.
60
     *
61
     * @param mixed $contentId
62
     * @param \Symfony\Component\HttpFoundation\Request $request
63
     *
64
     * @return \eZ\Publish\Core\REST\Server\Values\RestContent
65
     */
66
    public function loadContent($contentId, Request $request)
67
    {
68
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
69
70
        $mainLocation = null;
71
        if (!empty($contentInfo->mainLocationId)) {
72
            $mainLocation = $this->repository->getLocationService()->loadLocation($contentInfo->mainLocationId);
73
        }
74
75
        $contentType = $this->repository->getContentTypeService()->loadContentType($contentInfo->contentTypeId);
76
77
        $contentVersion = null;
78
        $relations = null;
79
        if ($this->getMediaType($request) === 'application/vnd.ez.api.content') {
80
            $languages = null;
81
            if ($request->query->has('languages')) {
82
                $languages = explode(',', $request->query->get('languages'));
83
            }
84
85
            $contentVersion = $this->repository->getContentService()->loadContent($contentId, $languages);
86
            $relations = $this->repository->getContentService()->loadRelations($contentVersion->getVersionInfo());
87
        }
88
89
        $restContent = new Values\RestContent(
90
            $contentInfo,
91
            $mainLocation,
92
            $contentVersion,
93
            $contentType,
94
            $relations,
95
            $request->getPathInfo()
96
        );
97
98
        if ($contentInfo->mainLocationId === null) {
99
            return $restContent;
100
        }
101
102
        return new Values\CachedValue(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...Info->mainLocationId)); (eZ\Publish\Core\REST\Server\Values\CachedValue) is incompatible with the return type documented by eZ\Publish\Core\REST\Ser...er\Content::loadContent of type eZ\Publish\Core\REST\Server\Values\RestContent.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
103
            $restContent,
104
            array('locationId' => $contentInfo->mainLocationId)
105
        );
106
    }
107
108
    /**
109
     * Updates a content's metadata.
110
     *
111
     * @param mixed $contentId
112
     *
113
     * @return \eZ\Publish\Core\REST\Server\Values\RestContent
114
     */
115
    public function updateContentMetadata($contentId, Request $request)
116
    {
117
        $updateStruct = $this->inputDispatcher->parse(
118
            new Message(
119
                array('Content-Type' => $request->headers->get('Content-Type')),
120
                $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
121
            )
122
        );
123
124
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
125
126
        // update section
127
        if ($updateStruct->sectionId !== null) {
0 ignored issues
show
Documentation introduced by
The property sectionId does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
128
            $section = $this->repository->getSectionService()->loadSection($updateStruct->sectionId);
0 ignored issues
show
Documentation introduced by
The property sectionId does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
129
            $this->repository->getSectionService()->assignSection($contentInfo, $section);
130
            $updateStruct->sectionId = null;
0 ignored issues
show
Documentation introduced by
The property sectionId does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
131
        }
132
133
        // @todo Consider refactoring! ContentService::updateContentMetadata throws the same exception
134
        // in case the updateStruct is empty and if remoteId already exists. Since REST version of update struct
135
        // includes section ID in addition to other fields, we cannot throw exception if only sectionId property
136
        // is set, so we must skip updating content in that case instead of allowing propagation of the exception.
137
        foreach ($updateStruct as $propertyName => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $updateStruct of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not traversable.
Loading history...
138
            if ($propertyName !== 'sectionId' && $propertyValue !== null) {
139
                // update content
140
                $this->repository->getContentService()->updateContentMetadata($contentInfo, $updateStruct);
0 ignored issues
show
Compatibility introduced by
$updateStruct of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...ntMetadataUpdateStruct>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\ValueObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
141
                $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
142
                break;
143
            }
144
        }
145
146
        try {
147
            $locationInfo = $this->repository->getLocationService()->loadLocation($contentInfo->mainLocationId);
148
        } catch (NotFoundException $e) {
149
            $locationInfo = null;
150
        }
151
152
        return new Values\RestContent(
153
            $contentInfo,
154
            $locationInfo
155
        );
156
    }
157
158
    /**
159
     * Loads a specific version of a given content object.
160
     *
161
     * @param mixed $contentId
162
     *
163
     * @return \eZ\Publish\Core\REST\Server\Values\TemporaryRedirect
164
     */
165 View Code Duplication
    public function redirectCurrentVersion($contentId)
166
    {
167
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
168
169
        return new Values\TemporaryRedirect(
170
            $this->router->generate(
171
                'ezpublish_rest_loadContentInVersion',
172
                array(
173
                    'contentId' => $contentId,
174
                    'versionNumber' => $contentInfo->currentVersionNo,
175
                )
176
            )
177
        );
178
    }
179
180
    /**
181
     * Loads a specific version of a given content object.
182
     *
183
     * @param mixed $contentId
184
     * @param int $versionNumber
185
     *
186
     * @return \eZ\Publish\Core\REST\Server\Values\Version
187
     */
188
    public function loadContentInVersion($contentId, $versionNumber, Request $request)
189
    {
190
        $languages = null;
191
        if ($request->query->has('languages')) {
192
            $languages = explode(',', $request->query->get('languages'));
193
        }
194
195
        $content = $this->repository->getContentService()->loadContent(
196
            $contentId,
197
            $languages,
198
            $versionNumber
199
        );
200
        $contentType = $this->repository->getContentTypeService()->loadContentType(
201
            $content->getVersionInfo()->getContentInfo()->contentTypeId
202
        );
203
204
        $versionValue = new Values\Version(
205
            $content,
206
            $contentType,
207
            $this->repository->getContentService()->loadRelations($content->getVersionInfo()),
208
            $request->getPathInfo()
209
        );
210
211
        if ($content->contentInfo->mainLocationId === null || $content->versionInfo->status === VersionInfo::STATUS_DRAFT) {
212
            return $versionValue;
213
        }
214
215
        return new Values\CachedValue(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...Info->mainLocationId)); (eZ\Publish\Core\REST\Server\Values\CachedValue) is incompatible with the return type documented by eZ\Publish\Core\REST\Ser...t::loadContentInVersion of type eZ\Publish\Core\REST\Server\Values\Version.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
216
            $versionValue,
217
            array('locationId' => $content->contentInfo->mainLocationId)
218
        );
219
    }
220
221
    /**
222
     * Creates a new content draft assigned to the authenticated user.
223
     * If a different userId is given in the input it is assigned to the
224
     * given user but this required special rights for the authenticated
225
     * user (this is useful for content staging where the transfer process
226
     * does not have to authenticate with the user which created the content
227
     * object in the source server). The user has to publish the content if
228
     * it should be visible.
229
     *
230
     * @return \eZ\Publish\Core\REST\Server\Values\CreatedContent
231
     */
232
    public function createContent(Request $request)
233
    {
234
        $contentCreate = $this->inputDispatcher->parse(
235
            new Message(
236
                array('Content-Type' => $request->headers->get('Content-Type')),
237
                $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
238
            )
239
        );
240
241
        try {
242
            $content = $this->repository->getContentService()->createContent(
243
                $contentCreate->contentCreateStruct,
0 ignored issues
show
Documentation introduced by
The property contentCreateStruct does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
244
                array($contentCreate->locationCreateStruct)
0 ignored issues
show
Documentation introduced by
The property locationCreateStruct does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
245
            );
246
        } catch (ContentValidationException $e) {
247
            throw new BadRequestException($e->getMessage());
248
        } catch (ContentFieldValidationException $e) {
249
            throw new RESTContentFieldValidationException($e);
250
        }
251
252
        $contentValue = null;
253
        $contentType = null;
254
        $relations = null;
255
        if ($this->getMediaType($request) === 'application/vnd.ez.api.content') {
256
            $contentValue = $content;
257
            $contentType = $this->repository->getContentTypeService()->loadContentType(
258
                $content->getVersionInfo()->getContentInfo()->contentTypeId
259
            );
260
            $relations = $this->repository->getContentService()->loadRelations($contentValue->getVersionInfo());
261
        }
262
263
        return new Values\CreatedContent(
264
            array(
265
                'content' => new Values\RestContent(
266
                    $content->contentInfo,
267
                    null,
268
                    $contentValue,
269
                    $contentType,
270
                    $relations
271
                ),
272
            )
273
        );
274
    }
275
276
    /**
277
     * The content is deleted. If the content has locations (which is required in 4.x)
278
     * on delete all locations assigned the content object are deleted via delete subtree.
279
     *
280
     * @param mixed $contentId
281
     *
282
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
283
     */
284
    public function deleteContent($contentId)
285
    {
286
        $this->repository->getContentService()->deleteContent(
287
            $this->repository->getContentService()->loadContentInfo($contentId)
288
        );
289
290
        return new Values\NoContent();
291
    }
292
293
    /**
294
     * Creates a new content object as copy under the given parent location given in the destination header.
295
     *
296
     * @param mixed $contentId
297
     *
298
     * @return \eZ\Publish\Core\REST\Server\Values\ResourceCreated
299
     */
300
    public function copyContent($contentId, Request $request)
301
    {
302
        $destination = $request->headers->get('Destination');
303
304
        $parentLocationParts = explode('/', $destination);
305
        $copiedContent = $this->repository->getContentService()->copyContent(
306
            $this->repository->getContentService()->loadContentInfo($contentId),
307
            $this->repository->getLocationService()->newLocationCreateStruct(array_pop($parentLocationParts))
308
        );
309
310
        return new Values\ResourceCreated(
311
            $this->router->generate(
312
                'ezpublish_rest_loadContent',
313
                array('contentId' => $copiedContent->id)
314
            )
315
        );
316
    }
317
318
    /**
319
     * Returns a list of all versions of the content. This method does not
320
     * include fields and relations in the Version elements of the response.
321
     *
322
     * @param mixed $contentId
323
     *
324
     * @return \eZ\Publish\Core\REST\Server\Values\VersionList
325
     */
326
    public function loadContentVersions($contentId, Request $request)
327
    {
328
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
329
330
        return new Values\VersionList(
331
            $this->repository->getContentService()->loadVersions($contentInfo),
332
            $request->getPathInfo()
333
        );
334
    }
335
336
    /**
337
     * The version is deleted.
338
     *
339
     * @param mixed $contentId
340
     * @param mixed $versionNumber
341
     *
342
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
343
     *
344
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
345
     */
346 View Code Duplication
    public function deleteContentVersion($contentId, $versionNumber)
347
    {
348
        $versionInfo = $this->repository->getContentService()->loadVersionInfo(
349
            $this->repository->getContentService()->loadContentInfo($contentId),
350
            $versionNumber
351
        );
352
353
        if ($versionInfo->isPublished()) {
354
            throw new ForbiddenException('Version in status PUBLISHED cannot be deleted');
355
        }
356
357
        $this->repository->getContentService()->deleteVersion(
358
            $versionInfo
359
        );
360
361
        return new Values\NoContent();
362
    }
363
364
    /**
365
     * Remove the given Translation from the given Version Draft.
366
     *
367
     * @param int $contentId
368
     * @param int $versionNumber
369
     * @param string $languageCode
370
     *
371
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
372
     *
373
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
374
     */
375
    public function deleteTranslationFromDraft($contentId, $versionNumber, $languageCode)
376
    {
377
        $contentService = $this->repository->getContentService();
378
        $versionInfo = $contentService->loadVersionInfoById($contentId, $versionNumber);
379
380
        if (!$versionInfo->isDraft()) {
381
            throw new ForbiddenException('Translation can be deleted from DRAFT Version only');
382
        }
383
384
        $contentService->deleteTranslationFromDraft($versionInfo, $languageCode);
385
386
        return new Values\NoContent();
387
    }
388
389
    /**
390
     * The system creates a new draft version as a copy from the given version.
391
     *
392
     * @param mixed $contentId
393
     * @param mixed $versionNumber
394
     *
395
     * @return \eZ\Publish\Core\REST\Server\Values\CreatedVersion
396
     */
397 View Code Duplication
    public function createDraftFromVersion($contentId, $versionNumber)
398
    {
399
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
400
        $contentType = $this->repository->getContentTypeService()->loadContentType($contentInfo->contentTypeId);
401
        $contentDraft = $this->repository->getContentService()->createContentDraft(
402
            $contentInfo,
403
            $this->repository->getContentService()->loadVersionInfo($contentInfo, $versionNumber)
404
        );
405
406
        return new Values\CreatedVersion(
407
            array(
408
                'version' => new Values\Version(
409
                    $contentDraft,
410
                    $contentType,
411
                    $this->repository->getContentService()->loadRelations($contentDraft->getVersionInfo())
412
                ),
413
            )
414
        );
415
    }
416
417
    /**
418
     * The system creates a new draft version as a copy from the current version.
419
     *
420
     * @param mixed $contentId
421
     *
422
     * @throws ForbiddenException if the current version is already a draft
423
     *
424
     * @return \eZ\Publish\Core\REST\Server\Values\CreatedVersion
425
     */
426 View Code Duplication
    public function createDraftFromCurrentVersion($contentId)
427
    {
428
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
429
        $contentType = $this->repository->getContentTypeService()->loadContentType($contentInfo->contentTypeId);
430
        $versionInfo = $this->repository->getContentService()->loadVersionInfo(
431
            $contentInfo
432
        );
433
434
        if ($versionInfo->isDraft()) {
435
            throw new ForbiddenException('Current version is already in status DRAFT');
436
        }
437
438
        $contentDraft = $this->repository->getContentService()->createContentDraft($contentInfo);
439
440
        return new Values\CreatedVersion(
441
            array(
442
                'version' => new Values\Version(
443
                    $contentDraft,
444
                    $contentType,
445
                    $this->repository->getContentService()->loadRelations($contentDraft->getVersionInfo())
446
                ),
447
            )
448
        );
449
    }
450
451
    /**
452
     * A specific draft is updated.
453
     *
454
     * @param mixed $contentId
455
     * @param mixed $versionNumber
456
     *
457
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
458
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\BadRequestException
459
     *
460
     * @return \eZ\Publish\Core\REST\Server\Values\Version
461
     */
462
    public function updateVersion($contentId, $versionNumber, Request $request)
463
    {
464
        $contentUpdateStruct = $this->inputDispatcher->parse(
465
            new Message(
466
                array(
467
                    'Content-Type' => $request->headers->get('Content-Type'),
468
                    'Url' => $this->router->generate(
469
                        'ezpublish_rest_updateVersion',
470
                        array(
471
                            'contentId' => $contentId,
472
                            'versionNumber' => $versionNumber,
473
                        )
474
                    ),
475
                ),
476
                $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
477
            )
478
        );
479
480
        $versionInfo = $this->repository->getContentService()->loadVersionInfo(
481
            $this->repository->getContentService()->loadContentInfo($contentId),
482
            $versionNumber
483
        );
484
485
        if (!$versionInfo->isDraft()) {
486
            throw new ForbiddenException('Only version in status DRAFT can be updated');
487
        }
488
489
        try {
490
            $this->repository->getContentService()->updateContent($versionInfo, $contentUpdateStruct);
0 ignored issues
show
Compatibility introduced by
$contentUpdateStruct of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...nt\ContentUpdateStruct>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\ValueObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
491
        } catch (ContentValidationException $e) {
492
            throw new BadRequestException($e->getMessage());
493
        } catch (ContentFieldValidationException $e) {
494
            throw new RESTContentFieldValidationException($e);
495
        }
496
497
        $languages = null;
498
        if ($request->query->has('languages')) {
499
            $languages = explode(',', $request->query->get('languages'));
500
        }
501
502
        // Reload the content to handle languages GET parameter
503
        $content = $this->repository->getContentService()->loadContent(
504
            $contentId,
505
            $languages,
506
            $versionInfo->versionNo
507
        );
508
        $contentType = $this->repository->getContentTypeService()->loadContentType(
509
            $content->getVersionInfo()->getContentInfo()->contentTypeId
510
        );
511
512
        return new Values\Version(
513
            $content,
514
            $contentType,
515
            $this->repository->getContentService()->loadRelations($content->getVersionInfo()),
516
            $request->getPathInfo()
517
        );
518
    }
519
520
    /**
521
     * The content version is published.
522
     *
523
     * @param mixed $contentId
524
     * @param mixed $versionNumber
525
     *
526
     * @throws ForbiddenException if version $versionNumber isn't a draft
527
     *
528
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
529
     */
530 View Code Duplication
    public function publishVersion($contentId, $versionNumber)
531
    {
532
        $versionInfo = $this->repository->getContentService()->loadVersionInfo(
533
            $this->repository->getContentService()->loadContentInfo($contentId),
534
            $versionNumber
535
        );
536
537
        if (!$versionInfo->isDraft()) {
538
            throw new ForbiddenException('Only version in status DRAFT can be published');
539
        }
540
541
        $this->repository->getContentService()->publishVersion(
542
            $versionInfo
543
        );
544
545
        return new Values\NoContent();
546
    }
547
548
    /**
549
     * Redirects to the relations of the current version.
550
     *
551
     * @param mixed $contentId
552
     *
553
     * @return \eZ\Publish\Core\REST\Server\Values\TemporaryRedirect
554
     */
555 View Code Duplication
    public function redirectCurrentVersionRelations($contentId)
556
    {
557
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
558
559
        return new Values\TemporaryRedirect(
560
            $this->router->generate(
561
                'ezpublish_rest_redirectCurrentVersionRelations',
562
                array(
563
                    'contentId' => $contentId,
564
                    'versionNumber' => $contentInfo->currentVersionNo,
565
                )
566
            )
567
        );
568
    }
569
570
    /**
571
     * Loads the relations of the given version.
572
     *
573
     * @param mixed $contentId
574
     * @param mixed $versionNumber
575
     *
576
     * @return \eZ\Publish\Core\REST\Server\Values\RelationList
577
     */
578
    public function loadVersionRelations($contentId, $versionNumber, Request $request)
579
    {
580
        $offset = $request->query->has('offset') ? (int)$request->query->get('offset') : 0;
581
        $limit = $request->query->has('limit') ? (int)$request->query->get('limit') : -1;
582
583
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
584
        $relationList = $this->repository->getContentService()->loadRelations(
585
            $this->repository->getContentService()->loadVersionInfo($contentInfo, $versionNumber)
586
        );
587
588
        $relationList = array_slice(
589
            $relationList,
590
            $offset >= 0 ? $offset : 0,
591
            $limit >= 0 ? $limit : null
592
        );
593
594
        $relationListValue = new Values\RelationList(
595
            $relationList,
596
            $contentId,
597
            $versionNumber,
598
            $request->getPathInfo()
599
        );
600
601
        if ($contentInfo->mainLocationId === null) {
602
            return $relationListValue;
603
        }
604
605
        return new Values\CachedValue(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...Info->mainLocationId)); (eZ\Publish\Core\REST\Server\Values\CachedValue) is incompatible with the return type documented by eZ\Publish\Core\REST\Ser...t::loadVersionRelations of type eZ\Publish\Core\REST\Server\Values\RelationList.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
606
            $relationListValue,
607
            array('locationId' => $contentInfo->mainLocationId)
608
        );
609
    }
610
611
    /**
612
     * Loads a relation for the given content object and version.
613
     *
614
     * @param mixed $contentId
615
     * @param int $versionNumber
616
     * @param mixed $relationId
617
     *
618
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException
619
     *
620
     * @return \eZ\Publish\Core\REST\Server\Values\RestRelation
621
     */
622
    public function loadVersionRelation($contentId, $versionNumber, $relationId, Request $request)
623
    {
624
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
625
        $relationList = $this->repository->getContentService()->loadRelations(
626
            $this->repository->getContentService()->loadVersionInfo($contentInfo, $versionNumber)
627
        );
628
629
        foreach ($relationList as $relation) {
630
            if ($relation->id == $relationId) {
631
                $relation = new Values\RestRelation($relation, $contentId, $versionNumber);
632
633
                if ($contentInfo->mainLocationId === null) {
634
                    return $relation;
635
                }
636
637
                return new Values\CachedValue(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...Info->mainLocationId)); (eZ\Publish\Core\REST\Server\Values\CachedValue) is incompatible with the return type documented by eZ\Publish\Core\REST\Ser...nt::loadVersionRelation of type eZ\Publish\Core\REST\Server\Values\RestRelation.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
638
                    $relation,
639
                    array('locationId' => $contentInfo->mainLocationId)
640
                );
641
            }
642
        }
643
644
        throw new Exceptions\NotFoundException("Relation not found: '{$request->getPathInfo()}'.");
645
    }
646
647
    /**
648
     * Deletes a relation of the given draft.
649
     *
650
     * @param mixed $contentId
651
     * @param int   $versionNumber
652
     * @param mixed $relationId
653
     *
654
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
655
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException
656
     *
657
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
658
     */
659
    public function removeRelation($contentId, $versionNumber, $relationId, Request $request)
660
    {
661
        $versionInfo = $this->repository->getContentService()->loadVersionInfo(
662
            $this->repository->getContentService()->loadContentInfo($contentId),
663
            $versionNumber
664
        );
665
666
        $versionRelations = $this->repository->getContentService()->loadRelations($versionInfo);
667
        foreach ($versionRelations as $relation) {
668
            if ($relation->id == $relationId) {
669
                if ($relation->type !== Relation::COMMON) {
670
                    throw new ForbiddenException('Relation is not of type COMMON');
671
                }
672
673
                if (!$versionInfo->isDraft()) {
674
                    throw new ForbiddenException('Relation of type COMMON can only be removed from drafts');
675
                }
676
677
                $this->repository->getContentService()->deleteRelation($versionInfo, $relation->getDestinationContentInfo());
678
679
                return new Values\NoContent();
680
            }
681
        }
682
683
        throw new Exceptions\NotFoundException("Relation not found: '{$request->getPathInfo()}'.");
684
    }
685
686
    /**
687
     * Creates a new relation of type COMMON for the given draft.
688
     *
689
     * @param mixed $contentId
690
     * @param int $versionNumber
691
     *
692
     * @throws ForbiddenException if version $versionNumber isn't a draft
693
     * @throws ForbiddenException if a relation to the same content already exists
694
     *
695
     * @return \eZ\Publish\Core\REST\Server\Values\CreatedRelation
696
     */
697
    public function createRelation($contentId, $versionNumber, Request $request)
698
    {
699
        $destinationContentId = $this->inputDispatcher->parse(
700
            new Message(
701
                array('Content-Type' => $request->headers->get('Content-Type')),
702
                $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
703
            )
704
        );
705
706
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
707
        $versionInfo = $this->repository->getContentService()->loadVersionInfo($contentInfo, $versionNumber);
708
        if (!$versionInfo->isDraft()) {
709
            throw new ForbiddenException('Relation of type COMMON can only be added to drafts');
710
        }
711
712
        try {
713
            $destinationContentInfo = $this->repository->getContentService()->loadContentInfo($destinationContentId);
0 ignored issues
show
Documentation introduced by
$destinationContentId is of type object<eZ\Publish\API\Re...ory\Values\ValueObject>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
714
        } catch (NotFoundException $e) {
715
            throw new ForbiddenException($e->getMessage());
716
        }
717
718
        $existingRelations = $this->repository->getContentService()->loadRelations($versionInfo);
719
        foreach ($existingRelations as $existingRelation) {
720
            if ($existingRelation->getDestinationContentInfo()->id == $destinationContentId) {
721
                throw new ForbiddenException('Relation of type COMMON to selected destination content ID already exists');
722
            }
723
        }
724
725
        $relation = $this->repository->getContentService()->addRelation($versionInfo, $destinationContentInfo);
726
727
        return new Values\CreatedRelation(
728
            array(
729
                'relation' => new Values\RestRelation($relation, $contentId, $versionNumber),
730
            )
731
        );
732
    }
733
734
    /**
735
     * Creates and executes a content view.
736
     *
737
     * @deprecated Since platform 1.0. Forwards the request to the new /views location, but returns a 301.
738
     *
739
     * @return \eZ\Publish\Core\REST\Server\Values\RestExecutedView
740
     */
741
    public function createView()
742
    {
743
        $response = $this->forward('ezpublish_rest.controller.views:createView');
744
745
        // Add 301 status code and location href
746
        $response->setStatusCode(301);
747
        $response->headers->set('Location', $this->router->generate('ezpublish_rest_views_create'));
748
749
        return $response;
750
    }
751
752
    /**
753
     * @param string $controller
754
     *
755
     * @return \Symfony\Component\HttpFoundation\Response
756
     */
757
    protected function forward($controller)
758
    {
759
        $path['_controller'] = $controller;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$path was never initialized. Although not strictly required by PHP, it is generally a good practice to add $path = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
760
        $subRequest = $this->container->get('request_stack')->getCurrentRequest()->duplicate(null, null, $path);
761
762
        return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
763
    }
764
}
765