Completed
Push — 2.0 ( e69d85...2b1775 )
by Kévin
19s
created

ChainItemDataProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getItem() 0 12 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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\DataProvider;
15
16
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
17
18
/**
19
 * Tries each configured data provider and returns the result of the first able to handle the resource class.
20
 *
21
 * @author Kévin Dunglas <[email protected]>
22
 */
23
final class ChainItemDataProvider implements ItemDataProviderInterface
24
{
25
    private $dataProviders;
26
27
    /**
28
     * @param ItemDataProviderInterface[] $dataProviders
29
     */
30
    public function __construct(array $dataProviders)
31
    {
32
        $this->dataProviders = $dataProviders;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
39
    {
40
        foreach ($this->dataProviders as $dataProviders) {
41
            try {
42
                return $dataProviders->getItem($resourceClass, $id, $operationName, $context);
43
            } catch (ResourceClassNotSupportedException $e) {
44
                continue;
45
            }
46
        }
47
48
        return null;
49
    }
50
}
51