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

TraceableChainCollectionDataProvider::getContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\ChainCollectionDataProvider;
17
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
18
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
19
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
20
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
21
22
/**
23
 * @author Anthony GRASSIOT <[email protected]>
24
 */
25
class TraceableChainCollectionDataProvider implements ContextAwareCollectionDataProviderInterface
26
{
27
    private $dataProviders = [];
28
    private $context = [];
29
    private $providersResponse = [];
30
31
    public function __construct(CollectionDataProviderInterface $collectionDataProvider)
32
    {
33
        if ($collectionDataProvider instanceof ChainCollectionDataProvider) {
34
            $reflection = new \ReflectionProperty(ChainCollectionDataProvider::class, 'dataProviders');
35
            $reflection->setAccessible(true);
36
            $this->dataProviders = $reflection->getValue($collectionDataProvider);
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 getCollection(string $resourceClass, 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
                $this->providersResponse[\get_class($dataProvider)] = true;
65
66
                return $dataProvider->getCollection($resourceClass, $operationName, $context);
67
            } catch (ResourceClassNotSupportedException $e) {
68
                @trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
69
                $this->providersResponse[\get_class($dataProvider)] = false;
70
                continue;
71
            }
72
        }
73
74
        return [];
75
    }
76
}
77