Passed
Pull Request — master (#1877)
by Antoine
03:29
created

OperationDataProviderTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 106
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getItemData() 0 21 4
A getCollectionData() 0 7 2
C getSubresourceData() 0 35 8
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
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
21
trait OperationDataProviderTrait
22
{
23
    /**
24
     * @var CollectionDataProviderInterface
25
     */
26
    private $collectionDataProvider;
27
    /**
28
     * @var ItemDataProviderInterface
29
     */
30
    private $itemDataProvider;
31
    /**
32
     * @var SubresourceDataProviderInterface
33
     */
34
    private $subresourceDataProvider;
35
    /**
36
     * @var ChainIdentifierDenormalizer
37
     */
38
    private $identifierDenormalizer;
39
40
    /**
41
     * Retrieves data for a collection operation.
42
     *
43
     * @return array|\Traversable|null
44
     */
45
    private function getCollectionData(Request $request, array $attributes, array $context)
46
    {
47
        if ($request->isMethod('POST')) {
48
            return null;
49
        }
50
51
        return $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $context);
52
    }
53
54
    /**
55
     * Gets data for an item operation.
56
     *
57
     * @throws NotFoundHttpException
58
     *
59
     * @return object|null
60
     */
61
    private function getItemData(Request $request, array $attributes, array $context)
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

61
    private function getItemData(Request $request, array $attributes, /** @scrutinizer ignore-unused */ array $context)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        $id = $request->attributes->get('id');
64
        $context = [];
65
66
        try {
67
            if ($this->identifierDenormalizer) {
68
                $id = $this->identifierDenormalizer->denormalize((string) $id, $attributes['resource_class']);
69
                $context = [ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER => true];
70
            }
71
72
            $data = $this->itemDataProvider->getItem($attributes['resource_class'], $id, $attributes['item_operation_name'], $context);
73
        } catch (InvalidIdentifierException $e) {
74
            $data = null;
75
        }
76
77
        if (null === $data) {
78
            throw new NotFoundHttpException('Not Found');
79
        }
80
81
        return $data;
82
    }
83
84
    /**
85
     * Gets data for a nested operation.
86
     *
87
     * @throws NotFoundHttpException
88
     * @throws RuntimeException
89
     *
90
     * @return object|null
91
     */
92
    private function getSubresourceData(Request $request, array $attributes, array $context)
93
    {
94
        if (null === $this->subresourceDataProvider) {
95
            throw new RuntimeException('No subresource data provider.');
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Core\DataProvider\RuntimeException was not found. Did you mean RuntimeException? If so, make sure to prefix the type with \.
Loading history...
96
        }
97
98
        $attributes['subresource_context'] += $context;
99
        $identifiers = [];
100
        if ($this->identifierDenormalizer) {
101
            $attributes['subresource_context'][ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] = true;
102
        }
103
104
        foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass, $hasIdentifier)) {
105
            if (false === $hasIdentifier) {
106
                continue;
107
            }
108
109
            $identifiers[$id] = $request->attributes->get($id);
110
111
            if ($this->identifierDenormalizer) {
112
                try {
113
                    $identifiers[$id] = $this->identifierDenormalizer->denormalize((string) $identifiers[$id], $resourceClass);
114
                } catch (InvalidIdentifierException $e) {
115
                    throw new NotFoundHttpException('Not Found');
116
                }
117
            }
118
        }
119
120
        $data = $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'], $attributes['subresource_operation_name']);
121
122
        if (null === $data) {
123
            throw new NotFoundHttpException('Not Found.');
124
        }
125
126
        return $data;
127
    }
128
}
129