Completed
Push — master ( bc803d...ad38cf )
by Sebastian
04:42
created

Factory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 11 2
A getClassName() 0 8 2
1
<?php
2
namespace phpbu\App\Backup\Target\Compression;
3
4
use phpbu\App\Exception;
5
6
/**
7
 * Factory
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 3.2.1
16
 */
17
class Factory
18
{
19
    /**
20
     * List of available compressors
21
     *
22
     * @var array
23
     */
24
    protected static $availableCompressors = [
25
        'gzip'  => 'Gzip',
26
        'bzip2' => 'Bzip2',
27
        'zip'   => 'Zip'
28
    ];
29
30
    /**
31
     * Create a Compression.
32
     *
33
     * @param  string $name
34
     * @return \phpbu\App\Backup\Target\Compression
35
     */
36
    public static function create($name)
37
    {
38
        $path = null;
39
        // check if a path is given for the compression command
40
        if (basename($name) !== $name) {
41
            $path = dirname($name);
42
            $name = basename($name);
43
        }
44
        $class = self::getClassName($name);
45
        return new $class($path);
46
    }
47
48
    /**
49
     * Return compressions FQCN by name.
50
     *
51
     * @param  string $name
52
     * @return string
53
     * @throws \phpbu\App\Exception
54
     */
55
    public static function getClassName($name)
56
    {
57
        if (!isset(self::$availableCompressors[$name])) {
58
            throw new Exception('Invalid compressor: ' .$name);
59
        }
60
        $class = self::$availableCompressors[$name];
61
        return '\\phpbu\\App\\Backup\\Target\\Compression\\' . $class;
62
    }
63
}
64