Passed
Pull Request — 2.2 (#1927)
by Antoine
03:18
created

OperationDataProviderTrait::getSubresourceData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 3
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;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Core\Excepti...alidIdentifierException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use ApiPlatform\Core\Exception\PropertyNotFoundException;
18
use ApiPlatform\Core\Exception\RuntimeException;
19
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Core\Identif...nIdentifierDenormalizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
/**
22
 * @internal
23
 */
24
trait OperationDataProviderTrait
25
{
26
    /**
27
     * @var CollectionDataProviderInterface
28
     */
29
    private $collectionDataProvider;
30
31
    /**
32
     * @var ItemDataProviderInterface
33
     */
34
    private $itemDataProvider;
35
36
    /**
37
     * @var SubresourceDataProviderInterface
38
     */
39
    private $subresourceDataProvider;
40
41
    /**
42
     * @var ChainIdentifierDenormalizer
43
     */
44
    private $identifierDenormalizer;
45
46
    /**
47
     * Retrieves data for a collection operation.
48
     *
49
     * @return iterable|null
50
     */
51
    private function getCollectionData(array $attributes, array $context)
52
    {
53
        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...
54
    }
55
56
    /**
57
     * Gets data for an item operation.
58
     *
59
     * @throws NotFoundHttpException
60
     *
61
     * @return object|null
62
     */
63
    private function getItemData($identifiers, array $attributes, array $context)
64
    {
65
        try {
66
            return $this->itemDataProvider->getItem($attributes['resource_class'], $identifiers, $attributes['item_operation_name'], $context);
67
        } catch (PropertyNotFoundException $e) {
68
            return null;
69
        }
70
    }
71
72
    /**
73
     * Gets data for a nested operation.
74
     *
75
     * @throws NotFoundHttpException
76
     * @throws RuntimeException
77
     *
78
     * @return object|null
79
     */
80
    private function getSubresourceData($identifiers, array $attributes, array $context)
81
    {
82
        if (!$this->subresourceDataProvider) {
83
            throw new RuntimeException('Subresources not supported');
84
        }
85
86
        return $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'] + $context, $attributes['subresource_operation_name']);
87
    }
88
89
    /**
90
     * @param array $parameters - usually comes from $request->attributes->all()
91
     *
92
     * @throws InvalidIdentifierException
93
     */
94
    private function extractIdentifiers(array $parameters, array $attributes)
95
    {
96
        if (isset($attributes['item_operation_name'])) {
97
            if (!isset($parameters['id'])) {
98
                throw new InvalidArgumentException('Parameter "id" not found');
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Core\DataPro...nvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
99
            }
100
101
            return $parameters['id'];
102
        }
103
104
        $identifiers = [];
105
106
        foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass, $hasIdentifier)) {
107
            if (false === $hasIdentifier) {
108
                continue;
109
            }
110
111
            $identifiers[$id] = $parameters[$id];
112
        }
113
114
        return $identifiers;
115
    }
116
}
117