Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a...6abe82 )
by
unknown
79:54 queued 33:48
created

RestRelation::visit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 36
nc 2
nop 3
dl 0
loc 54
rs 9.6716
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
18
/**
19
 * RestRelation value object visitor.
20
 */
21
class RestRelation 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\RestRelation $data
29
     */
30
    public function visit(Visitor $visitor, Generator $generator, $data)
31
    {
32
        $generator->startObjectElement('Relation');
33
        $visitor->setHeader('Content-Type', $generator->getMediaType('Relation'));
34
35
        $generator->startAttribute(
36
            'href',
37
            $this->router->generate(
38
                'ezpublish_rest_loadVersionRelation',
39
                array(
40
                    'contentId' => $data->contentId,
41
                    'versionNumber' => $data->versionNo,
42
                    'relationId' => $data->relation->id,
43
                )
44
            )
45
        );
46
        $generator->endAttribute('href');
47
48
        $generator->startObjectElement('SourceContent', 'ContentInfo');
49
        $generator->startAttribute(
50
            'href',
51
            $this->router->generate(
52
                'ezpublish_rest_loadContent',
53
                array(
54
                    'contentId' => $data->contentId,
55
                )
56
            )
57
        );
58
        $generator->endAttribute('href');
59
        $generator->endObjectElement('SourceContent');
60
61
        $generator->startObjectElement('DestinationContent', 'ContentInfo');
62
        $generator->startAttribute(
63
            'href',
64
            $this->router->generate(
65
                'ezpublish_rest_loadContent',
66
                array(
67
                    'contentId' => $data->relation->getDestinationContentInfo()->id,
68
                )
69
            )
70
        );
71
        $generator->endAttribute('href');
72
        $generator->endObjectElement('DestinationContent');
73
74
        if ($data->relation->sourceFieldDefinitionIdentifier !== null) {
75
            $generator->startValueElement('SourceFieldDefinitionIdentifier', $data->relation->sourceFieldDefinitionIdentifier);
76
            $generator->endValueElement('SourceFieldDefinitionIdentifier');
77
        }
78
79
        $generator->startValueElement('RelationType', $this->getRelationTypeString($data->relation->type));
80
        $generator->endValueElement('RelationType');
81
82
        $generator->endObjectElement('Relation');
83
    }
84
85
    /**
86
     * Returns $relationType as a readable string.
87
     *
88
     * @param int $relationType
89
     *
90
     * @return string
91
     */
92
    protected function getRelationTypeString($relationType)
93
    {
94
        switch ($relationType) {
95
            case RelationValue::COMMON:
96
                return 'COMMON';
97
            case RelationValue::EMBED:
98
                return 'EMBED';
99
            case RelationValue::LINK:
100
                return 'LINK';
101
            case RelationValue::FIELD:
102
                return 'ATTRIBUTE';
103
        }
104
105
        throw new \Exception('Unknown relation type ' . $relationType . '.');
106
    }
107
}
108