Completed
Push — master ( f73309...4d4b19 )
by Antoine
03:25
created

ChainSubresourceDataProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
/*
5
 * This file is part of the API Platform project.
6
 *
7
 * (c) Kévin Dunglas <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace ApiPlatform\Core\DataProvider;
14
15
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
16
17
/**
18
 * Tries each configured data provider and returns the result of the first able to handle the resource class.
19
 *
20
 * @author Antoine Bluchet <[email protected]>
21
 */
22
final class ChainSubresourceDataProvider implements SubresourceDataProviderInterface
23
{
24
    private $dataProviders;
25
26
    /**
27
     * @param SubresourceDataProviderInterface[] $dataProviders
28
     */
29
    public function __construct(array $dataProviders)
30
    {
31
        $this->dataProviders = $dataProviders;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName)
38
    {
39
        foreach ($this->dataProviders as $dataProviders) {
40
            try {
41
                return $dataProviders->getSubresource($resourceClass, $identifiers, $context, $operationName);
42
            } catch (ResourceClassNotSupportedException $e) {
43
                continue;
44
            }
45
        }
46
    }
47
}
48