Passed
Push — master ( f73d2a...b6f14a )
by f
14:52
created

BaseCommand::open()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
namespace wapmorgan\UnifiedArchive\Commands;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use wapmorgan\UnifiedArchive\UnifiedArchive;
7
8
class BaseCommand extends \Symfony\Component\Console\Command\Command
9
{
10
    /**
11
     * @param $file
12
     * @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...
13
     * @return UnifiedArchive
14
     * @throws \Exception
15
     */
16
    protected function open($file, $password = null)
17
    {
18
        if (!UnifiedArchive::canOpen($file))
19
            throw new \Exception('Could not open archive '.$file.'. Try installing suggested packages or run `cam -f` to see formats support.');
20
21
        $archive = UnifiedArchive::open($file, $password);
22
        if ($archive === null)
23
            throw new \Exception('Could not open archive '.$file);
24
25
        return $archive;
26
    }
27
28
    /**
29
     * @param $unixtime
30
     *
31
     * @return string
32
     * @throws \Exception
33
     */
34
    public function formatDate($unixtime)
35
    {
36
        if (strtotime('today') < $unixtime)
37
            return 'Today, '.date('G:m', $unixtime);
38
        else if (strtotime('yesterday') < $unixtime)
39
            return 'Yesterday, '.date('G:m', $unixtime);
40
        else {
41
            $datetime = new \DateTime();
42
            $datetime->setTimestamp($unixtime);
43
            if ($datetime->format('Y') == date('Y'))
44
                return $datetime->format('d M, H:m');
45
            else
46
                return $datetime->format('d M Y, H:m');
47
        }
48
    }
49
50
    /**
51
     * @param $bytes
52
     * @param int $precision
53
     * @return array
54
     */
55
    public function formatSize($bytes, $precision = 2)
56
    {
57
        $units = ['b', 'k', 'm', 'g', 't'];
58
59
        $bytes = max($bytes, 0);
60
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
61
        $pow = min($pow, count($units) - 1);
62
        $bytes /= pow(1024, $pow);
63
        $i = round($bytes, $precision);
64
        if ($precision == 1 && $i >= 10) {
65
            $i = round($i / 1024, 1);
66
            $pow++;
67
        }
68
69
        return [$i, $units[$pow]];
70
    }
71
72
    /**
73
     * @param $stream
74
     * @return false|string
75
     */
76
    protected function getMimeTypeByStream($stream)
77
    {
78
        return @mime_content_type($stream);
79
    }
80
}
81