Passed
Pull Request — 2.2 (#1927)
by Antoine
02:43
created

OperationDataProviderTrait::extractIdentifiers()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 10
nc 5
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;
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\RuntimeException;
18
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...
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 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...
94
            }
95
96
            return $parameters['id'];
97
        }
98
99
        $identifiers = [];
100
101
        foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass, $hasIdentifier)) {
102
            if (false === $hasIdentifier) {
103
                continue;
104
            }
105
106
            $identifiers[$id] = $parameters[$id];
107
        }
108
109
        return $identifiers;
110
    }
111
}
112