Passed
Branch devel (076198)
by Marcin
04:11
created

Api::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.004
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\UnoconvException;
13
use SplFileObject;
14
15
/**
16
 * Class Api
17
 */
18
class Api
19
{
20
    /**
21
     * @var array
22
     */
23
    private $params = [];
24
25
    /**
26
     * Api constructor.
27
     *
28
     * @param \Mrcnpdlk\Api\Unoconv\Config $oConfig
29
     *
30
     * @throws \Mrcnpdlk\Api\Unoconv\Exception
31
     */
32 2
    public function __construct(Config $oConfig = null)
33
    {
34 2
        $oConfig                    = $oConfig ?? new Config();
35 2
        $this->params['connection'] = $oConfig->getConnectionString();
36 2
        $this->params['timeout']    = $oConfig->getTimeout();
37 2
        $this->params['docType']    = $oConfig->getDocType();
38 2
        $this->params['format']     = $oConfig->getFormat();
39 2
    }
40
41
    /**
42
     * @param string          $from
43
     * @param string          $to
44
     * @param FormatType|null $format
45
     *
46
     * @throws \Mrcnpdlk\Api\Unoconv\Exception\UnoconvException
47
     *
48
     * @return SplFileObject
49
     */
50 1
    public function create(string $from, string $to, FormatType $format = null): SplFileObject
51
    {
52 1
        $command = new Command($this->params['connection']);
53
        $command
54 1
            ->addArg('--doctype', $this->params['docType'], false)
55 1
            ->addArg('--format', $format ?? $this->params['format'], false)
56 1
            ->addArg('--timeout', $this->params['timeout'], false)
57 1
            ->addArg('--output', $to)
58 1
            ->addArg($from)
59
        ;
60
61 1
        if ($command->execute()) {
62
            return new SplFileObject($to);
63
        }
64 1
        throw new UnoconvException(sprintf('Unoconv error: %s', $command->getError()), $command->getExitCode());
65
    }
66
}
67