Completed
Push — 2.x ( 0e1f1b...77cf8b )
by Christian
41s queued 16s
created

ContainerDecoderProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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