Completed
Pull Request — 2.1 (#1429)
by Grégoire
02:48
created

ChainCollectionDataProvider::getCollection()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
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
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 View Code Duplication
final class ChainCollectionDataProvider implements CollectionDataProviderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
{
25
    private $dataProviders;
26
27
    /**
28
     * @param CollectionDataProviderInterface[] $dataProviders
29
     */
30
    public function __construct(array $dataProviders)
31
    {
32
        $this->dataProviders = $dataProviders;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getCollection(string $resourceClass, string $operationName = null)
39
    {
40
        foreach ($this->dataProviders as $dataProvider) {
41
            try {
42
                if ($dataProvider instanceof RestrictedDataProviderInterface
43
                    && !$dataProvider->supports($resourceClass, $operationName)) {
44
                    continue;
45
                }
46
47
                return $dataProvider->getCollection($resourceClass, $operationName);
48
            } catch (ResourceClassNotSupportedException $e) {
49
                @trigger_error(sprintf('Throwing a "%s" is deprecated in favor of implementing "%s"', get_class($e), RestrictedDataProviderInterface::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
50
                continue;
51
            }
52
        }
53
    }
54
}
55