PackedBlobReader::commandPaths()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 0
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\RfcLinc\Updater;
6
7
use PhpCfdi\RfcLinc\Util\CommandReader;
8
use PhpCfdi\RfcLinc\Util\ReaderInterface;
9
use PhpCfdi\RfcLinc\Util\ShellWhich;
10
11
class PackedBlobReader implements ReaderInterface
12
{
13
    /** @var CommandReader */
14
    private $reader;
15
16
    /** @var string[] */
17
    private $commands;
18
19 2
    public function __construct()
20
    {
21 2
        $this->reader = new CommandReader();
22 2
        $this->commands = $this->commandPaths();
23 2
    }
24
25 2
    public function commandPaths(): array
26
    {
27 2
        $which = new ShellWhich();
28
        $commands = [
29 2
            'gunzip' => $which('gunzip'),
30 2
            'openssl' => $which('openssl'),
31 2
            'iconv' => $which('iconv'),
32 2
            'sed' => $which('sed'),
33
        ];
34 2
        foreach ($commands as $command => $path) {
35 2
            if ('' === $path) {
36 2
                throw new \InvalidArgumentException("Cannot find $command, it is required to update");
37
            }
38
        }
39 2
        return $commands;
40
    }
41
42 2
    private function createCommandString($filename): string
43
    {
44 2
        $command = implode(' | ', [
45 2
            $this->commands['gunzip'] . ' --stdout ' . escapeshellarg($filename),
46 2
            $this->commands['openssl'] . ' smime -verify -inform der -noverify 2> /dev/null',
47 2
            $this->commands['iconv'] . ' --from iso8859-1 --to utf-8',
48 2
            $this->commands['sed'] . ' ' . escapeshellarg('s/\r$//'),
49
        ]);
50 2
        return $command;
51
    }
52
53 2
    public function open(string $source)
54
    {
55 2
        $command = $this->createCommandString($source);
56 2
        $this->reader = new CommandReader();
57 2
        $this->reader->open($command);
58 2
    }
59
60 2
    public function readLine()
61
    {
62 2
        return $this->reader->readLine();
63
    }
64
65 2
    public function close()
66
    {
67 2
        $this->reader->close();
68 2
    }
69
70 1
    public function isOpen(): bool
71
    {
72 1
        return $this->reader->isOpen();
73
    }
74
}
75