Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a )
by
unknown
36:29
created

UserGroupRefList::visit()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 27
nc 3
nop 3
dl 0
loc 48
rs 8.7396
c 0
b 0
f 0
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
use eZ\Publish\Core\REST\Server\Values\ResourceRouteReference;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, eZ\Publish\Core\REST\Ser...\ResourceRouteReference.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
18
/**
19
 * UserGroupRefList value object visitor.
20
 */
21
class UserGroupRefList extends ValueObjectVisitor
22
{
23
    /**
24
     * Visit struct returned by controllers.
25
     *
26
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
27
     * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
28
     * @param \eZ\Publish\Core\REST\Server\Values\UserGroupRefList $data
29
     */
30
    public function visit(Visitor $visitor, Generator $generator, $data)
31
    {
32
        $generator->startObjectElement('UserGroupRefList');
33
        $visitor->setHeader('Content-Type', $generator->getMediaType('UserGroupRefList'));
34
        //@todo Needs refactoring, disabling certain headers should not be done this way
35
        $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...
36
37
        $generator->startAttribute('href', $data->path);
38
        $generator->endAttribute('href');
39
40
        $groupCount = count($data->userGroups);
41
42
        $generator->startList('UserGroup');
43
        foreach ($data->userGroups as $userGroup) {
44
            $generator->startObjectElement('UserGroup');
45
46
            $visitor->visitValueObject(
47
                new ResourceRouteReference(
48
                    'ezpublish_rest_loadUserGroup',
0 ignored issues
show
Documentation introduced by
'ezpublish_rest_loadUserGroup' is of type string, but the function expects a array.

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...
49
                    ['groupPath' => trim($userGroup->mainLocation->pathString, '/')]
50
                )
51
            );
52
53
            if ($data->userId !== null && $groupCount > 1) {
54
                $generator->startHashElement('unassign');
55
56
                $visitor->visitValueObject(
57
                    new ResourceRouteReference(
58
                        'ezpublish_rest_unassignUserFromUserGroup',
0 ignored issues
show
Documentation introduced by
'ezpublish_rest_unassignUserFromUserGroup' is of type string, but the function expects a array.

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...
59
                        [
60
                            'userId' => $data->userId,
61
                            'groupPath' => $userGroup->mainLocation->path[count($userGroup->mainLocation->path) - 1],
62
                        ]
63
                    )
64
                );
65
66
                $generator->startAttribute('method', 'DELETE');
67
                $generator->endAttribute('method');
68
69
                $generator->endHashElement('unassign');
70
            }
71
72
            $generator->endObjectElement('UserGroup');
73
        }
74
        $generator->endList('UserGroup');
75
76
        $generator->endObjectElement('UserGroupRefList');
77
    }
78
}
79