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