Passed
Push — master ( d3e373...d5c9d0 )
by Kévin
08:29 queued 05:32
created

getProvidersResponse()   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
final 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
            $this->dataProviders = $collectionDataProvider->dataProviders;
35
        }
36
    }
37
38
    public function getProvidersResponse(): array
39
    {
40
        return $this->providersResponse;
41
    }
42
43
    public function getContext(): array
44
    {
45
        return $this->context;
46
    }
47
48
    public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
49
    {
50
        $this->context = $context;
51
        $results = null;
52
        $match = false;
53
54
        foreach ($this->dataProviders as $dataProvider) {
55
            $this->providersResponse[\get_class($dataProvider)] = $match ? null : false;
56
            if ($match) {
57
                continue;
58
            }
59
            try {
60
                if ($dataProvider instanceof RestrictedDataProviderInterface
61
                    && !$dataProvider->supports($resourceClass, $operationName, $context)) {
62
                    continue;
63
                }
64
65
                $results = $dataProvider->getCollection($resourceClass, $operationName, $context);
66
                $this->providersResponse[\get_class($dataProvider)] = $match = true;
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
                continue;
70
            }
71
        }
72
73
        return $results ?? [];
74
    }
75
}
76