1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by Marcin. |
5
|
|
|
* Date: 03.03.2019 |
6
|
|
|
* Time: 14:22 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Mrcnpdlk\Api\Unoconv; |
10
|
|
|
|
11
|
|
|
use Mrcnpdlk\Api\Unoconv\Enum\DocType; |
12
|
|
|
use Mrcnpdlk\Api\Unoconv\Enum\FormatType; |
13
|
|
|
use Mrcnpdlk\Api\Unoconv\Exception\ConfigurationException; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Psr\Log\NullLogger; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Config |
19
|
|
|
*/ |
20
|
|
|
class Config |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $binary = '/usr/bin/unoconv'; |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $host = 'localhost'; |
30
|
|
|
/** |
31
|
|
|
* Port to listen on (as listener) or to connect to (as client). |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $port = 2002; |
36
|
|
|
/** |
37
|
|
|
* Specify the LibreOffice document type of the backend format. Possible document types are: document, graphics, presentation, |
38
|
|
|
* spreadsheet. |
39
|
|
|
* |
40
|
|
|
* @var DocType |
41
|
|
|
*/ |
42
|
|
|
protected $docType; |
43
|
|
|
/** |
44
|
|
|
* Specify the output format for the document. You can get a list of possible output formats per document type by using the --show |
45
|
|
|
* option. |
46
|
|
|
* |
47
|
|
|
* @var FormatType |
48
|
|
|
*/ |
49
|
|
|
protected $format; |
50
|
|
|
/** |
51
|
|
|
* When unoconv starts its own listener, try to connect to it for an amount of seconds before giving up. Increasing this may help when |
52
|
|
|
* you receive random errors caused by the listener not being ready to accept conversion jobs. |
53
|
|
|
* |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
protected $timeout = 30; |
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $options = 'urp;StarOffice.ComponentContext'; |
61
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
62
|
|
|
/** |
63
|
|
|
* @var \Psr\Log\LoggerInterface |
64
|
|
|
*/ |
65
|
|
|
protected $logger; |
66
|
|
|
/** |
67
|
|
|
* Webservice url |
68
|
|
|
* |
69
|
|
|
* @see https://hub.docker.com/r/zrrrzzt/docker-unoconv-webservice |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
protected $webservice = 'http://localhost:3000'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Config constructor. |
77
|
|
|
* Default values; |
78
|
|
|
* |
79
|
|
|
* @param array $config |
80
|
|
|
* |
81
|
|
|
* @throws \Mrcnpdlk\Api\Unoconv\Exception\ConfigurationException |
82
|
|
|
*/ |
83
|
10 |
|
public function __construct(array $config = []) |
84
|
|
|
{ |
85
|
10 |
|
$this->docType = DocType::DOCUMENT(); |
86
|
10 |
|
$this->format = FormatType::PDF(); |
87
|
|
|
/* @noinspection PhpUndefinedClassInspection */ |
88
|
10 |
|
$this->logger = new NullLogger(); |
89
|
|
|
|
90
|
10 |
|
foreach ($config as $key => $value) { |
91
|
5 |
|
$funName = sprintf('set%s', ucfirst($key)); |
92
|
5 |
|
if (method_exists($this, $funName)) { |
93
|
4 |
|
$this->$funName($value); |
94
|
1 |
|
} elseif (property_exists($this, $key)) { |
95
|
|
|
$this->{$key} = $value; |
96
|
|
|
} else { |
97
|
5 |
|
throw new ConfigurationException(sprintf('Property "%s" not defined in Config class "%s"', $key, __CLASS__)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
9 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
6 |
|
public function getConnectionString(): string |
106
|
|
|
{ |
107
|
6 |
|
return sprintf('%s --connection="socket,host=%s,port=%s;%s"', $this->binary, $this->host, $this->port, $this->options); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return DocType |
112
|
|
|
*/ |
113
|
7 |
|
public function getDocType(): DocType |
114
|
|
|
{ |
115
|
7 |
|
return $this->docType; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return FormatType |
120
|
|
|
*/ |
121
|
7 |
|
public function getFormat(): FormatType |
122
|
|
|
{ |
123
|
7 |
|
return $this->format; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return \Psr\Log\LoggerInterface |
130
|
|
|
*/ |
131
|
6 |
|
public function getLogger(): LoggerInterface |
132
|
|
|
{ |
133
|
6 |
|
return $this->logger; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
7 |
|
public function getTimeout(): int |
140
|
|
|
{ |
141
|
7 |
|
return $this->timeout; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
6 |
|
public function getWebservice(): string |
148
|
|
|
{ |
149
|
6 |
|
return $this->webservice; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $binary |
154
|
|
|
* |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
4 |
|
public function setBinary(string $binary): self |
158
|
|
|
{ |
159
|
4 |
|
$this->binary = $binary; |
160
|
|
|
|
161
|
4 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param DocType $docType |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
1 |
|
public function setDocType(DocType $docType): self |
170
|
|
|
{ |
171
|
1 |
|
$this->docType = $docType; |
172
|
|
|
|
173
|
1 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param FormatType $format |
178
|
|
|
* |
179
|
|
|
* @return $this |
180
|
|
|
*/ |
181
|
1 |
|
public function setFormat(FormatType $format): self |
182
|
|
|
{ |
183
|
1 |
|
$this->format = $format; |
184
|
|
|
|
185
|
1 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $host |
190
|
|
|
* |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
1 |
|
public function setHost(string $host): self |
194
|
|
|
{ |
195
|
1 |
|
$this->host = $host; |
196
|
|
|
|
197
|
1 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
204
|
|
|
* |
205
|
|
|
* @return $this |
206
|
|
|
*/ |
207
|
1 |
|
public function setLogger(LoggerInterface $logger): self |
208
|
|
|
{ |
209
|
1 |
|
$this->logger = $logger; |
210
|
|
|
|
211
|
1 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param int $port |
216
|
|
|
* |
217
|
|
|
* @return $this |
218
|
|
|
*/ |
219
|
1 |
|
public function setPort(int $port): self |
220
|
|
|
{ |
221
|
1 |
|
$this->port = $port; |
222
|
|
|
|
223
|
1 |
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param int $timeout |
228
|
|
|
* |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
1 |
|
public function setTimeout(int $timeout): self |
232
|
|
|
{ |
233
|
1 |
|
$this->timeout = $timeout; |
234
|
|
|
|
235
|
1 |
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $webservice |
240
|
|
|
* |
241
|
|
|
* @return Config |
242
|
|
|
*/ |
243
|
|
|
public function setWebservice(string $webservice): Config |
244
|
|
|
{ |
245
|
|
|
$this->webservice = $webservice; |
246
|
|
|
|
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|