|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Import\Feed; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
6
|
|
|
use TreeHouse\Feeder\Transport\FileTransport; |
|
7
|
|
|
use TreeHouse\Feeder\Transport\FtpTransport; |
|
8
|
|
|
use TreeHouse\Feeder\Transport\HttpTransport; |
|
9
|
|
|
use TreeHouse\Feeder\Transport\TransportInterface; |
|
10
|
|
|
|
|
11
|
|
|
class TransportFactory |
|
12
|
|
|
{ |
|
13
|
|
|
const TYPE_FILE = 'file'; |
|
14
|
|
|
const TYPE_HTTP = 'http'; |
|
15
|
|
|
const TYPE_FTP = 'ftp'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param array $config |
|
19
|
|
|
* |
|
20
|
|
|
* @throws \LogicException |
|
21
|
|
|
* |
|
22
|
|
|
* @return TransportInterface |
|
23
|
|
|
*/ |
|
24
|
4 |
|
public static function createTransportFromConfig(array $config) |
|
25
|
|
|
{ |
|
26
|
4 |
|
$config = new ParameterBag($config); |
|
27
|
|
|
|
|
28
|
4 |
|
switch ($config->get('type')) { |
|
29
|
4 |
|
case self::TYPE_HTTP: |
|
30
|
|
|
$transport = static::createTransportFromUrl( |
|
31
|
|
|
$config->get('url'), |
|
32
|
|
|
$config->get('user'), |
|
33
|
|
|
$config->get('pass') |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
return $transport; |
|
37
|
4 |
|
case self::TYPE_FTP: |
|
38
|
|
|
return FtpTransport::create( |
|
39
|
|
|
$config->get('host'), |
|
40
|
|
|
$config->get('user'), |
|
41
|
|
|
$config->get('pass'), |
|
42
|
|
|
$config->get('file'), |
|
43
|
|
|
[ |
|
44
|
|
|
'mode' => $config->get('mode', 'binary'), |
|
45
|
|
|
'pasv' => $config->get('pasv', true), |
|
46
|
|
|
] |
|
47
|
|
|
); |
|
48
|
4 |
|
case self::TYPE_FILE: |
|
49
|
4 |
|
return static::createTransportFromFile($config->get('file')); |
|
50
|
|
|
default: |
|
51
|
|
|
throw new \LogicException(sprintf('Unsupported transport type "%s"', $config->get('type'))); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $file |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \RuntimeException When the file does not exist |
|
59
|
|
|
* |
|
60
|
|
|
* @return FileTransport |
|
61
|
|
|
*/ |
|
62
|
4 |
|
public static function createTransportFromFile($file) |
|
63
|
|
|
{ |
|
64
|
4 |
|
if (false === file_exists($file)) { |
|
65
|
|
|
throw new \RuntimeException(sprintf('File "%s" does not exist', $file)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
return FileTransport::create($file); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $url |
|
73
|
|
|
* @param string $user |
|
74
|
|
|
* @param string $pass |
|
75
|
|
|
* |
|
76
|
|
|
* @return HttpTransport |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function createTransportFromUrl($url, $user = null, $pass = null) |
|
79
|
|
|
{ |
|
80
|
|
|
return HttpTransport::create($url, $user, $pass); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param TransportInterface $transport |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \LogicException |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public static function createConfigFromTransport(TransportInterface $transport) |
|
91
|
|
|
{ |
|
92
|
4 |
|
if ($transport instanceof FileTransport) { |
|
93
|
4 |
|
return static::createConfigFromFile($transport->getConnection()['file']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($transport instanceof HttpTransport) { |
|
97
|
|
|
return static::createConfigFromUrl($transport->getUrl(), $transport->getUser(), $transport->getPass()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($transport instanceof FtpTransport) { |
|
101
|
|
|
return [ |
|
102
|
|
|
'type' => self::TYPE_FTP, |
|
103
|
|
|
'host' => $transport->getHost(), |
|
104
|
|
|
'user' => $transport->getUser(), |
|
105
|
|
|
'pass' => $transport->getPass(), |
|
106
|
|
|
'file' => $transport->getConnection()['file'], |
|
107
|
|
|
'mode' => $transport->getMode(), |
|
108
|
|
|
'pasv' => $transport->getPasv(), |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
throw new \LogicException(sprintf('Unable to create config for transport of type "%s"', get_class($transport))); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $file |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
4 |
|
public static function createConfigFromFile($file) |
|
121
|
|
|
{ |
|
122
|
4 |
|
return ['type' => self::TYPE_FILE, 'file' => $file]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $url |
|
127
|
|
|
* @param string $user |
|
128
|
|
|
* @param string $pass |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function createConfigFromUrl($url, $user = null, $pass = null) |
|
133
|
|
|
{ |
|
134
|
|
|
$config = ['type' => self::TYPE_HTTP, 'url' => $url]; |
|
135
|
|
|
|
|
136
|
|
|
if (null !== $user) { |
|
137
|
|
|
$config['user'] = $user; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (null !== $pass) { |
|
141
|
|
|
$config['pass'] = $pass; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $config; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|