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

URLAlias::visitURLAliasAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 57
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 39
nc 2
nop 3
dl 0
loc 57
rs 9.6818
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 URLAlias 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\Core\REST\Common\Output\ValueObjectVisitor;
12
use eZ\Publish\Core\REST\Common\Output\Generator;
13
use eZ\Publish\Core\REST\Common\Output\Visitor;
14
use eZ\Publish\API\Repository\Values;
15
use eZ\Publish\API\Repository\Values\Content\URLAlias as URLAliasValue;
16
17
/**
18
 * URLAlias value object visitor.
19
 */
20
class URLAlias extends ValueObjectVisitor
21
{
22
    /**
23
     * Visit struct returned by controllers.
24
     *
25
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
26
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
27
     * @param \eZ\Publish\API\Repository\Values\Content\URLAlias $data
28
     */
29
    public function visit(Visitor $visitor, Generator $generator, $data)
30
    {
31
        $generator->startObjectElement('UrlAlias');
32
        $visitor->setHeader('Content-Type', $generator->getMediaType('UrlAlias'));
33
        $this->visitURLAliasAttributes($visitor, $generator, $data);
34
        $generator->endObjectElement('UrlAlias');
35
    }
36
37
    /**
38
     * Serializes the given $urlAliasType to a string representation.
39
     *
40
     * @param int $urlAliasType
41
     *
42
     * @return string
43
     */
44
    protected function serializeType($urlAliasType)
45
    {
46
        switch ($urlAliasType) {
47
            case Values\Content\URLAlias::LOCATION:
48
                return 'LOCATION';
49
50
            case Values\Content\URLAlias::RESOURCE:
51
                return 'RESOURCE';
52
53
            case Values\Content\URLAlias::VIRTUAL:
54
                return 'VIRTUAL';
55
        }
56
57
        throw new \RuntimeException("Unknown URL alias type: '{$urlAliasType}'.");
58
    }
59
60
    protected function visitURLAliasAttributes(Visitor $visitor, Generator $generator, URLAliasValue $data)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        $generator->startAttribute(
63
            'href',
64
            $this->router->generate('ezpublish_rest_loadURLAlias', array('urlAliasId' => $data->id))
65
        );
66
        $generator->endAttribute('href');
67
68
        $generator->startAttribute('id', $data->id);
69
        $generator->endAttribute('id');
70
71
        $generator->startAttribute('type', $this->serializeType($data->type));
72
        $generator->endAttribute('type');
73
74
        if ($data->type === Values\Content\URLAlias::LOCATION) {
75
            $generator->startObjectElement('location', 'Location');
76
            $generator->startAttribute(
77
                'href',
78
                $this->router->generate('ezpublish_rest_loadLocation', array('locationPath' => $data->destination))
79
            );
80
            $generator->endAttribute('href');
81
            $generator->endObjectElement('location');
82
        } else {
83
            $generator->startValueElement('resource', $data->destination);
84
            $generator->endValueElement('resource');
85
        }
86
87
        $generator->startValueElement('path', $data->path);
88
        $generator->endValueElement('path');
89
90
        $generator->startValueElement('languageCodes', implode(',', $data->languageCodes));
91
        $generator->endValueElement('languageCodes');
92
93
        $generator->startValueElement(
94
            'alwaysAvailable',
95
            $this->serializeBool($generator, $data->alwaysAvailable)
96
        );
97
        $generator->endValueElement('alwaysAvailable');
98
99
        $generator->startValueElement(
100
            'isHistory',
101
            $this->serializeBool($generator, $data->isHistory)
102
        );
103
        $generator->endValueElement('isHistory');
104
105
        $generator->startValueElement(
106
            'forward',
107
            $this->serializeBool($generator, $data->forward)
108
        );
109
        $generator->endValueElement('forward');
110
111
        $generator->startValueElement(
112
            'custom',
113
            $this->serializeBool($generator, $data->isCustom)
114
        );
115
        $generator->endValueElement('custom');
116
    }
117
}
118