Completed
Pull Request — 2.x (#2138)
by Christian
03:15
created

ChainVersionResolver::resolve()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.0702

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 8
cp 0.875
rs 9.1111
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 6.0702
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\Version;
13
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * @author Ener-Getick <[email protected]>
18
 *
19
 * @final since 2.8
20
 */
21
class ChainVersionResolver implements VersionResolverInterface
22
{
23
    private $resolvers = [];
24
25
    /**
26
     * @var VersionResolverInterface[]
27
     */
28 8
    public function __construct(array $resolvers)
29
    {
30 8
        foreach ($resolvers as $resolver) {
31
            $this->addResolver($resolver);
32
        }
33 8
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 8
    public function resolve(Request $request)
39
    {
40 8
        foreach ($this->resolvers as $resolver) {
41 8
            $version = $resolver->resolve($request);
42
43 8
            if (null !== $version && !is_string($version)) {
44
                @trigger_error(sprintf('Not returning a string or null from %s::resolve() when implementing the %s is deprecated since FOSRestBundle 2.8.', get_class($resolver), VersionResolverInterface::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...
45
            }
46
47 8
            if (null !== $version && false !== $version) {
48 4
                return $version;
49
            }
50
        }
51
52 4
        return null;
53
    }
54
55 8
    public function addResolver(VersionResolverInterface $resolver)
56
    {
57 8
        $this->resolvers[] = $resolver;
58 8
    }
59
}
60