Completed
Push — master ( 933a2a...d1c0c8 )
by Lukas Kahwe
05:29
created

ContainerDecoderProvider::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Decoder;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
16
/**
17
 * Provides encoders through the Symfony2 DIC.
18
 *
19
 * @author Igor Wiedler <[email protected]>
20
 */
21
class ContainerDecoderProvider implements DecoderProviderInterface
22
{
23
    private $container;
24
    private $decoders;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param ContainerInterface $container The container from which the actual decoders are retrieved
30
     * @param array              $decoders  List of key (format) value (service ids) of decoders
31
     */
32 30
    public function __construct(ContainerInterface $container, array $decoders)
33
    {
34 30
        $this->container = $container;
35 30
        $this->decoders = $decoders;
36 30
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 10
    public function supports($format)
42
    {
43 10
        return isset($this->decoders[$format]);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 7
    public function getDecoder($format)
50
    {
51 7
        if (!$this->supports($format)) {
52
            throw new \InvalidArgumentException(
53
                sprintf("Format '%s' is not supported by ContainerDecoderProvider.", $format)
54
            );
55
        }
56
57 7
        return $this->container->get($this->decoders[$format]);
58
    }
59
}
60