UnrarCommandDecoder::getFiles()   C
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 6
nop 1
1
<?php
2
3
namespace AlreadyExtract\Utils\ExtractorCommandDecoder;
4
5
use Symfony\Component\Process\Process;
6
7
/**
8
 * Created by PhpStorm.
9
 * User: mhor
10
 * Date: 02/08/17
11
 * Time: 23:32
12
 */
13
class UnrarCommandDecoder implements ExtractorCommandDecoderInterface
14
{
15
    /**
16
     *  {@inheritdoc}
17
     */
18
    public function getFiles($path)
19
    {
20
        $process = new Process(sprintf('unrar l "%s"', $path));
21
        $process->run();
22
        if (!$process->isSuccessful()) {
23
            throw new \RuntimeException($process->getErrorOutput());
24
        }
25
26
        $files = [];
27
        $inFiles = false;
28
        $colSize = null;
29
        foreach (explode(PHP_EOL, trim($process->getOutput())) as $line) {
30
31
            if (trim($line) !== '' && strlen(str_replace([' ', '-'], '', $line)) === 0) {
32
                if ($inFiles === false) {
33
                    $colSize = $this->getColSize($line);
34
                }
35
                $inFiles = !$inFiles;
36
37
                continue;
38
            }
39
40
            if ($inFiles === false) {
41
                continue;
42
            }
43
44
            $files[] = (new File())
45
                ->setUnpackedSize((int) trim((substr($line, $colSize['length'], $colSize['date']))))
46
                ->setPath(trim((substr($line, $colSize['name']))))
47
            ;
48
49
        }
50
51
        return $files;
52
    }
53
54
    /**
55
     * @param $line
56
     * @return array
57
     */
58
    private function getColSize($line)
59
    {
60
61
        $cols = explode(' ',  preg_replace('~\s+~', ' ', $line));
62
        $attributes = strlen($cols[0]) + 1;
63
        $length = strlen($cols[1]) + $attributes + 2;
64
        $date = strlen($cols[2]) + $length + 1;
65
        $time = strlen($cols[3]) + $date + 2;
66
        return [
67
            'attributes' => 0,
68
            'length' => $attributes,
69
            'date' => $length,
70
            'time' => $date,
71
            'name' => $time,
72
        ];
73
    }
74
}