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

UserGroupRefList::visit()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 33
nc 3
nop 3
dl 0
loc 54
rs 9.0306
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 UserGroupRefList 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
17
/**
18
 * UserGroupRefList value object visitor.
19
 */
20
class UserGroupRefList 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\Core\REST\Server\Values\UserGroupRefList $data
28
     */
29
    public function visit(Visitor $visitor, Generator $generator, $data)
30
    {
31
        $generator->startObjectElement('UserGroupRefList');
32
        $visitor->setHeader('Content-Type', $generator->getMediaType('UserGroupRefList'));
33
        //@todo Needs refactoring, disabling certain headers should not be done this way
34
        $visitor->setHeader('Accept-Patch', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
36
        $generator->startAttribute('href', $data->path);
37
        $generator->endAttribute('href');
38
39
        $groupCount = count($data->userGroups);
40
41
        $generator->startList('UserGroup');
42
        foreach ($data->userGroups as $userGroup) {
43
            $generator->startObjectElement('UserGroup');
44
45
            $generator->startAttribute(
46
                'href',
47
                $this->router->generate(
48
                    'ezpublish_rest_loadUserGroup',
49
                    array(
50
                        'groupPath' => trim($userGroup->mainLocation->pathString, '/'),
51
                    )
52
                )
53
            );
54
            $generator->endAttribute('href');
55
56
            if ($data->userId !== null && $groupCount > 1) {
57
                $generator->startHashElement('unassign');
58
59
                $generator->startAttribute(
60
                    'href',
61
                    $this->router->generate(
62
                        'ezpublish_rest_unassignUserFromUserGroup',
63
                        array(
64
                            'userId' => $data->userId,
65
                            'groupPath' => $userGroup->mainLocation->path[count($userGroup->mainLocation->path) - 1],
66
                        )
67
                    )
68
                );
69
                $generator->endAttribute('href');
70
71
                $generator->startAttribute('method', 'DELETE');
72
                $generator->endAttribute('method');
73
74
                $generator->endHashElement('unassign');
75
            }
76
77
            $generator->endObjectElement('UserGroup');
78
        }
79
        $generator->endList('UserGroup');
80
81
        $generator->endObjectElement('UserGroupRefList');
82
    }
83
}
84