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

Api   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 49
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A create() 0 16 2
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