Completed
Push — EZP-25721_improve_warning ( d7a071...6ed08c )
by André
17:24
created

Location::visit()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 112

Duplication

Lines 12
Ratio 10.71 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 12
loc 112
rs 8
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 RestLocation 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\LocationService;
12
use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor;
13
use eZ\Publish\Core\REST\Common\Output\Generator;
14
use eZ\Publish\Core\REST\Common\Output\Visitor;
15
use eZ\Publish\Core\REST\Server\Values\RestContent as RestContentValue;
16
17
/**
18
 * Location value object visitor.
19
 */
20
class Location extends ValueObjectVisitor
21
{
22
    /**
23
     * @var \eZ\Publish\API\Repository\LocationService
24
     */
25
    private $locationService;
26
27
    public function __construct(LocationService $locationService)
28
    {
29
        $this->locationService = $locationService;
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\API\Repository\Values\Content\Location $location
38
     */
39
    public function visit(Visitor $visitor, Generator $generator, $location)
40
    {
41
        $generator->startObjectElement('Location');
42
        $visitor->setHeader('Content-Type', $generator->getMediaType('Location'));
43
        $visitor->setHeader('Accept-Patch', $generator->getMediaType('LocationUpdate'));
44
45
        $generator->startAttribute(
46
            'href',
47
            $this->router->generate(
48
                'ezpublish_rest_loadLocation',
49
                array('locationPath' => trim($location->pathString, '/'))
50
            )
51
        );
52
        $generator->endAttribute('href');
53
54
        $generator->startValueElement('id', $location->id);
55
        $generator->endValueElement('id');
56
57
        $generator->startValueElement('priority', $location->priority);
58
        $generator->endValueElement('priority');
59
60
        $generator->startValueElement(
61
            'hidden',
62
            $this->serializeBool($generator, $location->hidden)
63
        );
64
        $generator->endValueElement('hidden');
65
66
        $generator->startValueElement(
67
            'invisible',
68
            $this->serializeBool($generator, $location->invisible)
69
        );
70
        $generator->endValueElement('invisible');
71
72
        $generator->startObjectElement('ParentLocation', 'Location');
73 View Code Duplication
        if (trim($location->pathString, '/') !== '1') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            $generator->startAttribute(
75
                'href',
76
                $this->router->generate(
77
                    'ezpublish_rest_loadLocation',
78
                    array(
79
                        'locationPath' => implode('/', array_slice($location->path, 0, count($location->path) - 1)),
80
                    )
81
                )
82
            );
83
            $generator->endAttribute('href');
84
        }
85
        $generator->endObjectElement('ParentLocation');
86
87
        $generator->startValueElement('pathString', $location->pathString);
88
        $generator->endValueElement('pathString');
89
90
        $generator->startValueElement('depth', $location->depth);
91
        $generator->endValueElement('depth');
92
93
        $generator->startValueElement('childCount', $this->locationService->getLocationChildCount($location));
94
        $generator->endValueElement('childCount');
95
96
        $generator->startValueElement('remoteId', $location->remoteId);
97
        $generator->endValueElement('remoteId');
98
99
        $generator->startObjectElement('Children', 'LocationList');
100
        $generator->startAttribute(
101
            'href',
102
            $this->router->generate(
103
                'ezpublish_rest_loadLocationChildren',
104
                array(
105
                    'locationPath' => trim($location->pathString, '/'),
106
                )
107
            )
108
        );
109
        $generator->endAttribute('href');
110
        $generator->endObjectElement('Children');
111
112
        $generator->startObjectElement('Content');
113
        $generator->startAttribute(
114
            'href',
115
            $this->router->generate('ezpublish_rest_loadContent', array('contentId' => $location->contentId))
116
        );
117
        $generator->endAttribute('href');
118
        $generator->endObjectElement('Content');
119
120
        $generator->startValueElement('sortField', $this->serializeSortField($location->sortField));
121
        $generator->endValueElement('sortField');
122
123
        $generator->startValueElement('sortOrder', $this->serializeSortOrder($location->sortOrder));
124
        $generator->endValueElement('sortOrder');
125
126
        $generator->startObjectElement('UrlAliases', 'UrlAliasRefList');
127
        $generator->startAttribute(
128
            'href',
129
            $this->router->generate(
130
                'ezpublish_rest_listLocationURLAliases',
131
                array('locationPath' => trim($location->pathString, '/'))
132
            )
133
        );
134
        $generator->endAttribute('href');
135
        $generator->endObjectElement('UrlAliases');
136
137
        $generator->startObjectElement('ContentInfo', 'ContentInfo');
138
        $generator->startAttribute(
139
            'href',
140
            $this->router->generate(
141
                'ezpublish_rest_loadContent',
142
                array('contentId' => $location->contentId)
143
            )
144
        );
145
        $generator->endAttribute('href');
146
        $visitor->visitValueObject(new RestContentValue($location->contentInfo));
147
        $generator->endObjectElement('ContentInfo');
148
149
        $generator->endObjectElement('Location');
150
    }
151
}
152