Passed
Pull Request — master (#2262)
by GRASSIOT
03:12
created

getCollection()   A

Complexity

Conditions 6
Paths 14

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 25
rs 9.1111
c 0
b 0
f 0
cc 6
nc 14
nop 3
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
        foreach ($this->dataProviders as $dataProvider) {
52
            $this->providersResponse[\get_class($dataProvider)] = null;
53
        }
54
55
        foreach ($this->dataProviders as $dataProvider) {
56
            try {
57
                if ($dataProvider instanceof RestrictedDataProviderInterface
58
                    && !$dataProvider->supports($resourceClass, $operationName, $context)) {
59
                    $this->providersResponse[\get_class($dataProvider)] = false;
60
                    continue;
61
                }
62
                $this->providersResponse[\get_class($dataProvider)] = true;
63
64
                return $dataProvider->getCollection($resourceClass, $operationName, $context);
65
            } catch (ResourceClassNotSupportedException $e) {
66
                @trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
67
                $this->providersResponse[\get_class($dataProvider)] = false;
68
                continue;
69
            }
70
        }
71
72
        return [];
73
    }
74
}
75