TransporterFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 61.9%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 83
loc 83
ccs 13
cts 21
cp 0.619
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A addTransporter() 5 5 1
A getTransporters() 4 4 1
A get() 13 13 2
A configuration() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
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 Webcreate\Conveyor\Factory;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Webcreate\Conveyor\Transporter\AbstractTransporter;
16
17 View Code Duplication
class TransporterFactory
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...
18
{
19
    /**
20
     * @var ContainerInterface
21
     */
22
    protected $container;
23
24
    /**
25
     * @var string[]
26
     */
27
    protected $transporters = array();
28
29
    /**
30
     * @var string[]
31
     */
32
    protected $configurations = array();
33
34
    /**
35
     * @param ContainerInterface $container
36
     */
37 3
    public function __construct(ContainerInterface $container)
38
    {
39 3
        $this->container = $container;
40 3
    }
41
42
    /**
43
     * @param string      $serviceId
44
     * @param string      $alias
45
     * @param string|bool $configuration
46
     */
47 3
    public function addTransporter($serviceId, $alias, $configuration = false)
48
    {
49 3
        $this->transporters[$alias]   = $serviceId;
50 3
        $this->configurations[$alias] = $configuration;
51 3
    }
52
53
    /**
54
     * @return string[]
55
     */
56 1
    public function getTransporters()
57
    {
58 1
        return $this->transporters;
59
    }
60
61
    /**
62
     * Returns transporter
63
     *
64
     * @param  string                    $alias   name of transporter
65
     * @param  array                     $options transporter settings
66
     * @throws \InvalidArgumentException
67
     * @return AbstractTransporter
68
     */
69
    public function get($alias, array $options = array())
70
    {
71
        if (!isset($this->transporters[$alias])) {
72
            throw new \InvalidArgumentException(sprintf('Transporter type \'%s\' does not exist', $alias));
73
        }
74
75
        $serviceId = $this->transporters[$alias];
76
77
        $transporter = $this->container->get($serviceId);
78
        $transporter->setOptions($options);
79
80
        return $transporter;
81
    }
82
83
    /**
84
     * Returns transporter configuration
85
     *
86
     * @param  string                                                              $alias transporter name
87
     * @return \Symfony\Component\Config\Definition\ConfigurationInterface|boolean
88
     */
89 1
    public function configuration($alias)
90
    {
91 1
        $configurationClass = $this->configurations[$alias];
92
93 1
        if (false !== $configurationClass) {
94 1
            return new $configurationClass();
95
        }
96
97
        return false;
98
    }
99
}
100