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

TraceableChainSubresourceDataProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 25
dl 0
loc 49
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getSubresource() 0 24 7
A getContext() 0 3 1
A __construct() 0 6 2
A getProvidersResponse() 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\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
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
            $reflection = new \ReflectionProperty(ChainSubresourceDataProvider::class, 'dataProviders');
34
            $reflection->setAccessible(true);
35
            $this->dataProviders = $reflection->getValue($subresourceDataProvider);
36
        }
37
    }
38
39
    public function getProvidersResponse(): array
40
    {
41
        return $this->providersResponse;
42
    }
43
44
    public function getContext(): array
45
    {
46
        return $this->context;
47
    }
48
49
    public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null)
50
    {
51
        $this->context = $context;
52
        foreach ($this->dataProviders as $dataProvider) {
53
            $this->providersResponse[\get_class($dataProvider)] = null;
54
        }
55
56
        foreach ($this->dataProviders as $dataProvider) {
57
            try {
58
                if ($dataProvider instanceof RestrictedDataProviderInterface && !$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->getSubresource($resourceClass, $identifiers, $context, $operationName);
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 ($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 null|object.
Loading history...
73
    }
74
}
75