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

ObjectState::deleteObjectStateGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * File containing the ObjectState 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\Common\Message;
14
use eZ\Publish\Core\REST\Server\Values;
15
use eZ\Publish\Core\REST\Common\Values\RestObjectState;
16
use eZ\Publish\Core\REST\Server\Controller as RestController;
17
use eZ\Publish\API\Repository\ObjectStateService;
18
use eZ\Publish\API\Repository\ContentService;
19
use eZ\Publish\Core\REST\Common\Values\ContentObjectStates;
20
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
21
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
22
use eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException;
23
use Symfony\Component\HttpFoundation\Request;
24
25
/**
26
 * ObjectState controller.
27
 */
28
class ObjectState extends RestController
29
{
30
    /**
31
     * ObjectState service.
32
     *
33
     * @var \eZ\Publish\API\Repository\ObjectStateService
34
     */
35
    protected $objectStateService;
36
37
    /**
38
     * Content service.
39
     *
40
     * @var \eZ\Publish\API\Repository\ContentService
41
     */
42
    protected $contentService;
43
44
    /**
45
     * Construct controller.
46
     *
47
     * @param \eZ\Publish\API\Repository\ObjectStateService $objectStateService
48
     * @param \eZ\Publish\API\Repository\ContentService $contentService
49
     */
50
    public function __construct(ObjectStateService $objectStateService, ContentService $contentService)
51
    {
52
        $this->objectStateService = $objectStateService;
53
        $this->contentService = $contentService;
54
    }
55
56
    /**
57
     * Creates a new object state group.
58
     *
59
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
60
     *
61
     * @return \eZ\Publish\Core\REST\Server\Values\CreatedObjectStateGroup
62
     */
63 View Code Duplication
    public function createObjectStateGroup(Request $request)
64
    {
65
        try {
66
            $createdStateGroup = $this->objectStateService->createObjectStateGroup(
67
                $this->inputDispatcher->parse(
0 ignored issues
show
Compatibility introduced by
$this->inputDispatcher->...request->getContent())) of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...StateGroupCreateStruct>. 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...
68
                    new Message(
69
                        array('Content-Type' => $request->headers->get('Content-Type')),
70
                        $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() 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...
71
                    )
72
                )
73
            );
74
        } catch (InvalidArgumentException $e) {
75
            throw new ForbiddenException($e->getMessage());
76
        }
77
78
        return new Values\CreatedObjectStateGroup(
79
            array(
80
                'objectStateGroup' => $createdStateGroup,
81
            )
82
        );
83
    }
84
85
    /**
86
     * Creates a new object state.
87
     *
88
     * @param $objectStateGroupId
89
     *
90
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
91
     *
92
     * @return \eZ\Publish\Core\REST\Server\Values\CreatedObjectState
93
     */
94
    public function createObjectState($objectStateGroupId, Request $request)
95
    {
96
        $objectStateGroup = $this->objectStateService->loadObjectStateGroup($objectStateGroupId);
97
98
        try {
99
            $createdObjectState = $this->objectStateService->createObjectState(
100
                $objectStateGroup,
101
                $this->inputDispatcher->parse(
0 ignored issues
show
Compatibility introduced by
$this->inputDispatcher->...request->getContent())) of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...bjectStateCreateStruct>. 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...
102
                    new Message(
103
                        array('Content-Type' => $request->headers->get('Content-Type')),
104
                        $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() 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...
105
                    )
106
                )
107
            );
108
        } catch (InvalidArgumentException $e) {
109
            throw new ForbiddenException($e->getMessage());
110
        }
111
112
        return new Values\CreatedObjectState(
113
            array(
114
                'objectState' => new RestObjectState(
115
                    $createdObjectState,
116
                    $objectStateGroup->id
117
                ),
118
            )
119
        );
120
    }
121
122
    /**
123
     * Loads an object state group.
124
     *
125
     * @param $objectStateGroupId
126
     *
127
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
128
     */
129
    public function loadObjectStateGroup($objectStateGroupId)
130
    {
131
        return $this->objectStateService->loadObjectStateGroup($objectStateGroupId);
132
    }
133
134
    /**
135
     * Loads an object state.
136
     *
137
     * @param $objectStateGroupId
138
     * @param $objectStateId
139
     *
140
     * @return \eZ\Publish\Core\REST\Common\Values\RestObjectState
141
     */
142
    public function loadObjectState($objectStateGroupId, $objectStateId)
143
    {
144
        return new RestObjectState(
145
            $this->objectStateService->loadObjectState($objectStateId),
146
            $objectStateGroupId
147
        );
148
    }
149
150
    /**
151
     * Returns a list of all object state groups.
152
     *
153
     * @return \eZ\Publish\Core\REST\Server\Values\ObjectStateGroupList
154
     */
155
    public function loadObjectStateGroups()
156
    {
157
        return new Values\ObjectStateGroupList(
158
            $this->objectStateService->loadObjectStateGroups()
159
        );
160
    }
161
162
    /**
163
     * Returns a list of all object states of the given group.
164
     *
165
     * @param $objectStateGroupId
166
     *
167
     * @return \eZ\Publish\Core\REST\Server\Values\ObjectStateList
168
     */
169
    public function loadObjectStates($objectStateGroupId)
170
    {
171
        $objectStateGroup = $this->objectStateService->loadObjectStateGroup($objectStateGroupId);
172
173
        return new Values\ObjectStateList(
174
            $this->objectStateService->loadObjectStates($objectStateGroup),
175
            $objectStateGroup->id
176
        );
177
    }
178
179
    /**
180
     * The given object state group including the object states is deleted.
181
     *
182
     * @param $objectStateGroupId
183
     *
184
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
185
     */
186
    public function deleteObjectStateGroup($objectStateGroupId)
187
    {
188
        $this->objectStateService->deleteObjectStateGroup(
189
            $this->objectStateService->loadObjectStateGroup($objectStateGroupId)
190
        );
191
192
        return new Values\NoContent();
193
    }
194
195
    /**
196
     * The given object state is deleted.
197
     *
198
     * @param $objectStateId
199
     *
200
     * @return \eZ\Publish\Core\REST\Server\Values\NoContent
201
     */
202
    public function deleteObjectState($objectStateId)
203
    {
204
        $this->objectStateService->deleteObjectState(
205
            $this->objectStateService->loadObjectState($objectStateId)
206
        );
207
208
        return new Values\NoContent();
209
    }
210
211
    /**
212
     * Updates an object state group.
213
     *
214
     * @param $objectStateGroupId
215
     *
216
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
217
     *
218
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
219
     */
220 View Code Duplication
    public function updateObjectStateGroup($objectStateGroupId, Request $request)
221
    {
222
        $updateStruct = $this->inputDispatcher->parse(
223
            new Message(
224
                array('Content-Type' => $request->headers->get('Content-Type')),
225
                $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() 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...
226
            )
227
        );
228
229
        $objectStateGroup = $this->objectStateService->loadObjectStateGroup($objectStateGroupId);
230
231
        try {
232
            $updatedStateGroup = $this->objectStateService->updateObjectStateGroup($objectStateGroup, $updateStruct);
0 ignored issues
show
Compatibility introduced by
$updateStruct of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...StateGroupUpdateStruct>. 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...
233
234
            return $updatedStateGroup;
235
        } catch (InvalidArgumentException $e) {
236
            throw new ForbiddenException($e->getMessage());
237
        }
238
    }
239
240
    /**
241
     * Updates an object state.
242
     *
243
     * @param $objectStateGroupId
244
     * @param $objectStateId
245
     *
246
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
247
     *
248
     * @return \eZ\Publish\Core\REST\Common\Values\RestObjectState
249
     */
250 View Code Duplication
    public function updateObjectState($objectStateGroupId, $objectStateId, Request $request)
251
    {
252
        $updateStruct = $this->inputDispatcher->parse(
253
            new Message(
254
                array('Content-Type' => $request->headers->get('Content-Type')),
255
                $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() 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...
256
            )
257
        );
258
259
        $objectState = $this->objectStateService->loadObjectState($objectStateId);
260
261
        try {
262
            $updatedObjectState = $this->objectStateService->updateObjectState($objectState, $updateStruct);
0 ignored issues
show
Compatibility introduced by
$updateStruct of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...bjectStateUpdateStruct>. 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...
263
264
            return new RestObjectState($updatedObjectState, $objectStateGroupId);
265
        } catch (InvalidArgumentException $e) {
266
            throw new ForbiddenException($e->getMessage());
267
        }
268
    }
269
270
    /**
271
     * Returns the object states of content.
272
     *
273
     * @param $contentId
274
     *
275
     * @return \eZ\Publish\Core\REST\Common\Values\ContentObjectStates
276
     */
277
    public function getObjectStatesForContent($contentId)
278
    {
279
        $groups = $this->objectStateService->loadObjectStateGroups();
280
        $contentInfo = $this->contentService->loadContentInfo($contentId);
281
282
        $contentObjectStates = array();
283
284
        foreach ($groups as $group) {
285
            try {
286
                $state = $this->objectStateService->getContentState($contentInfo, $group);
287
                $contentObjectStates[] = new RestObjectState($state, $group->id);
288
            } catch (NotFoundException $e) {
289
                // Do nothing
290
            }
291
        }
292
293
        return new ContentObjectStates($contentObjectStates);
294
    }
295
296
    /**
297
     * Updates object states of content
298
     * An object state in the input overrides the state of the object state group.
299
     *
300
     * @param $contentId
301
     *
302
     * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
303
     *
304
     * @return \eZ\Publish\Core\REST\Common\Values\ContentObjectStates
305
     */
306
    public function setObjectStatesForContent($contentId, Request $request)
307
    {
308
        $newObjectStates = $this->inputDispatcher->parse(
309
            new Message(
310
                array('Content-Type' => $request->headers->get('Content-Type')),
311
                $request->getContent()
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, eZ\Publish\Core\REST\Common\Message::__construct() 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...
312
            )
313
        );
314
315
        $countByGroups = array();
316
        foreach ($newObjectStates as $newObjectState) {
0 ignored issues
show
Bug introduced by
The expression $newObjectStates of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not traversable.
Loading history...
317
            $groupId = (int)$newObjectState->groupId;
318
            if (array_key_exists($groupId, $countByGroups)) {
319
                ++$countByGroups[$groupId];
320
            } else {
321
                $countByGroups[$groupId] = 1;
322
            }
323
        }
324
325
        foreach ($countByGroups as $groupId => $count) {
326
            if ($count > 1) {
327
                throw new ForbiddenException("Multiple object states provided for group with ID $groupId");
328
            }
329
        }
330
331
        $contentInfo = $this->contentService->loadContentInfo($contentId);
332
333
        $contentObjectStates = array();
334
        foreach ($newObjectStates as $newObjectState) {
0 ignored issues
show
Bug introduced by
The expression $newObjectStates of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not traversable.
Loading history...
335
            $objectStateGroup = $this->objectStateService->loadObjectStateGroup($newObjectState->groupId);
336
            $this->objectStateService->setContentState($contentInfo, $objectStateGroup, $newObjectState->objectState);
337
            $contentObjectStates[(int)$objectStateGroup->id] = $newObjectState;
338
        }
339
340
        return new ContentObjectStates($contentObjectStates);
341
    }
342
}
343