Completed
Pull Request — master (#578)
by Kévin
03:58
created

ChainItemDataProvider::getItem()   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 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 Kévin Dunglas <[email protected]>
20
 */
21
class ChainItemDataProvider implements ItemDataProviderInterface
22
{
23
    private $itemDataProviders;
24
25
    /**
26
     * @param ItemDataProviderInterface[] $itemDataProviders
27
     */
28
    public function __construct(array $itemDataProviders)
29
    {
30
        $this->itemDataProviders = $itemDataProviders;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false)
37
    {
38
        foreach ($this->itemDataProviders as $itemDataProviders) {
39
            try {
40
                return $itemDataProviders->getItem($resourceClass, $id, $operationName, $fetchData);
41
            } catch (ResourceClassNotSupportedException $e) {
42
                continue;
43
            }
44
        }
45
    }
46
}
47