Completed
Push — master ( 57c54d...9135e7 )
by Sebastian
03:47
created

Compression::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace phpbu\App\Backup\Target;
3
4
use phpbu\App\Exception;
5
6
/**
7
 * Compression
8
 *
9
 * @package    phpbu
10
 * @subpackage Backup
11
 * @author     Sebastian Feldmann <[email protected]>
12
 * @copyright  Sebastian Feldmann <[email protected]>
13
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
14
 * @link       http://phpbu.de/
15
 * @since      Class available since Release 1.0.0
16
 */
17
class Compression
18
{
19
    /**
20
     * Path to command binary
21
     *
22
     * @var string
23
     */
24
    protected $path;
25
26
    /**
27
     * Command name
28
     *
29
     * @var string
30
     */
31
    protected $cmd;
32
33
    /**
34
     * Suffix for compressed files
35
     *
36
     * @var string
37
     */
38
    protected $suffix;
39
40
    /**
41
     * MIME type for compressed files
42
     *
43
     * @var string
44
     */
45
    protected $mimeType;
46
47
    /**
48
     * List of available compressors
49
     *
50
     * @var array
51
     */
52
    protected static $availableCompressors = [
53
        'gzip' => [
54
            'suffix' => 'gz',
55
            'mime'   => 'application/x-gzip'
56
        ],
57
        'bzip2' => [
58
            'suffix' => 'bz2',
59
            'mime'   => 'application/x-bzip2'
60
        ],
61
        'zip' => [
62
            'suffix' => 'zip',
63
            'mime'   => 'application/zip'
64
        ]
65
    ];
66
67
    /**
68
     * Constructor.
69
     *
70
     * @param string $cmd
71
     * @param string $pathToCmd without trailing slash
72
     */
73
    public function __construct($cmd, $pathToCmd = null)
74
    {
75
        $this->path     = $pathToCmd;
76
        $this->cmd      = $cmd;
77
        $this->suffix   = self::$availableCompressors[$cmd]['suffix'];
78
        $this->mimeType = self::$availableCompressors[$cmd]['mime'];
79
    }
80
81
    /**
82
     * Return the cli command.
83
     *
84
     * @return string
85
     */
86
    public function getCommand()
87
    {
88
        return $this->cmd;
89
    }
90
91
    /**
92
     * Path getter.
93
     *
94
     * @return string
95
     */
96
    public function getPath()
97
    {
98
        return $this->path;
99
    }
100
101
    /**
102
     * Returns the compressor suffix e.g. 'bzip2'
103
     *
104
     * @return string
105
     */
106
    public function getSuffix()
107
    {
108
        return $this->suffix;
109
    }
110
111
    /**
112
     * Returns the compressor mime type.
113
     *
114
     * @return string
115
     */
116
    public function getMimeType()
117
    {
118
        return $this->mimeType;
119
    }
120
121
    /**
122
     * Factory method.
123
     *
124
     * @param  string $name
125
     * @return \phpbu\App\Backup\Compressor
126
     * @throws \phpbu\App\Exception
127
     */
128
    public static function create($name)
129
    {
130
        $path = null;
131
        // check if a path is given for the compressor
132
        if (basename($name) !== $name) {
133
            $path = dirname($name);
134
            $name = basename($name);
135
        }
136
137
        if (!isset(self::$availableCompressors[$name])) {
138
            throw new Exception('invalid compressor:' . $name);
139
        }
140
        return new static($name, $path);
141
    }
142
}
143