Completed
Pull Request — master (#904)
by Antoine
03:14 queued 18s
created

ChainSubresourceDataProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubresource() 0 10 3
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