Passed
Push — master ( b43a24...1ec6ac )
by Marcin
42s
created

Api   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 78.95%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 86
ccs 30
cts 38
cp 0.7895
rs 10
c 0
b 0
f 0

2 Methods

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