Passed
Pull Request — master (#1908)
by Ben
02:54
created

OperationDataProviderTrait::extractIdentifiers()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 7
nop 2
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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\DataProvider;
15
16
use ApiPlatform\Core\Exception\InvalidIdentifierException;
17
use ApiPlatform\Core\Exception\RuntimeException;
18
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
19
20
/**
21
 * @internal
22
 */
23
trait OperationDataProviderTrait
24
{
25
    /**
26
     * @var CollectionDataProviderInterface
27
     */
28
    private $collectionDataProvider;
29
30
    /**
31
     * @var ItemDataProviderInterface
32
     */
33
    private $itemDataProvider;
34
35
    /**
36
     * @var SubresourceDataProviderInterface
37
     */
38
    private $subresourceDataProvider;
39
40
    /**
41
     * @var ChainIdentifierDenormalizer
42
     */
43
    private $identifierDenormalizer;
44
45
    /**
46
     * Retrieves data for a collection operation.
47
     *
48
     * @return iterable|null
49
     */
50
    private function getCollectionData(array $attributes, array $context)
51
    {
52
        return $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $context);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->collection...ation_name'], $context) returns the type Traversable|array which is incompatible with the documented return type iterable|null.
Loading history...
53
    }
54
55
    /**
56
     * Gets data for an item operation.
57
     *
58
     * @throws NotFoundHttpException
59
     *
60
     * @return object|null
61
     */
62
    private function getItemData($identifiers, array $attributes, array $context)
63
    {
64
        return $this->itemDataProvider->getItem($attributes['resource_class'], $identifiers, $attributes['item_operation_name'], $context);
65
    }
66
67
    /**
68
     * Gets data for a nested operation.
69
     *
70
     * @throws NotFoundHttpException
71
     * @throws RuntimeException
72
     *
73
     * @return object|null
74
     */
75
    private function getSubresourceData($identifiers, array $attributes, array $context)
76
    {
77
        if (!$this->subresourceDataProvider) {
78
            throw new RuntimeException('Subresources not supported');
79
        }
80
81
        return $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'] + $context, $attributes['subresource_operation_name']);
82
    }
83
84
    /**
85
     * @param array $parameters - usually comes from $request->attributes->all()
86
     *
87
     * @throws InvalidIdentifierException
88
     */
89
    private function extractIdentifiers(array $parameters, array $attributes)
90
    {
91
        if (isset($attributes['item_operation_name'])) {
92
            if (!isset($parameters['id'])) {
93
                throw new InvalidIdentifierException('Parameter "id" not found');
94
            }
95
96
            $id = $parameters['id'];
97
98
            if ($this->identifierDenormalizer) {
99
                return $this->identifierDenormalizer->denormalize((string) $id, $attributes['resource_class']);
100
            }
101
102
            return $id;
103
        }
104
105
        $identifiers = [];
106
107
        foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass, $hasIdentifier)) {
108
            if (false === $hasIdentifier) {
109
                continue;
110
            }
111
112
            $identifiers[$id] = $parameters[$id];
113
114
            if ($this->identifierDenormalizer) {
115
                $identifiers[$id] = $this->identifierDenormalizer->denormalize((string) $identifiers[$id], $resourceClass);
116
            }
117
        }
118
119
        return $identifiers;
120
    }
121
}
122