Passed
Push — master ( d96a4c...76b07c )
by Carlos C
05:39
created

XlsxToCsvFolderConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 84%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 23
c 1
b 0
f 0
dl 0
loc 43
ccs 21
cts 25
cp 0.84
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A convert() 0 23 5
A xlsx2csvPath() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Converters;
6
7
use PhpCfdi\SatCatalogosPopulate\Utils\ShellExec;
8
use PhpCfdi\SatCatalogosPopulate\Utils\WhichTrait;
9
use RuntimeException;
10
11
class XlsxToCsvFolderConverter
12
{
13 1
    use WhichTrait;
14
15
    /** @var string Location of xlsx2csvPath executable */
16
    private $xlsx2csvPath;
17
18 4
    public function __construct(string $xlsx2csvPath = '')
19
    {
20 4
        if ('' === $xlsx2csvPath) {
21 4
            $xlsx2csvPath = $this->which('xlsx2csv');
22
        }
23 4
        $this->xlsx2csvPath = $xlsx2csvPath;
24 4
    }
25
26 4
    public function xlsx2csvPath(): string
27
    {
28 4
        return $this->xlsx2csvPath;
29
    }
30
31 4
    public function convert(string $source, string $destination): void
32
    {
33 4
        if ('' === $destination) {
34
            throw new RuntimeException('Destination is empty');
35
        }
36 4
        if (! is_dir($destination) || ! is_writable($destination)) {
37
            throw new RuntimeException("Destination directory $destination is not a directory or is not writable");
38
        }
39
40 4
        $command = escapeshellarg($this->xlsx2csvPath()) . ' ' . implode(' ', array_map('escapeshellarg', [
41 4
            '--ignoreempty',
42 4
            '--escape',
43 4
            '--all',
44 4
            '--dateformat',
45 4
            '%Y-%m-%d',
46 4
            $source,
47 4
            $destination,
48
        ]));
49
50 4
        $execution = ShellExec::run($command);
51 4
        if (0 !== $execution->exitStatus()) {
52
            throw new RuntimeException(
53
                "Execution of xlsx2csv convertion return a non zero status code [{$execution->exitStatus()}]"
54
            );
55
        }
56 4
    }
57
}
58