Passed
Pull Request — 2.2 (#1877)
by Antoine
03:01
created

OperationDataProviderTrait::getCollectionData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
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\Identifier\Normalizer\ChainIdentifierDenormalizer;
18
19
trait OperationDataProviderTrait
20
{
21
    /**
22
     * @var CollectionDataProviderInterface
23
     */
24
    private $collectionDataProvider;
25
    /**
26
     * @var ItemDataProviderInterface
27
     */
28
    private $itemDataProvider;
29
    /**
30
     * @var SubresourceDataProviderInterface
31
     */
32
    private $subresourceDataProvider;
33
    /**
34
     * @var ChainIdentifierDenormalizer
35
     */
36
    private $identifierDenormalizer;
37
38
    /**
39
     * Retrieves data for a collection operation.
40
     *
41
     * @return array|\Traversable|null
42
     */
43
    private function getCollectionData(array $attributes, array $context)
44
    {
45
        return $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $context);
46
    }
47
48
    /**
49
     * Gets data for an item operation.
50
     *
51
     * @throws NotFoundHttpException
52
     *
53
     * @return object|null
54
     */
55
    private function getItemData($identifiers, array $attributes, array $context)
56
    {
57
        return $this->itemDataProvider->getItem($attributes['resource_class'], $identifiers, $attributes['item_operation_name'], $context);
58
    }
59
60
    /**
61
     * Gets data for a nested operation.
62
     *
63
     * @throws NotFoundHttpException
64
     * @throws RuntimeException
65
     *
66
     * @return object|null
67
     */
68
    private function getSubresourceData($identifiers, array $attributes, array $context)
69
    {
70
        return $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'] + $context, $attributes['subresource_operation_name']);
71
    }
72
73
    /**
74
     * @param array $parameters - usually comes from $request->attributes->all()
75
     *
76
     * @throws InvalidIdentifierException
77
     */
78
    private function extractIdentifiers(array $parameters, array $attributes)
79
    {
80
        if (isset($attributes['item_operation_name'])) {
81
            $id = $parameters['id'];
82
83
            if ($this->identifierDenormalizer) {
84
                return $this->identifierDenormalizer->denormalize((string) $id, $attributes['resource_class']);
85
            }
86
87
            return $id;
88
        }
89
90
        $identifiers = [];
91
92
        foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass, $hasIdentifier)) {
93
            if (false === $hasIdentifier) {
94
                continue;
95
            }
96
97
            $identifiers[$id] = $parameters[$id];
98
99
            if ($this->identifierDenormalizer) {
100
                $identifiers[$id] = $this->identifierDenormalizer->denormalize((string) $identifiers[$id], $resourceClass);
101
            }
102
        }
103
104
        return $identifiers;
105
    }
106
}
107