Passed
Push — master ( 4095f5...c69355 )
by Marcin
37s queued 11s
created

Api   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 78.05%

Importance

Changes 0
Metric Value
wmc 8
eloc 42
dl 0
loc 92
ccs 32
cts 41
cp 0.7805
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B transcode() 0 52 7
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
     * @param string[]        $exportOpts  Export options
52
     *
53
     * @throws \Mrcnpdlk\Api\Unoconv\Exception
54
     * @throws \Mrcnpdlk\Api\Unoconv\Exception\InvalidFileArgumentException
55
     * @throws \Mrcnpdlk\Api\Unoconv\Exception\UnoconvException
56
     *
57
     * @return SplFileObject
58
     */
59 2
    public function transcode(string $sourceFile, ?FormatType $format, ?string $destination, array $exportOpts = []): SplFileObject
60
    {
61 2
        $sourceFile = realpath($sourceFile);
62
63 2
        if (!is_file($sourceFile)) {
64 1
            throw new InvalidFileArgumentException(sprintf('Input file "%s" not exists', $sourceFile));
65
        }
66 1
        if (!is_readable($sourceFile)) {
67
            throw new InvalidFileArgumentException(sprintf('Input file "%s" is not readable', $sourceFile));
68
        }
69
70 1
        $format       = $format ?? $this->params['format'];
71 1
        $fromPathInfo = pathinfo($sourceFile);
72
73 1
        if (null === $destination) {
74 1
            $destination = sprintf('%s%s%s.%s',
75 1
                $fromPathInfo['dirname'],
76 1
                DIRECTORY_SEPARATOR,
77 1
                $fromPathInfo['filename'],
78 1
                $format->getExtension()
79
            );
80
        } elseif (is_dir($destination)) {
81
            $destination = sprintf('%s%s%s.%s',
82
                $destination,
83
                DIRECTORY_SEPARATOR,
84
                $fromPathInfo['filename'],
85
                $format->getExtension()
86
            );
87
        }
88
89 1
        $this->logger->debug(sprintf('Creating "%s" from "%s"', $destination, $sourceFile));
90
91 1
        $command = new Command($this->params['connection']);
92
        $command
93 1
            ->addArg('--doctype', $this->params['docType'], false)
94 1
            ->addArg('--format', $format, false)
95 1
            ->addArg('--timeout', $this->params['timeout'], false)
96 1
            ->addArg('--output', $destination, false)
97
        ;
98
99 1
        foreach ($exportOpts as $key => $value) {
100
            $command->addArg('--export', sprintf('%s=%s', $key, $value), false);
101
        }
102
103 1
        $command->addArg($sourceFile);
104
105 1
        $this->logger->debug(sprintf('Executing command: %s', $command->getExecCommand()));
0 ignored issues
show
Bug introduced by
It seems like $command->getExecCommand() can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
        $this->logger->debug(sprintf('Executing command: %s', /** @scrutinizer ignore-type */ $command->getExecCommand()));
Loading history...
106
107 1
        if ($command->execute()) {
108
            return new SplFileObject($destination);
109
        }
110 1
        throw new UnoconvException(sprintf('Unoconv error: %s', $command->getError()), $command->getExitCode());
111
    }
112
}
113