Passed
Pull Request — master (#2262)
by GRASSIOT
02:53
created

TraceableChainItemDataProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 33
dl 0
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getProvidersResponse() 0 3 1
A __construct() 0 6 2
B getItem() 0 35 10
A getContext() 0 3 1
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\Bridge\Symfony\Bundle\DataProvider;
15
16
use ApiPlatform\Core\DataProvider\ChainItemDataProvider;
17
use ApiPlatform\Core\DataProvider\DenormalizedIdentifiersAwareItemDataProviderInterface;
18
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
19
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
20
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
21
22
/**
23
 * @author Anthony GRASSIOT <[email protected]>
24
 */
25
class TraceableChainItemDataProvider implements ItemDataProviderInterface
26
{
27
    private $dataProviders = [];
28
    private $context = [];
29
    private $providersResponse = [];
30
31
    public function __construct(ItemDataProviderInterface $itemDataProvider)
32
    {
33
        if ($itemDataProvider instanceof ChainItemDataProvider) {
34
            $reflection = new \ReflectionProperty(ChainItemDataProvider::class, 'dataProviders');
35
            $reflection->setAccessible(true);
36
            $this->dataProviders = $reflection->getValue($itemDataProvider);
37
        }
38
    }
39
40
    public function getProvidersResponse(): array
41
    {
42
        return $this->providersResponse;
43
    }
44
45
    public function getContext(): array
46
    {
47
        return $this->context;
48
    }
49
50
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
51
    {
52
        $this->context = $context;
53
        foreach ($this->dataProviders as $dataProvider) {
54
            $this->providersResponse[\get_class($dataProvider)] = null;
55
        }
56
57
        foreach ($this->dataProviders as $dataProvider) {
58
            try {
59
                if ($dataProvider instanceof RestrictedDataProviderInterface
60
                    && !$dataProvider->supports($resourceClass, $operationName, $context)) {
61
                    $this->providersResponse[\get_class($dataProvider)] = false;
62
                    continue;
63
                }
64
65
                $identifier = $id;
66
                if (!$dataProvider instanceof DenormalizedIdentifiersAwareItemDataProviderInterface && $identifier && \is_array($identifier)) {
67
                    if (\count($identifier) > 1) {
68
                        @trigger_error(sprintf('Receiving "$id" as non-array in an item data provider is deprecated in 2.3 in favor of implementing "%s".', DenormalizedIdentifiersAwareItemDataProviderInterface::class), E_USER_DEPRECATED);
69
                        $identifier = http_build_query($identifier, '', ';');
70
                    } else {
71
                        $identifier = current($identifier);
72
                    }
73
                }
74
                $this->providersResponse[\get_class($dataProvider)] = true;
75
76
                return $dataProvider->getItem($resourceClass, $identifier, $operationName, $context);
77
            } catch (ResourceClassNotSupportedException $e) {
78
                @trigger_error(sprintf('Throwing a "%s" is deprecated in favor of implementing "%s"', \get_class($e), RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
79
                $this->providersResponse[\get_class($dataProvider)] = false;
80
                continue;
81
            }
82
        }
83
84
        return null;
85
    }
86
}
87