-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTask.php
More file actions
94 lines (74 loc) · 2.31 KB
/
Task.php
File metadata and controls
94 lines (74 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace ConvertApi;
class Task
{
const DEFAULT_URL_FORMAT = 'url';
function __construct($fromFormat, $toFormat, $params, $conversionTimeout = null)
{
$this->fromFormat = $fromFormat;
$this->toFormat = $toFormat;
$this->params = $params;
$this->conversionTimeout = $conversionTimeout ?: ConvertApi::$conversionTimeout;
}
function run()
{
$params = array_replace(
[
'StoreFile' => true,
],
$this->normalizedParams()
);
if ($this->conversionTimeout) {
$params['Timeout'] = $this->conversionTimeout;
$readTimeout = $this->conversionTimeout + ConvertApi::$conversionTimeoutDelta;
} else {
$readTimeout = ConvertApi::$readTimeout;
}
$fromFormat = $this->fromFormat ?: $this->detectFormat($params);
$converter = isset($params['converter']) ? "/converter/{$params['converter']}" : '';
$path = 'convert/' . $fromFormat . '/to/' . $this->toFormat . $converter;
$response = ConvertApi::client()->post($path, $params, $readTimeout);
return new Result($response);
}
private function normalizedParams()
{
$result = [];
foreach ($this->params as $key => $val)
{
switch($key) {
case 'File':
$result[$key] = FileParam::build($val);
break;
case 'Files':
$result[$key] = $this->filesBatch($val);
break;
default:
$result[$key] = $val;
}
}
return $result;
}
private function filesBatch($values)
{
$files = [];
foreach ((array)$values as $val)
$files[] = FileParam::build($val);
return $files;
}
private function detectFormat($params)
{
if (!empty($params['Url']))
return self::DEFAULT_URL_FORMAT;
if (!empty($params['File']))
{
$resource = $params['File'];
}
elseif (!empty($params['Files']))
{
$files = (array)$params['Files'];
$resource = $files[0];
}
$detector = new FormatDetector($resource);
return $detector->run();
}
}