Completed
Pull Request — master (#904)
by Antoine
03:34
created

ChainSubresourceDataProvider::getSubresource()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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
namespace ApiPlatform\Core\DataProvider;
13
14
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
15
16
/**
17
 * Tries each configured data provider and returns the result of the first able to handle the resource class.
18
 *
19
 * @author Antoine Bluchet <[email protected]>
20
 */
21
final class ChainSubresourceDataProvider implements SubresourceDataProviderInterface
22
{
23
    private $dataProviders;
24
25
    /**
26
     * @param SubresourceDataProviderInterface[] $dataProviders
27
     */
28
    public function __construct(array $dataProviders)
29
    {
30
        $this->dataProviders = $dataProviders;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName)
37
    {
38
        foreach ($this->dataProviders as $dataProviders) {
39
            try {
40
                return $dataProviders->getSubresource($resourceClass, $identifiers, $context, $operationName);
41
            } catch (ResourceClassNotSupportedException $e) {
42
                continue;
43
            }
44
        }
45
    }
46
}
47