Completed
Pull Request — master (#584)
by Kévin
03:11
created

ActionUtilTrait::getItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 4
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Action;
13
14
use ApiPlatform\Core\Exception\RuntimeException;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * Utility class .
19
 *
20
 * @author Kévin Dunglas <[email protected]>
21
 */
22
trait ActionUtilTrait
23
{
24
    /**
25
     * Extract resource class, operation name and format request attributes. Throws an exception if the request does not contain required
26
     * attributes.
27
     *
28
     * @param Request $request
29
     *
30
     * @throws RuntimeException
31
     *
32
     * @return array
33
     */
34
    private function extractAttributes(Request $request)
35
    {
36
        $resourceClass = $request->attributes->get('_resource_class');
37
38
        if (!$resourceClass) {
39
            throw new RuntimeException('The request attribute "_resource_class" must be defined.');
40
        }
41
42
        $collectionOperation = $request->attributes->get('_collection_operation_name');
43
        $itemOperation = $request->attributes->get('_item_operation_name');
44
45
        if (!$itemOperation && !$collectionOperation) {
46
            throw new RuntimeException('One of the request attribute "_item_operation_name" or "_collection_operation_name" must be defined.');
47
        }
48
49
        $format = $request->attributes->get('_api_format');
50
        if (!$format) {
51
            throw new RuntimeException('The request attribute "_api_format" must be defined.');
52
        }
53
54
        return [$resourceClass, $collectionOperation, $itemOperation, $format];
55
    }
56
}
57