DecoderProvider::getDecoder()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the AMFWebServicesClientBundle package.
5
 *
6
 * (c) Amine Fattouch <http://github.com/fattouchsquall>
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 AMF\WebServicesClientBundle\Rest\Decoder;
13
14
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
15
16
/**
17
 * This is the container provider for the decoder.
18
 *
19
 * @author Mohamed Amine Fattouch <[email protected]>
20
 */
21 View Code Duplication
class DecoderProvider extends ContainerAwareTrait implements DecoderProviderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $decoders;
27
28
    /**
29
     * The constructor class.
30
     *
31
     * @param array $decoders List of decoders (default empty).
32
     */
33
    public function __construct(array $decoders = [])
34
    {
35
        $this->decoders = $decoders;
36
    }
37
38
    /**
39
     * @param string $format The format of decoder.
40
     *
41
     * @throws \InvalidArgumentException
42
     *
43
     * @return DecoderInterface
44
     */
45
    public function getDecoder($format)
46
    {
47
        if ($this->supports($format) === false) {
48
            throw new \InvalidArgumentException(sprintf("Format '%s' is not supported.", $format));
49
        }
50
51
        return $this->container->get($this->decoders[$format]);
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
    }
53
54
    /**
55
     * Tests whether a decoder for a given format exists.
56
     *
57
     * @param string $format The format.
58
     *
59
     * @return boolean
60
     */
61
    protected function supports($format)
62
    {
63
        return isset($this->decoders[$format]);
64
    }
65
}
66