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 |
|
|
|
|
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
|
|
|
|
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.