Passed
Pull Request — 2.2 (#1927)
by Antoine
02:55
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\InvalidArgumentException;
17
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...
18
use ApiPlatform\Core\Exception\PropertyNotFoundException;
19
use ApiPlatform\Core\Exception\RuntimeException;
20
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...
21
22
/**
23
 * @internal
24
 */
25
trait OperationDataProviderTrait
26
{
27
    /**
28
     * @var CollectionDataProviderInterface
29
     */
30
    private $collectionDataProvider;
31
32
    /**
33
     * @var ItemDataProviderInterface
34
     */
35
    private $itemDataProvider;
36
37
    /**
38
     * @var SubresourceDataProviderInterface
39
     */
40
    private $subresourceDataProvider;
41
42
    /**
43
     * @var ChainIdentifierDenormalizer
44
     */
45
    private $identifierDenormalizer;
46
47
    /**
48
     * Retrieves data for a collection operation.
49
     *
50
     * @return iterable|null
51
     */
52
    private function getCollectionData(array $attributes, array $context)
53
    {
54
        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...
55
    }
56
57
    /**
58
     * Gets data for an item operation.
59
     *
60
     * @throws NotFoundHttpException
61
     *
62
     * @return object|null
63
     */
64
    private function getItemData($identifiers, array $attributes, array $context)
65
    {
66
        try {
67
            return $this->itemDataProvider->getItem($attributes['resource_class'], $identifiers, $attributes['item_operation_name'], $context);
68
        } catch (PropertyNotFoundException $e) {
69
            return null;
70
        }
71
    }
72
73
    /**
74
     * Gets data for a nested operation.
75
     *
76
     * @throws NotFoundHttpException
77
     * @throws RuntimeException
78
     *
79
     * @return object|null
80
     */
81
    private function getSubresourceData($identifiers, array $attributes, array $context)
82
    {
83
        if (!$this->subresourceDataProvider) {
84
            throw new RuntimeException('Subresources not supported');
85
        }
86
87
        return $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'] + $context, $attributes['subresource_operation_name']);
88
    }
89
90
    /**
91
     * @param array $parameters - usually comes from $request->attributes->all()
92
     *
93
     * @throws InvalidIdentifierException
94
     */
95
    private function extractIdentifiers(array $parameters, array $attributes)
96
    {
97
        if (isset($attributes['item_operation_name'])) {
98
            if (!isset($parameters['id'])) {
99
                throw new InvalidArgumentException('Parameter "id" not found');
100
            }
101
102
            return $parameters['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
115
        return $identifiers;
116
    }
117
}
118