Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

TraceableChainSubresourceDataProvider.php (1 issue)

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\ChainSubresourceDataProvider;
17
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
18
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
19
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
20
21
/**
22
 * @author Anthony GRASSIOT <[email protected]>
23
 */
24
final class TraceableChainSubresourceDataProvider implements SubresourceDataProviderInterface
25
{
26
    private $dataProviders = [];
27
    private $context = [];
28
    private $providersResponse = [];
29
30
    public function __construct(SubresourceDataProviderInterface $subresourceDataProvider)
31
    {
32
        if ($subresourceDataProvider instanceof ChainSubresourceDataProvider) {
33
            $this->dataProviders = $subresourceDataProvider->dataProviders;
34
        }
35
    }
36
37
    public function getProvidersResponse(): array
38
    {
39
        return $this->providersResponse;
40
    }
41
42
    public function getContext(): array
43
    {
44
        return $this->context;
45
    }
46
47
    public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null)
48
    {
49
        $this->context = $context;
50
        $match = false;
51
        $result = null;
52
53
        foreach ($this->dataProviders as $dataProvider) {
54
            $this->providersResponse[\get_class($dataProvider)] = $match ? null : false;
55
            if ($match) {
56
                continue;
57
            }
58
            try {
59
                if ($dataProvider instanceof RestrictedDataProviderInterface && !$dataProvider->supports($resourceClass, $operationName, $context)) {
60
                    continue;
61
                }
62
63
                $result = $dataProvider->getSubresource($resourceClass, $identifiers, $context, $operationName);
64
                $this->providersResponse[\get_class($dataProvider)] = $match = true;
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
                continue;
68
            }
69
        }
70
71
        if ($match) {
72
            return $result;
73
        }
74
75
        return ($context['collection'] ?? false) ? [] : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $context['collect... false ? array() : null also could return the type array which is incompatible with the return type mandated by ApiPlatform\Core\DataPro...rface::getSubresource() of iterable|null|object.
Loading history...
76
    }
77
}
78