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

TraceableChainSubresourceDataProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 23
dl 0
loc 47
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 4 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
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
        foreach ($this->dataProviders as $dataProvider) {
51
            $this->providersResponse[\get_class($dataProvider)] = null;
52
        }
53
54
        foreach ($this->dataProviders as $dataProvider) {
55
            try {
56
                if ($dataProvider instanceof RestrictedDataProviderInterface && !$dataProvider->supports($resourceClass, $operationName, $context)) {
57
                    $this->providersResponse[\get_class($dataProvider)] = false;
58
                    continue;
59
                }
60
                $this->providersResponse[\get_class($dataProvider)] = true;
61
62
                return $dataProvider->getSubresource($resourceClass, $identifiers, $context, $operationName);
63
            } catch (ResourceClassNotSupportedException $e) {
64
                @trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
65
                $this->providersResponse[\get_class($dataProvider)] = false;
66
                continue;
67
            }
68
        }
69
70
        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...
71
    }
72
}
73