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

OperationDataProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C getDataFromRequestParameters() 0 27 7
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\ItemNotFoundException;
18
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer;
19
use ApiPlatform\Core\Util\RequestAttributesExtractor;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
23
final class OperationDataProvider
24
{
25
    use OperationDataProviderTrait;
0 ignored issues
show
Bug introduced by
The trait ApiPlatform\Core\DataPro...rationDataProviderTrait requires the property $attributes which is not provided by ApiPlatform\Core\DataPro...r\OperationDataProvider.
Loading history...
26
27
    public function __construct(CollectionDataProviderInterface $collectionDataProvider, ItemDataProviderInterface $itemDataProvider, SubresourceDataProviderInterface $subresourceDataProvider, ChainIdentifierDenormalizer $identifierDenormalizer)
28
    {
29
        $this->collectionDataProvider = $collectionDataProvider;
30
        $this->itemDataProvider = $itemDataProvider;
31
        $this->subresourceDataProvider = $subresourceDataProvider;
32
        $this->identifierDenormalizer = $identifierDenormalizer;
33
    }
34
35
    /**
36
     * Construct a request from given parameters and get the corresponding operation data.
37
     */
38
    public function getDataFromRequestParameters(string $iri = null, array $parameters, array $context)
39
    {
40
        if (!isset($parameters['_api_resource_class'])) {
41
            throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri));
42
        }
43
44
        $request = new Request([], [], $parameters);
45
        $attributes = RequestAttributesExtractor::extractAttributes($request);
46
47
        if (isset($parameters['_api_subresource_operation_name'])) {
48
            if ($item = $this->getSubresourceData($request, $attributes, $context)) {
49
                return $item;
50
            }
51
52
            throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri));
53
        }
54
55
        if (!isset($parameters['id'])) {
56
            throw new InvalidArgumentException(sprintf('Missing parameter "id" on "%s".', $iri));
57
        }
58
59
        try {
60
            if ($item = $this->getItemData($request, $attributes, $context)) {
61
                return $item;
62
            }
63
        } catch (NotFoundHttpException $e) {
64
            throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri));
65
        }
66
    }
67
}
68