Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a )
by
unknown
36:29
created

RestRelation::visit()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 30
nc 2
nop 3
dl 0
loc 46
rs 8.9411
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the RestRelation 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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
12
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\API\Repository\Values\Content\Relation as RelationValue;
17
use eZ\Publish\Core\REST\Server\Values\ResourceRouteReference;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, eZ\Publish\Core\REST\Ser...\ResourceRouteReference.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
19
/**
20
 * RestRelation value object visitor.
21
 */
22
class RestRelation extends ValueObjectVisitor
23
{
24
    /**
25
     * Visit struct returned by controllers.
26
     *
27
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
28
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
29
     * @param \eZ\Publish\Core\REST\Server\Values\RestRelation $data
30
     */
31
    public function visit(Visitor $visitor, Generator $generator, $data)
32
    {
33
        $generator->startObjectElement('Relation');
34
        $visitor->setHeader('Content-Type', $generator->getMediaType('Relation'));
35
36
        $generator->startAttribute(
37
            'href',
38
            $this->router->generate(
39
                'ezpublish_rest_loadVersionRelation',
40
                array(
41
                    'contentId' => $data->contentId,
42
                    'versionNumber' => $data->versionNo,
43
                    'relationId' => $data->relation->id,
44
                )
45
            )
46
        );
47
        $generator->endAttribute('href');
48
49
        $generator->startObjectElement('SourceContent', 'ContentInfo');
50
        $visitor->visitValueObject(
51
            new ResourceRouteReference(
52
                'ezpublish_rest_loadContent',
0 ignored issues
show
Documentation introduced by
'ezpublish_rest_loadContent' is of type string, but the function expects a array.

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...
53
                ['contentId' => $data->contentId]
54
            )
55
        );
56
        $generator->endObjectElement('SourceContent');
57
58
        $generator->startObjectElement('DestinationContent', 'ContentInfo');
59
        $visitor->visitValueObject(
60
            new ResourceRouteReference(
61
                'ezpublish_rest_loadContent',
0 ignored issues
show
Documentation introduced by
'ezpublish_rest_loadContent' is of type string, but the function expects a array.

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...
62
                ['contentId' => $data->relation->getDestinationContentInfo()->id]
63
            )
64
        );
65
        $generator->endObjectElement('DestinationContent');
66
67
        if ($data->relation->sourceFieldDefinitionIdentifier !== null) {
68
            $generator->startValueElement('SourceFieldDefinitionIdentifier', $data->relation->sourceFieldDefinitionIdentifier);
69
            $generator->endValueElement('SourceFieldDefinitionIdentifier');
70
        }
71
72
        $generator->startValueElement('RelationType', $this->getRelationTypeString($data->relation->type));
73
        $generator->endValueElement('RelationType');
74
75
        $generator->endObjectElement('Relation');
76
    }
77
78
    /**
79
     * Returns $relationType as a readable string.
80
     *
81
     * @param int $relationType
82
     *
83
     * @return string
84
     */
85
    protected function getRelationTypeString($relationType)
86
    {
87
        switch ($relationType) {
88
            case RelationValue::COMMON:
89
                return 'COMMON';
90
            case RelationValue::EMBED:
91
                return 'EMBED';
92
            case RelationValue::LINK:
93
                return 'LINK';
94
            case RelationValue::FIELD:
95
                return 'ATTRIBUTE';
96
        }
97
98
        throw new \Exception('Unknown relation type ' . $relationType . '.');
99
    }
100
}
101