DecoderProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 48
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 4 1
A getDecoder() 0 10 2
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