1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Marcin. |
4
|
|
|
* Date: 03.03.2019 |
5
|
|
|
* Time: 14:22 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Mrcnpdlk\Api\Unoconv; |
9
|
|
|
|
10
|
|
|
use mikehaertl\shellcommand\Command; |
11
|
|
|
use Mrcnpdlk\Api\Unoconv\Enum\FormatType; |
12
|
|
|
use Mrcnpdlk\Api\Unoconv\Exception\DomainException; |
13
|
|
|
use Mrcnpdlk\Api\Unoconv\Exception\InvalidFileArgumentException; |
14
|
|
|
use Mrcnpdlk\Api\Unoconv\Exception\UnoconvException; |
15
|
|
|
use SplFileObject; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Api |
19
|
|
|
*/ |
20
|
|
|
class Api |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $params = []; |
26
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
27
|
|
|
/** |
28
|
|
|
* @var \Psr\Log\LoggerInterface |
29
|
|
|
*/ |
30
|
|
|
private $logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Api constructor. |
34
|
|
|
* |
35
|
|
|
* @param \Mrcnpdlk\Api\Unoconv\Config $oConfig |
36
|
|
|
* |
37
|
|
|
* @throws \Mrcnpdlk\Api\Unoconv\Exception |
38
|
|
|
*/ |
39
|
5 |
|
public function __construct(Config $oConfig = null) |
40
|
|
|
{ |
41
|
5 |
|
$oConfig = $oConfig ?? new Config(); |
42
|
5 |
|
$this->logger = $oConfig->getLogger(); |
43
|
5 |
|
$this->params['connection'] = $oConfig->getConnectionString(); |
44
|
5 |
|
$this->params['timeout'] = $oConfig->getTimeout(); |
45
|
5 |
|
$this->params['docType'] = $oConfig->getDocType(); |
46
|
5 |
|
$this->params['format'] = $oConfig->getFormat(); |
47
|
5 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $sourceFile Path to input file |
51
|
|
|
* @param FormatType|null $format Default PDF |
52
|
|
|
* @param string|null $destination Path to output file or directory |
53
|
|
|
* @param array $exportOpts Export options |
54
|
|
|
* |
55
|
|
|
* @throws \Mrcnpdlk\Api\Unoconv\Exception\DomainException |
56
|
|
|
* @throws \Mrcnpdlk\Api\Unoconv\Exception\InvalidFileArgumentException |
57
|
|
|
* @throws \Mrcnpdlk\Api\Unoconv\Exception\UnoconvException |
58
|
|
|
* |
59
|
|
|
* @return SplFileObject |
60
|
|
|
*/ |
61
|
4 |
|
public function transcode(string $sourceFile, ?FormatType $format, ?string $destination, array $exportOpts = []): SplFileObject |
62
|
|
|
{ |
63
|
4 |
|
$sourceFile = realpath($sourceFile); |
64
|
|
|
|
65
|
4 |
|
if (!is_file($sourceFile)) { |
66
|
1 |
|
throw new InvalidFileArgumentException(sprintf('Input file "%s" not exists', $sourceFile)); |
67
|
|
|
} |
68
|
3 |
|
if (!is_readable($sourceFile)) { |
69
|
|
|
throw new InvalidFileArgumentException(sprintf('Input file "%s" is not readable', $sourceFile)); |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
$format = $format ?? $this->params['format']; |
73
|
3 |
|
$fromPathInfo = pathinfo($sourceFile); |
74
|
|
|
|
75
|
3 |
|
if (null === $destination) { |
76
|
1 |
|
$destination = sprintf('%s%s%s.%s', |
77
|
1 |
|
$fromPathInfo['dirname'], |
78
|
1 |
|
DIRECTORY_SEPARATOR, |
79
|
1 |
|
$fromPathInfo['filename'], |
80
|
1 |
|
$format->getExtension() |
81
|
|
|
); |
82
|
2 |
|
} elseif (is_dir($destination)) { |
83
|
2 |
|
$destination = sprintf('%s%s%s.%s', |
84
|
2 |
|
$destination, |
85
|
2 |
|
DIRECTORY_SEPARATOR, |
86
|
2 |
|
$fromPathInfo['filename'], |
87
|
2 |
|
$format->getExtension() |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
$this->logger->debug(sprintf('Creating "%s" from "%s"', $destination, $sourceFile)); |
92
|
|
|
|
93
|
3 |
|
$command = new Command($this->params['connection']); |
94
|
|
|
$command |
95
|
3 |
|
->addArg('--doctype', $this->params['docType'], false) |
96
|
3 |
|
->addArg('--format', $format, false) |
97
|
3 |
|
->addArg('--timeout', $this->params['timeout'], false) |
98
|
3 |
|
->addArg('--output', $destination, false) |
99
|
|
|
; |
100
|
|
|
|
101
|
3 |
|
foreach ($exportOpts as $key => $value) { |
102
|
2 |
|
if (is_bool($value)) { |
103
|
1 |
|
$value = $value ? 'true' : 'false'; |
104
|
2 |
|
} elseif (is_string($value)) { |
105
|
1 |
|
$value = sprintf('"%s"', $value); |
106
|
1 |
|
} elseif (!is_int($value)) { |
107
|
1 |
|
throw new DomainException( |
108
|
1 |
|
sprintf('Invalid type of export argument "%s", only %s are allowed.', |
109
|
1 |
|
gettype($value), |
110
|
1 |
|
implode(',', ['int', 'string', 'bool'])) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
$command->addArg('--export', sprintf('%s=%s', $key, $value), false); |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
$command->addArg($sourceFile); |
118
|
|
|
|
119
|
2 |
|
$this->logger->debug(sprintf('Executing command: %s', $command->getExecCommand())); |
|
|
|
|
120
|
|
|
|
121
|
2 |
|
if ($command->execute()) { |
122
|
|
|
return new SplFileObject($destination); |
123
|
|
|
} |
124
|
2 |
|
throw new UnoconvException(sprintf('Unoconv error: %s', $command->getError()), $command->getExitCode()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|