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

Api::transcode()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7.9936

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 52
ccs 24
cts 33
cp 0.7272
rs 8.4586
c 0
b 0
f 0
cc 7
nc 14
nop 4
crap 7.9936

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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