Passed
Push — 2.2 ( 3c7322...2f609b )
by Antoine
03:09
created

ChainSubresourceDataProvider::getSubresource()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 9
nop 4
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\DataProvider;
15
16
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
17
18
/**
19
 * Tries each configured data provider and returns the result of the first able to handle the resource class.
20
 *
21
 * @author Antoine Bluchet <[email protected]>
22
 */
23
final class ChainSubresourceDataProvider implements SubresourceDataProviderInterface
24
{
25
    private $dataProviders;
26
27
    /**
28
     * @param SubresourceDataProviderInterface[] $dataProviders
29
     */
30
    public function __construct(array $dataProviders)
31
    {
32
        $this->dataProviders = $dataProviders;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null)
39
    {
40
        foreach ($this->dataProviders as $dataProvider) {
41
            try {
42
                if ($dataProvider instanceof RestrictedDataProviderInterface && !$dataProvider->supports($resourceClass, $operationName, $context)) {
43
                    continue;
44
                }
45
46
                return $dataProvider->getSubresource($resourceClass, $identifiers, $context, $operationName);
47
            } catch (ResourceClassNotSupportedException $e) {
48
                @trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
49
                continue;
50
            }
51
        }
52
53
        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 object|null.
Loading history...
54
    }
55
}
56