Passed
Push — master ( 125b29...554a92 )
by f
27:06 queued 12:02
created

BaseCommand::getDriverBaseName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use wapmorgan\UnifiedArchive\Drivers\BasicDriver;
7
use wapmorgan\UnifiedArchive\Formats;
8
use wapmorgan\UnifiedArchive\UnifiedArchive;
9
10
class BaseCommand extends \Symfony\Component\Console\Command\Command
11
{
12
    protected static $abilitiesLabels = [
13
        'open' => BasicDriver::OPEN,
14
        'open (+password)' => BasicDriver::OPEN_ENCRYPTED,
15
        'get comment' => BasicDriver::GET_COMMENT,
16
        'extract' => BasicDriver::EXTRACT_CONTENT,
17
        'stream' => BasicDriver::STREAM_CONTENT,
18
        'append' => BasicDriver::APPEND,
19
        'delete' => BasicDriver::DELETE,
20
        'set comment' => BasicDriver::SET_COMMENT,
21
        'create' => BasicDriver::CREATE,
22
        'create (+password)' => BasicDriver::CREATE_ENCRYPTED,
23
    ];
24
25
    protected static $abilitiesShortCuts = [
26
        BasicDriver::OPEN => 'o',
27
        BasicDriver::OPEN_ENCRYPTED => 'O',
28
        BasicDriver::GET_COMMENT => 't',
29
        BasicDriver::EXTRACT_CONTENT => 'x',
30
        BasicDriver::STREAM_CONTENT => 's',
31
        BasicDriver::APPEND => 'a',
32
        BasicDriver::DELETE => 'd',
33
        BasicDriver::SET_COMMENT => 'T',
34
        BasicDriver::CREATE => 'c',
35
        BasicDriver::CREATE_ENCRYPTED => 'C',
36
    ];
37
38
    /**
39
     * @param $file
40
     * @param null $password
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $password is correct as it would always require null to be passed?
Loading history...
41
     * @return UnifiedArchive
42
     * @throws \Exception
43
     */
44
    protected function open($file, $password = null)
45
    {
46
        if (!UnifiedArchive::canOpen($file))
47
            throw new \Exception('Could not open archive '.$file.'. Try installing suggested packages or run `cam -f` to see formats support.');
48
49
        $archive = UnifiedArchive::open($file, $password);
50
        if ($archive === null)
51
            throw new \Exception('Could not open archive '.$file);
52
53
        return $archive;
54
    }
55
56
    /**
57
     * @param $unixtime
58
     *
59
     * @return string
60
     * @throws \Exception
61
     */
62
    public function formatDate($unixtime)
63
    {
64
        if (strtotime('today') < $unixtime)
65
            return 'Today, '.date('G:m', $unixtime);
66
        else if (strtotime('yesterday') < $unixtime)
67
            return 'Yesterday, '.date('G:m', $unixtime);
68
        else {
69
            $datetime = new \DateTime();
70
            $datetime->setTimestamp($unixtime);
71
            if ($datetime->format('Y') == date('Y'))
72
                return $datetime->format('d M, H:m');
73
            else
74
                return $datetime->format('d M Y, H:m');
75
        }
76
    }
77
78
    /**
79
     * @param $bytes
80
     * @param int $precision
81
     * @return array
82
     */
83
    public function formatSize($bytes, $precision = 2)
84
    {
85
        $units = ['b', 'k', 'm', 'g', 't'];
86
87
        $bytes = max($bytes, 0);
88
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
89
        $pow = min($pow, count($units) - 1);
90
        $bytes /= pow(1024, $pow);
91
        $i = round($bytes, $precision);
92
        if ($precision == 1 && $i >= 10) {
93
            $i = round($i / 1024, 1);
94
            $pow++;
95
        }
96
97
        return [$i, $units[$pow]];
98
    }
99
100
    /**
101
     * @param $stream
102
     * @return false|string
103
     */
104
    protected function getMimeTypeByStream($stream)
105
    {
106
        return @mime_content_type($stream);
107
    }
108
109
    protected function getDriverBaseName($driverClass)
110
    {
111
        return substr($driverClass, strrpos($driverClass, '\\') + 1);
112
    }
113
114
    protected function resolveDriverName($driver)
115
    {
116
        if (strpos($driver, '\\') === false) {
117
            if (class_exists('\\wapmorgan\\UnifiedArchive\\Drivers\\' . $driver)) {
118
                $driver = '\\wapmorgan\\UnifiedArchive\\Drivers\\' . $driver;
119
            } else if (class_exists('\\wapmorgan\\UnifiedArchive\\Drivers\\OneFile\\' . $driver)) {
120
                $driver = '\\wapmorgan\\UnifiedArchive\\Drivers\\OneFile\\' . $driver;
121
            }
122
        }
123
        if ($driver[0] !== '\\') {
124
            $driver = '\\'.$driver;
125
        }
126
        return $driver;
127
    }
128
}
129