EncoderProvider::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Encoder;
13
14
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
15
16
/**
17
 * This is the container provider for the encoder.
18
 *
19
 * @author Mohamed Amine Fattouch <[email protected]>
20
 */
21 View Code Duplication
class EncoderProvider extends ContainerAwareTrait implements EncoderProviderInterface
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 $encoders;
27
28
    /**
29
     * The constructor class.
30
     *
31
     * @param array $encoders List of encoders (default empty).
32
     */
33
    public function __construct(array $encoders = [])
34
    {
35
        $this->encoders = $encoders;
36
    }
37
38
    /**
39
     * @param string $format The format of encoder.
40
     *
41
     * @throws \InvalidArgumentException
42
     *
43
     * @return EncoderInterface
44
     */
45
    public function getEncoder($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->encoders[$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 encoder 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->encoders[$format]);
64
    }
65
}
66