ContainerDecoderProvider::getDecoder()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
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 class ContainerDecoderProvider implements DecoderProviderInterface
22
{
23
    private $container;
24
    private $decoders;
25
26
    /**
27
     * @param array<string,string> $decoders List of key (format) value (service ids) of decoders
28
     */
29 12
    public function __construct(ContainerInterface $container, array $decoders)
30
    {
31 12
        $this->container = $container;
32 12
        $this->decoders = $decoders;
33 12
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 8
    public function supports(string $format): bool
39
    {
40 8
        return isset($this->decoders[$format]);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 7
    public function getDecoder(string $format): DecoderInterface
47
    {
48 7
        if (!$this->supports($format)) {
49
            throw new \InvalidArgumentException(sprintf("Format '%s' is not supported by ContainerDecoderProvider.", $format));
50
        }
51
52 7
        return $this->container->get($this->decoders[$format]);
53
    }
54
}
55