ZipOutputParser::parseFileListing()   B
last analyzed

Complexity

Conditions 7
Paths 11

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 11
nop 1
dl 0
loc 49
rs 8.5546
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Compressy.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Gocobachi\Compressy\Parser;
12
13
/**
14
 * This class is responsible of parsing GNUTar command line output
15
 */
16
class ZipOutputParser implements ParserInterface
17
{
18
    const LENGTH        = '(\d*)';
19
    const ISO_DATE      = '([0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+)';
20
    const FILENAME      = '(.*)';
21
22
    /**
23
     * @var string
24
     */
25
    private $dateFormat;
26
27
    /**
28
     * @param string $dateFormat
29
     */
30
    public function __construct($dateFormat = "Y-m-d H:i")
31
    {
32
        $this->dateFormat = $dateFormat;
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function parseFileListing($output)
39
    {
40
        $lines = array_values(array_filter(explode("\n", $output)));
41
        $members = array();
42
43
        foreach ($lines as $line) {
44
            $matches = array();
45
46
            // 785  2012-10-24 10:39  file
47
            if (!preg_match_all("#" .
48
                self::LENGTH . "\s+" . // match (785)
49
                self::ISO_DATE . "\s+" . // match (2012-10-24 10:39)
50
                self::FILENAME . // match (file)
51
                "#",
52
                $line, $matches, PREG_SET_ORDER
53
            )) {
54
                continue;
55
            }
56
57
            $chunks = array_shift($matches);
58
59
            if (4 !== count($chunks)) {
60
                continue;
61
            }
62
63
            $mtime = \DateTime::createFromFormat($this->dateFormat, $chunks[2]);
64
65
            if ($mtime === false) {
66
                // See https://github.com/alchemy-fr/Zippy/issues/111#issuecomment-251668427
67
                $mtime = \DateTime::createFromFormat('H:i Y-m-d', $chunks[2]);
68
            }
69
70
            if ($mtime === false) {
71
                $mtime = \DateTime::createFromFormat('m-d-Y H:i', $chunks[2]);
72
            }
73
74
            if ($mtime === false) {
75
                $mtime = new \DateTime($chunks[2]);
76
            }
77
78
            $members[] = array(
79
                'location'  => $chunks[3],
80
                'size'      => $chunks[1],
81
                'mtime'     => $mtime,
82
                'is_dir'    => '/' === substr($chunks[3], -1)
83
            );
84
        }
85
86
        return $members;
87
    }
88
89
        /**
90
         * @inheritdoc
91
         */
92
    public function parseInflatorVersion($output)
93
    {
94
        $lines = array_values(array_filter(explode("\n", $output, 3)));
95
96
        $chunks = explode(' ', $lines[1], 3);
97
98
        if (2 > count($chunks)) {
99
            return null;
100
        }
101
102
        list(, $version) = $chunks;
103
104
        return $version;
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function parseDeflatorVersion($output)
111
    {
112
        $lines = array_values(array_filter(explode("\n", $output, 2)));
113
        $firstLine = array_shift($lines);
114
        $chunks = explode(' ', $firstLine, 3);
115
116
        if (2 > count($chunks)) {
117
            return null;
118
        }
119
120
        list(, $version) = $chunks;
121
122
        return $version;
123
    }
124
}
125