DecoderProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Seblegall\ApiValidatorBundle\Decoder;
4
5
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
6
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
7
8
class DecoderProvider implements ContainerAwareInterface
9
{
10
    use ContainerAwareTrait;
11
12
    private $decoders;
13
14
    /**
15
     * Constructor.
16
     *
17
     * @param array $decoders List of key (format) value (service ids) of decoders
18
     */
19 12
    public function __construct(array $decoders)
20
    {
21 12
        $this->decoders = $decoders;
22 12
    }
23
24
    /**
25
     * Verifies a format is supported.
26
     *
27
     * @param string $format
28
     *
29
     * @return bool
30
     */
31 12
    public function supports($format)
32
    {
33 12
        return isset($this->decoders[$format]);
34
    }
35
36
    /**
37
     * Provides a docder for a supported format.
38
     *
39
     * @param string $format
40
     *
41
     * @return \Seblegall\ApiValidatorBundle\DecoderInterface
42
     *
43
     * @throws \InvalidArgumentException
44
     */
45 3
    public function getDecoder($format)
46
    {
47 3
        if (!$this->supports($format)) {
48 1
            throw new \InvalidArgumentException(
49 1
                sprintf("Format '%s' is not supported by %s.", $format, __CLASS__)
50 1
            );
51
        }
52
53 2
        return $this->container->get($this->decoders[$format]);
54
    }
55
}
56