Completed
Push — master ( 553bc5...721b68 )
by
unknown
49:35 queued 26:20
created

Location::visit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
use eZ\Publish\API\Repository\Values\Content\Location as LocationValue;
17
18
/**
19
 * Location value object visitor.
20
 */
21
class Location extends ValueObjectVisitor
22
{
23
    /**
24
     * @var \eZ\Publish\API\Repository\LocationService
25
     */
26
    private $locationService;
27
28
    public function __construct(LocationService $locationService)
29
    {
30
        $this->locationService = $locationService;
31
    }
32
33
    /**
34
     * Visit struct returned by controllers.
35
     *
36
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
37
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
38
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
39
     */
40
    public function visit(Visitor $visitor, Generator $generator, $location)
41
    {
42
        $generator->startObjectElement('Location');
43
        $visitor->setHeader('Content-Type', $generator->getMediaType('Location'));
44
        $visitor->setHeader('Accept-Patch', $generator->getMediaType('LocationUpdate'));
45
        $this->visitLocationAttributes($visitor, $generator, $location);
46
        $generator->endObjectElement('Location');
47
    }
48
49
    protected function visitLocationAttributes(Visitor $visitor, Generator $generator, LocationValue $location)
50
    {
51
        $generator->startAttribute(
52
            'href',
53
            $this->router->generate(
54
                'ezpublish_rest_loadLocation',
55
                array('locationPath' => trim($location->pathString, '/'))
56
            )
57
        );
58
        $generator->endAttribute('href');
59
60
        $generator->startValueElement('id', $location->id);
61
        $generator->endValueElement('id');
62
63
        $generator->startValueElement('priority', $location->priority);
64
        $generator->endValueElement('priority');
65
66
        $generator->startValueElement(
67
            'hidden',
68
            $this->serializeBool($generator, $location->hidden)
69
        );
70
        $generator->endValueElement('hidden');
71
72
        $generator->startValueElement(
73
            'invisible',
74
            $this->serializeBool($generator, $location->invisible)
75
        );
76
        $generator->endValueElement('invisible');
77
78
        $generator->startObjectElement('ParentLocation', 'Location');
79
        if (trim($location->pathString, '/') !== '1') {
80
            $generator->startAttribute(
81
                'href',
82
                $this->router->generate(
83
                    'ezpublish_rest_loadLocation',
84
                    array(
85
                        'locationPath' => implode('/', array_slice($location->path, 0, count($location->path) - 1)),
86
                    )
87
                )
88
            );
89
            $generator->endAttribute('href');
90
        }
91
        $generator->endObjectElement('ParentLocation');
92
93
        $generator->startValueElement('pathString', $location->pathString);
94
        $generator->endValueElement('pathString');
95
96
        $generator->startValueElement('depth', $location->depth);
97
        $generator->endValueElement('depth');
98
99
        $generator->startValueElement('childCount', $this->locationService->getLocationChildCount($location));
100
        $generator->endValueElement('childCount');
101
102
        $generator->startValueElement('remoteId', $location->remoteId);
103
        $generator->endValueElement('remoteId');
104
105
        $generator->startObjectElement('Children', 'LocationList');
106
        $generator->startAttribute(
107
            'href',
108
            $this->router->generate(
109
                'ezpublish_rest_loadLocationChildren',
110
                array(
111
                    'locationPath' => trim($location->pathString, '/'),
112
                )
113
            )
114
        );
115
        $generator->endAttribute('href');
116
        $generator->endObjectElement('Children');
117
118
        $generator->startObjectElement('Content');
119
        $generator->startAttribute(
120
            'href',
121
            $this->router->generate('ezpublish_rest_loadContent', array('contentId' => $location->contentId))
122
        );
123
        $generator->endAttribute('href');
124
        $generator->endObjectElement('Content');
125
126
        $generator->startValueElement('sortField', $this->serializeSortField($location->sortField));
127
        $generator->endValueElement('sortField');
128
129
        $generator->startValueElement('sortOrder', $this->serializeSortOrder($location->sortOrder));
130
        $generator->endValueElement('sortOrder');
131
132
        $generator->startObjectElement('UrlAliases', 'UrlAliasRefList');
133
        $generator->startAttribute(
134
            'href',
135
            $this->router->generate(
136
                'ezpublish_rest_listLocationURLAliases',
137
                array('locationPath' => trim($location->pathString, '/'))
138
            )
139
        );
140
        $generator->endAttribute('href');
141
        $generator->endObjectElement('UrlAliases');
142
143
        $generator->startObjectElement('ContentInfo', 'ContentInfo');
144
        $generator->startAttribute(
145
            'href',
146
            $this->router->generate(
147
                'ezpublish_rest_loadContent',
148
                array('contentId' => $location->contentId)
149
            )
150
        );
151
        $generator->endAttribute('href');
152
        $visitor->visitValueObject(new RestContentValue($location->contentInfo));
153
        $generator->endObjectElement('ContentInfo');
154
    }
155
}
156