Completed
Push — master ( c5e677...4df15a )
by Sebastian
02:56
created

Factory::createCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
ccs 5
cts 5
cp 1
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
namespace phpbu\App;
3
4
use phpbu\App\Backup\Check;
5
use phpbu\App\Backup\Cleaner;
6
use phpbu\App\Backup\Crypter;
7
use phpbu\App\Backup\Source;
8
use phpbu\App\Backup\Sync;
9
use phpbu\App\Backup\Target;
10
use phpbu\App\Log\Logger;
11
12
/**
13
 * Factory
14
 *
15
 * @package    phpbu
16
 * @subpackage App
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       https://phpbu.de/
21
 * @since      Class available since Release 1.0.0
22
 */
23
class Factory
24
{
25
    /**
26
     * Map of available sources, checks, syncs and cleanups.
27
     *
28
     * @var array
29
     */
30
    private static $classMap = [
31
        // type
32
        //   alias => fqcn
33
        'adapter'  => [
34
            'array'  => '\\phpbu\\App\\Adapter\\PHPArray',
35
            'dotenv' => '\\phpbu\\App\\Adapter\\Dotenv',
36
            'env'    => '\\phpbu\\App\\Adapter\\Env',
37
        ],
38
        'logger'  => [
39
            'json'    => '\\phpbu\\App\\Log\\Json',
40
            'mail'    => '\\phpbu\\App\\Log\\Mail',
41
            'webhook' => '\\phpbu\\App\\Log\\Webhook',
42
        ],
43
        'source'  => [
44
            'arangodump'  => '\\phpbu\\App\\Backup\\Source\\Arangodump',
45
            'elasticdump' => '\\phpbu\\App\\Backup\\Source\\Elasticdump',
46
            'mongodump'   => '\\phpbu\\App\\Backup\\Source\\Mongodump',
47
            'mysqldump'   => '\\phpbu\\App\\Backup\\Source\\Mysqldump',
48
            'pgdump'      => '\\phpbu\\App\\Backup\\Source\\Pgdump',
49
            'redis'       => '\\phpbu\\App\\Backup\\Source\\Redis',
50
            'rsync'       => '\\phpbu\\App\\Backup\\Source\\Rsync',
51
            'tar'         => '\\phpbu\\App\\Backup\\Source\\Tar',
52
            'xtrabackup'  => '\\phpbu\\App\\Backup\\Source\\XtraBackup',
53
        ],
54
        'check'   => [
55
            'sizemin'                 => '\\phpbu\\App\\Backup\\Check\\SizeMin',
56
            'sizediffpreviouspercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffPreviousPercent',
57
            'sizediffavgpercent'      => '\\phpbu\\App\\Backup\\Check\\SizeDiffAvgPercent',
58
        ],
59
        'crypter'   => [
60
            'mcrypt'  => '\\phpbu\\App\\Backup\\Crypter\\Mcrypt',
61
            'openssl' => '\\phpbu\\App\\Backup\\Crypter\\OpenSSL',
62
        ],
63
        'sync'    => [
64
            'amazons3'    => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3',
65
            'amazons3-v3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3',
66
            'amazons3-v2' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v2',
67
            'dropbox'     => '\\phpbu\\App\\Backup\\Sync\\Dropbox',
68
            'ftp'         => '\\phpbu\\App\\Backup\\Sync\\Ftp',
69
            'googledrive' => '\\phpbu\\App\\Backup\\Sync\\GoogleDrive',
70
            'rsync'       => '\\phpbu\\App\\Backup\\Sync\\Rsync',
71
            'sftp'        => '\\phpbu\\App\\Backup\\Sync\\Sftp',
72
            'softlayer'   => '\\phpbu\\App\\Backup\\Sync\\SoftLayer',
73
        ],
74
        'cleaner' => [
75
            'capacity'  => '\\phpbu\\App\\Backup\\Cleaner\\Capacity',
76
            'outdated'  => '\\phpbu\\App\\Backup\\Cleaner\\Outdated',
77
            'stepwise'  => '\\phpbu\\App\\Backup\\Cleaner\\Stepwise',
78
            'quantity'  => '\\phpbu\\App\\Backup\\Cleaner\\Quantity',
79
        ],
80
    ];
81
82
    /**
83
     * Backup Factory.
84
     * Creates 'Source', 'Check', 'Crypter', 'Sync' and 'Cleaner' Objects.
85
     *
86
     * @param  string $type
87
     * @param  string $alias
88
     * @throws \phpbu\App\Exception
89
     * @return mixed
90
     */
91 22 View Code Duplication
    protected function create($type, $alias)
92
    {
93 22
        self::checkType($type);
94 22
        $alias = strtolower($alias);
95
96 22
        if (!isset(self::$classMap[$type][$alias])) {
97 1
            throw new Exception(sprintf('unknown %s: %s', $type, $alias));
98
        }
99 21
        $class = self::$classMap[$type][$alias];
100 21
        return new $class();
101
    }
102
103
    /**
104
     * Adapter Factory.
105
     *
106
     * @param  string $alias
107
     * @param  array  $conf
108
     * @throws \phpbu\App\Exception
109
     * @return \phpbu\App\Adapter
110
     */
111 6
    public function createAdapter($alias, $conf = [])
112
    {
113
        /** @var \phpbu\App\Adapter $adapter */
114 6
        $adapter = $this->create('adapter', $alias);
115 6
        if (!($adapter instanceof Adapter)) {
116 1
            throw new Exception(sprintf('adapter \'%s\' has to implement the \'Adapter\' interfaces', $alias));
117
        }
118 5
        $adapter->setup($conf);
119 5
        return $adapter;
120
    }
121
122
    /**
123
     * Logger Factory.
124
     *
125
     * @param  string $alias
126
     * @param  array  $conf
127
     * @throws \phpbu\App\Exception
128
     * @return \phpbu\App\Listener
129
     */
130 3
    public function createLogger($alias, $conf = [])
131
    {
132
        /** @var \phpbu\App\Log\Logger $logger */
133 3
        $logger = $this->create('logger', $alias);
134 3
        if (!($logger instanceof Logger)) {
135 1
            throw new Exception(sprintf('logger \'%s\' has to implement the \'Logger\' interfaces', $alias));
136
        }
137 2
        if (!($logger instanceof Listener)) {
138 1
            throw new Exception(sprintf('logger \'%s\' has to implement the \'Listener\' interface', $alias));
139
        }
140 1
        $logger->setup($conf);
141 1
        return $logger;
142
    }
143
144
    /**
145
     * Create a backup target.
146
     *
147
     * @param  \phpbu\App\Configuration\Backup\Target $conf
148
     * @return \phpbu\App\Backup\Target
149
     * @throws \phpbu\App\Exception
150
     */
151 1
    public function createTarget(Configuration\Backup\Target $conf)
152
    {
153 1
        $target = new Target($conf->dirname, $conf->filename);
154 1
        $target->setupPath();
155
        // add possible compressor
156 1
        if (!empty($conf->compression)) {
157 1
            $compression = Target\Compression\Factory::create($conf->compression);
158 1
            $target->setCompression($compression);
159
        }
160 1
        return $target;
161
    }
162
163
    /**
164
     * Source Factory.
165
     *
166
     * @param  string $alias
167
     * @param  array  $conf
168
     * @throws \phpbu\App\Exception
169
     * @return \phpbu\App\Backup\Source
170
     */
171 2
    public function createSource($alias, $conf = [])
172
    {
173
        /** @var \phpbu\App\Backup\Source $source */
174 2
        $source = $this->create('source', $alias);
175 2
        if (!($source instanceof Source)) {
176 1
            throw new Exception(sprintf('source \'%s\' has to implement the \'Source\' interface', $alias));
177
        }
178 1
        $source->setup($conf);
179 1
        return $source;
180
    }
181
182
    /**
183
     * Check Factory.
184
     *
185
     * @param  string $alias
186
     * @throws \phpbu\App\Exception
187
     * @return \phpbu\App\Backup\Check
188
     */
189 4
    public function createCheck($alias)
190
    {
191
        /** @var \phpbu\App\Backup\Check $check */
192 4
        $check = $this->create('check', $alias);
193 4
        if (!($check instanceof Check)) {
194 1
            throw new Exception(sprintf('Check \'%s\' has to implement the \'Check\' interface', $alias));
195
        }
196 3
        return $check;
197
    }
198
199
    /**
200
     * Crypter Factory.
201
     *
202
     * @param  string $alias
203
     * @param  array  $conf
204
     * @throws \phpbu\App\Exception
205
     * @return \phpbu\App\Backup\Crypter
206
     */
207 2
    public function createCrypter($alias, $conf = [])
208
    {
209
        /** @var \phpbu\App\Backup\Crypter $crypter */
210 2
        $crypter = $this->create('crypter', $alias);
211 2
        if (!($crypter instanceof Crypter)) {
212 1
            throw new Exception(sprintf('Crypter \'%s\' has to implement the \'Crypter\' interface', $alias));
213
        }
214 1
        $crypter->setup($conf);
215 1
        return $crypter;
216
    }
217
218
    /**
219
     * Sync Factory.
220
     *
221
     * @param  string $alias
222
     * @param  array  $conf
223
     * @throws \phpbu\App\Exception
224
     * @return \phpbu\App\Backup\Sync
225
     */
226 3
    public function createSync($alias, $conf = [])
227
    {
228
        /** @var \phpbu\App\Backup\Sync $sync */
229 3
        $sync = $this->create('sync', $alias);
230 2
        if (!($sync instanceof Sync)) {
231 1
            throw new Exception(sprintf('sync \'%s\' has to implement the \'Sync\' interface', $alias));
232
        }
233 1
        $sync->setup($conf);
234 1
        return $sync;
235
    }
236
237
    /**
238
     * Cleaner Factory.
239
     *
240
     * @param  string $alias
241
     * @param  array  $conf
242
     * @throws \phpbu\App\Exception
243
     * @return \phpbu\App\Backup\Cleaner
244
     */
245 2
    public function createCleaner($alias, $conf = [])
246
    {
247
        /** @var \phpbu\App\Backup\Cleaner $cleaner */
248 2
        $cleaner = $this->create('cleaner', $alias);
249 2
        if (!($cleaner instanceof Cleaner)) {
250 1
            throw new Exception(sprintf('cleaner \'%s\' has to implement the \'Cleaner\' interface', $alias));
251
        }
252 1
        $cleaner->setup($conf);
253 1
        return $cleaner;
254
    }
255
256
    /**
257
     * Extend the backup factory.
258
     *
259
     * @param  string  $type        Type to create 'adapter', 'source', 'check', 'sync' or 'cleaner'
260
     * @param  string  $alias       Name the class is registered at
261
     * @param  string  $fqcn        Full Qualified Class Name
262
     * @param  boolean $force       Overwrite already registered class
263
     * @throws \phpbu\App\Exception
264
     */
265 23 View Code Duplication
    public static function register($type, $alias, $fqcn, $force = false)
266
    {
267 23
        self::checkType($type);
268 21
        $alias = strtolower($alias);
269
270 21
        if (!$force && isset(self::$classMap[$type][$alias])) {
271 1
            throw new Exception(sprintf('%s is already registered use force parameter to overwrite', $type));
272
        }
273 20
        self::$classMap[$type][$alias] = $fqcn;
274 20
    }
275
276
    /**
277
     * Throws an exception if type is invalid.
278
     *
279
     * @param  string $type
280
     * @throws \phpbu\App\Exception
281
     */
282 29
    private static function checkType($type)
283
    {
284 29
        if (!isset(self::$classMap[$type])) {
285 2
            throw new Exception(
286 2
                'invalid type, please use only \'' . implode('\', \'', array_keys(self::$classMap)) . '\''
287
            );
288
        }
289 27
    }
290
}
291