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

Location   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 132
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 12
loc 132
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B visit() 12 112 2

How to fix   Duplicated Code   

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:

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