Passed
Push — master ( b97c32...e7e450 )
by Carlos C
10:36 queued 12s
created

XlsxToCsvFolderConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 43
ccs 12
cts 16
cp 0.75
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A xlsx2csvPath() 0 3 1
A convert() 0 23 5
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
    use WhichTrait;
14
15
    /** @var string Location of xlsx2csvPath executable */
16
    private string $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
    }
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
            '--ignoreempty',
42
            '--escape',
43
            '--all',
44
            '--dateformat',
45
            '%Y-%m-%d',
46
            $source,
47
            $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
    }
57
}
58