Completed
Push — fallback_language_fields ( 531860 )
by André
37:34
created

Trash   B

Complexity

Total Complexity 16

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17
Metric Value
wmc 16
lcom 1
cbo 17
dl 0
loc 156
rs 7.8571

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B loadTrashItems() 0 23 6
A loadTrashItem() 0 7 1
A emptyTrash() 0 6 1
A deleteTrashItem() 0 8 1
B restoreTrashItem() 0 46 6
1
<?php
2
3
/**
4
 * File containing the Trash controller 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\Controller;
12
13
use eZ\Publish\Core\REST\Server\Values;
14
use eZ\Publish\Core\REST\Server\Controller as RestController;
15
use eZ\Publish\API\Repository\TrashService;
16
use eZ\Publish\API\Repository\LocationService;
17
use eZ\Publish\API\Repository\Values\Content\Query;
18
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
19
use eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException;
20
use InvalidArgumentException;
21
use Symfony\Component\HttpFoundation\Request;
22
23
/**
24
 * Trash controller.
25
 */
26
class Trash extends RestController
27
{
28
    /**
29
     * Trash service.
30
     *
31
     * @var \eZ\Publish\API\Repository\TrashService
32
     */
33
    protected $trashService;
34
35
    /**
36
     * Location service.
37
     *
38
     * @var \eZ\Publish\API\Repository\LocationService
39
     */
40
    protected $locationService;
41
42
    /**
43
     * Construct controller.
44
     *
45
     * @param \eZ\Publish\API\Repository\TrashService $trashService
46
     * @param \eZ\Publish\API\Repository\LocationService $locationService
47
     */
48
    public function __construct(TrashService $trashService, LocationService $locationService)
49
    {
50
        $this->trashService = $trashService;
51
        $this->locationService = $locationService;
52
    }
53
54
    /**
55
     * Returns a list of all trash items.
56
     *
57
     * @return \eZ\Publish\Core\REST\Server\Values\Trash
58
     */
59
    public function loadTrashItems(Request $request)
60
    {
61
        $offset = $request->query->has('offset') ? (int)$request->query->get('offset') : 0;
62
        $limit = $request->query->has('limit') ? (int)$request->query->get('limit') : -1;
63
64
        $query = new Query();
65
        $query->offset = $offset >= 0 ? $offset : null;
66
        $query->limit = $limit >= 0 ? $limit : null;
67
68
        $trashItems = array();
69
70
        foreach ($this->trashService->findTrashItems($query)->items as $trashItem) {
71
            $trashItems[] = new Values\RestTrashItem(
72
                $trashItem,
0 ignored issues
show
Compatibility introduced by
$trashItem of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...lues\Content\TrashItem>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\ValueObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
73
                $this->locationService->getLocationChildCount($trashItem)
0 ignored issues
show
Compatibility introduced by
$trashItem of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...alues\Content\Location>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\ValueObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
74
            );
75
        }
76
77
        return new Values\Trash(
78
            $trashItems,
79
            $request->getPathInfo()
80
        );
81
    }
82
83
    /**
84
     * Returns the trash item given by id.
85
     *
86
     * @param $trashItemId
87
     *
88
     * @return \eZ\Publish\Core\REST\Server\Values\RestTrashItem
89
     */
90
    public function loadTrashItem($trashItemId)
91
    {
92
        return new Values\RestTrashItem(
93
            $trashItem = $this->trashService->loadTrashItem($trashItemId),
94
            $this->locationService->getLocationChildCount($trashItem)
95
        );
96
    }
97
98
    /**
99
     * Empties the trash.
100
     *
101
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
102
     */
103
    public function emptyTrash()
104
    {
105
        $this->trashService->emptyTrash();
106
107
        return new Values\NoContent();
108
    }
109
110
    /**
111
     * Deletes the given trash item.
112
     *
113
     * @param $trashItemId
114
     *
115
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
116
     */
117
    public function deleteTrashItem($trashItemId)
118
    {
119
        $this->trashService->deleteTrashItem(
120
            $this->trashService->loadTrashItem($trashItemId)
121
        );
122
123
        return new Values\NoContent();
124
    }
125
126
    /**
127
     * Restores a trashItem.
128
     *
129
     * @param $trashItemId
130
     *
131
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
132
     *
133
     * @return \eZ\Publish\Core\REST\Server\Values\ResourceCreated
134
     */
135
    public function restoreTrashItem($trashItemId, Request $request)
136
    {
137
        $requestDestination = null;
138
        try {
139
            $requestDestination = $request->headers->get('Destination');
140
        } catch (InvalidArgumentException $e) {
141
            // No Destination header
142
        }
143
144
        $parentLocation = null;
145
        if ($request->headers->has('Destination')) {
146
            $locationPathParts = explode(
147
                '/',
148
                $this->requestParser->parseHref($request->headers->get('Destination'), 'locationPath')
1 ignored issue
show
Bug introduced by
It seems like $request->headers->get('Destination') targeting Symfony\Component\HttpFoundation\HeaderBag::get() can also be of type array; however, eZ\Publish\Core\REST\Com...uestParser::parseHref() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
149
            );
150
151
            try {
152
                $parentLocation = $this->locationService->loadLocation(array_pop($locationPathParts));
153
            } catch (NotFoundException $e) {
154
                throw new ForbiddenException($e->getMessage());
155
            }
156
        }
157
158
        $trashItem = $this->trashService->loadTrashItem($trashItemId);
159
160
        if ($requestDestination === null) {
161
            // If we're recovering under the original location
162
            // check if it exists, to return "403 Forbidden" in case it does not
163
            try {
164
                $this->locationService->loadLocation($trashItem->parentLocationId);
0 ignored issues
show
Documentation introduced by
The property $parentLocationId is declared protected in eZ\Publish\API\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
165
            } catch (NotFoundException $e) {
166
                throw new ForbiddenException($e->getMessage());
167
            }
168
        }
169
170
        $location = $this->trashService->recover($trashItem, $parentLocation);
171
172
        return new Values\ResourceCreated(
173
            $this->router->generate(
174
                'ezpublish_rest_loadLocation',
175
                array(
176
                    'locationPath' => trim($location->pathString, '/'),
177
                )
178
            )
179
        );
180
    }
181
}
182