Completed
Pull Request — master (#578)
by Kévin
04:02
created

ChainCollectionDataProvider::getCollection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
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 Kévin Dunglas <[email protected]>
20
 */
21
class ChainCollectionDataProvider implements CollectionDataProviderInterface
22
{
23
    private $collectionDataProviders;
24
25
    /**
26
     * @param CollectionDataProviderInterface[] $collectionDataProviders
27
     */
28
    public function __construct(array $collectionDataProviders)
29
    {
30
        $this->collectionDataProviders = $collectionDataProviders;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getCollection(string $resourceClass, string $operationName = null)
37
    {
38
        foreach ($this->collectionDataProviders as $collectionDataProvider) {
39
            try {
40
                return $collectionDataProvider->getCollection($resourceClass, $operationName);
41
            } catch (ResourceClassNotSupportedException $e) {
42
                continue;
43
            }
44
        }
45
    }
46
}
47