Completed
Push — master ( b1054d...eba5f2 )
by Christian
07:28 queued 11s
created

ContainerDecoderProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 41
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 1
A __construct() 0 9 4
A getDecoder() 0 8 2
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 as PsrContainerInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
/**
18
 * Provides encoders through the Symfony DIC.
19
 *
20
 * @author Igor Wiedler <[email protected]>
21
 *
22
 * @final since 2.8
23
 */
24
class ContainerDecoderProvider implements DecoderProviderInterface
25
{
26
    private $container;
27
    private $decoders;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param PsrContainerInterface $container The container from which the actual decoders are retrieved
33
     * @param array                 $decoders  List of key (format) value (service ids) of decoders
34
     */
35 48
    public function __construct($container, array $decoders)
36
    {
37 48
        if (!$container instanceof PsrContainerInterface && !$container instanceof ContainerInterface) {
38
            throw new \InvalidArgumentException(sprintf('The container must be an instance of %s or %s (%s given).', PsrContainerInterface::class, ContainerInterface::class, is_object($container) ? get_class($container) : gettype($container)));
39
        }
40
41 48
        $this->container = $container;
42 48
        $this->decoders = $decoders;
43 48
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 11
    public function supports($format)
49
    {
50 11
        return isset($this->decoders[$format]);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 8
    public function getDecoder($format)
57
    {
58 8
        if (!$this->supports($format)) {
59
            throw new \InvalidArgumentException(sprintf("Format '%s' is not supported by ContainerDecoderProvider.", $format));
60
        }
61
62 8
        return $this->container->get($this->decoders[$format]);
63
    }
64
}
65