Passed
Pull Request — master (#1)
by Marcin
01:42
created

Api   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 79
ccs 28
cts 36
cp 0.7778
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B transcode() 0 44 6
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\InvalidFileArgumentException;
13
use Mrcnpdlk\Api\Unoconv\Exception\UnoconvException;
14
use SplFileObject;
15
16
/**
17
 * Class Api
18
 */
19
class Api
20
{
21
    /**
22
     * @var array
23
     */
24
    private $params = [];
25
26
    /**
27
     * Api constructor.
28
     *
29
     * @param \Mrcnpdlk\Api\Unoconv\Config $oConfig
30
     *
31
     * @throws \Mrcnpdlk\Api\Unoconv\Exception
32
     */
33 3
    public function __construct(Config $oConfig = null)
34
    {
35 3
        $oConfig                    = $oConfig ?? new Config();
36 3
        $this->params['connection'] = $oConfig->getConnectionString();
37 3
        $this->params['timeout']    = $oConfig->getTimeout();
38 3
        $this->params['docType']    = $oConfig->getDocType();
39 3
        $this->params['format']     = $oConfig->getFormat();
40 3
    }
41
42
    /**
43
     * @param string          $from   Path to input file
44
     * @param FormatType|null $format Default PDF
45
     * @param string|null     $to     Path to output file or directory
46
     *
47
     * @throws \Mrcnpdlk\Api\Unoconv\Exception
48
     * @throws \Mrcnpdlk\Api\Unoconv\Exception\InvalidFileArgumentException
49
     * @throws \Mrcnpdlk\Api\Unoconv\Exception\UnoconvException
50
     *
51
     * @return SplFileObject
52
     */
53 2
    public function transcode(string $from, FormatType $format = null, string $to = null): SplFileObject
54
    {
55 2
        $from = realpath($from);
56
57 2
        if (!is_file($from)) {
58 1
            throw new InvalidFileArgumentException(sprintf('Input file "%s" not exists', $from));
59
        }
60 1
        if (!is_readable($from)) {
61
            throw new InvalidFileArgumentException(sprintf('Input file "%s" is not readable', $from));
62
        }
63
64 1
        $format       = $format ?? $this->params['format'];
65 1
        $fromPathInfo = pathinfo($from);
66
67 1
        if (null === $to) {
68 1
            $to = sprintf('%s%s%s.%s',
69 1
                $fromPathInfo['dirname'],
70 1
                DIRECTORY_SEPARATOR,
71 1
                $fromPathInfo['filename'],
72 1
                $format->getExtension()
73
            );
74
        } elseif (is_dir($to)) {
75
            $to = sprintf('%s%s%s.%s',
76
                $to,
77
                DIRECTORY_SEPARATOR,
78
                $fromPathInfo['filename'],
79
                $format->getExtension()
80
            );
81
        }
82
83 1
        $command = new Command($this->params['connection']);
84
        $command
85 1
            ->addArg('--doctype', $this->params['docType'], false)
86 1
            ->addArg('--format', $format, false)
87 1
            ->addArg('--timeout', $this->params['timeout'], false)
88 1
            ->addArg('--output', $to)
89 1
            ->addArg($from)
90
        ;
91
92 1
        if ($command->execute()) {
93
            return new SplFileObject($to);
94
        }
95 1
        throw new UnoconvException(sprintf('Unoconv error: %s', $command->getError()), $command->getExitCode());
96
    }
97
}
98