Completed
Push — master ( 281eb8...04fe01 )
by Sebastian
10:51
created

Factory::createSync()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.4285
ccs 6
cts 6
cp 1
cc 2
eloc 6
nc 2
nop 2
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\Log\Logger;
10
use phpbu\App\Runner\Task;
11
12
/**
13
 * Source 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       http://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
            'env'    => '\\phpbu\\App\\Adapter\\Env',
35
            'dotenv' => '\\phpbu\\App\\Adapter\\DotEnv',
36
        ],
37
        'runner' => [
38
            'bootstrap' => '\\phpbu\\App\\Runner\\Bootstrap',
39
            'check'     => '\\phpbu\\App\\Runner\\Check',
40
            'cleaner'   => '\\phpbu\\App\\Runner\\Cleaner',
41
            'crypter'   => '\\phpbu\\App\\Runner\\Crypter',
42
            'source'    => '\\phpbu\\App\\Runner\\Source',
43
            'sync'      => '\\phpbu\\App\\Runner\\Sync',
44
        ],
45
        'logger'  => [
46
            'json' => '\\phpbu\\App\\Log\\Json',
47
            'mail' => '\\phpbu\\App\\Log\\Mail',
48
        ],
49
        'source'  => [
50
            'arangodump'  => '\\phpbu\\App\\Backup\\Source\\Arangodump',
51
            'elasticdump' => '\\phpbu\\App\\Backup\\Source\\Elasticdump',
52
            'mongodump'   => '\\phpbu\\App\\Backup\\Source\\Mongodump',
53
            'mysqldump'   => '\\phpbu\\App\\Backup\\Source\\Mysqldump',
54
            'pgdump'      => '\\phpbu\\App\\Backup\\Source\\Pgdump',
55
            'redis'       => '\\phpbu\\App\\Backup\\Source\\Redis',
56
            'rsync'       => '\\phpbu\\App\\Backup\\Source\\Rsync',
57
            'tar'         => '\\phpbu\\App\\Backup\\Source\\Tar',
58
            'xtrabackup'  => '\\phpbu\\App\\Backup\\Source\\XtraBackup',
59
        ],
60
        'check'   => [
61
            'sizemin'                 => '\\phpbu\\App\\Backup\\Check\\SizeMin',
62
            'sizediffpreviouspercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffPreviousPercent',
63
            'sizediffavgpercent'      => '\\phpbu\\App\\Backup\\Check\\SizeDiffAvgPercent',
64
        ],
65
        'crypter'   => [
66
            'mcrypt'  => '\\phpbu\\App\\Backup\\Crypter\\Mcrypt',
67
            'openssl' => '\\phpbu\\App\\Backup\\Crypter\\OpenSSL',
68
        ],
69
        'sync'    => [
70
            'amazons3'    => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3',
71
            'amazons3-v3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3',
72
            'amazons3-v2' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v2',
73
            'dropbox'     => '\\phpbu\\App\\Backup\\Sync\\Dropbox',
74
            'ftp'         => '\\phpbu\\App\\Backup\\Sync\\Ftp',
75
            'rsync'       => '\\phpbu\\App\\Backup\\Sync\\Rsync',
76
            'sftp'        => '\\phpbu\\App\\Backup\\Sync\\Sftp',
77
            'softlayer'   => '\\phpbu\\App\\Backup\\Sync\\SoftLayer',
78
        ],
79
        'cleaner' => [
80
            'capacity'  => '\\phpbu\\App\\Backup\\Cleaner\\Capacity',
81
            'outdated'  => '\\phpbu\\App\\Backup\\Cleaner\\Outdated',
82
            'quantity'  => '\\phpbu\\App\\Backup\\Cleaner\\Quantity',
83
        ],
84 17
    ];
85
86 17
    /**
87 17
     * Backup Factory.
88 17
     * Creates 'Source', 'Check', 'Crypter', 'Sync' and 'Cleaner' Objects.
89 17
     *
90 1
     * @param  string $type
91
     * @param  string $alias
92 16
     * @throws \phpbu\App\Exception
93 16
     * @return mixed
94
     */
95 View Code Duplication
    protected function create($type, $alias)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        self::checkType($type);
98
        $alias = strtolower($alias);
99
100
        if (!isset(self::$classMap[$type][$alias])) {
101
            throw new Exception(sprintf('unknown %s: %s', $type, $alias));
102
        }
103 1
        $class = self::$classMap[$type][$alias];
104
        return new $class();
105 1
    }
106
107
    /**
108
     * Runner Factory.
109
     *
110
     * @param  string $alias
111
     * @param  bool   $isSimulation
112
     * @return mixed
113
     * @throws \phpbu\App\Exception
114
     */
115
    public function createRunner($alias, $isSimulation)
116 3
    {
117
        $runner = $this->create('runner', $alias);
118
        if (!($runner instanceof Task)) {
119 3
            throw new Exception(sprintf('Runner \'%s\' has to implement the \'Task\' interface', $alias));
120 3
        }
121 1
        $runner->setSimulation($isSimulation);
122
        return $runner;
123 2
    }
124 1
125
    /**
126 1
     * Adapter Factory.
127 1
     *
128
     * @param  string $alias
129
     * @param  array  $conf
130
     * @throws \phpbu\App\Exception
131
     * @return \phpbu\App\Adapter
132
     */
133 View Code Duplication
    public function createAdapter($alias, $conf = [])
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
    {
135
        /** @var \phpbu\App\Adapter $adapter */
136
        $adapter = $this->create('adapter', $alias);
137
        if (!($adapter instanceof Adapter)) {
138 2
            throw new Exception(sprintf('adapter \'%s\' has to implement the \'Adapter\' interfaces', $alias));
139
        }
140
        $adapter->setup($conf);
141 2
        return $adapter;
142 2
    }
143 1
144
    /**
145 1
     * Logger Factory.
146 1
     *
147
     * @param  string $alias
148
     * @param  array  $conf
149
     * @throws \phpbu\App\Exception
150
     * @return \phpbu\App\Log\Logger
151
     */
152
    public function createLogger($alias, $conf = [])
153
    {
154
        /** @var \phpbu\App\Log\Logger $logger */
155
        $logger = $this->create('logger', $alias);
156 3
        if (!($logger instanceof Logger)) {
157
            throw new Exception(sprintf('logger \'%s\' has to implement the \'Logger\' interfaces', $alias));
158
        }
159 3
        if (!($logger instanceof Listener)) {
160 2
            throw new Exception(sprintf('logger \'%s\' has to implement the \'Listener\' interface', $alias));
161 1
        }
162
        $logger->setup($conf);
163 1
        return $logger;
164
    }
165
166
    /**
167
     * Source Factory.
168
     *
169
     * @param  string $alias
170
     * @param  array  $conf
171
     * @throws \phpbu\App\Exception
172
     * @return \phpbu\App\Backup\Source
173
     */
174 2 View Code Duplication
    public function createSource($alias, $conf = [])
175
    {
176
        /** @var \phpbu\App\Backup\Source $source */
177 2
        $source = $this->create('source', $alias);
178 2
        if (!($source instanceof Source)) {
179 1
            throw new Exception(sprintf('source \'%s\' has to implement the \'Source\' interface', $alias));
180
        }
181 1
        $source->setup($conf);
182 1
        return $source;
183
    }
184
185
    /**
186
     * Check Factory.
187
     *
188
     * @param  string $alias
189
     * @throws \phpbu\App\Exception
190
     * @return \phpbu\App\Backup\Check
191
     */
192
    public function createCheck($alias)
193 2
    {
194
        /** @var \phpbu\App\Backup\Check $check */
195
        $check = $this->create('check', $alias);
196 2
        if (!($check instanceof Check)) {
197 2
            throw new Exception(sprintf('Check \'%s\' has to implement the \'Check\' interface', $alias));
198 1
        }
199
        return $check;
200 1
    }
201 1
202
    /**
203
     * Crypter Factory.
204
     *
205
     * @param  string $alias
206
     * @param  array  $conf
207
     * @throws \phpbu\App\Exception
208
     * @return \phpbu\App\Backup\Crypter
209
     */
210 View Code Duplication
    public function createCrypter($alias, $conf = [])
211
    {
212 2
        /** @var \phpbu\App\Backup\Crypter $crypter */
213
        $crypter = $this->create('crypter', $alias);
214
        if (!($crypter instanceof Crypter)) {
215 2
            throw new Exception(sprintf('Crypter \'%s\' has to implement the \'Crypter\' interface', $alias));
216 2
        }
217 1
        $crypter->setup($conf);
218
        return $crypter;
219 1
    }
220 1
221
    /**
222
     * Sync Factory.
223
     *
224
     * @param  string $alias
225
     * @param  array  $conf
226
     * @throws \phpbu\App\Exception
227
     * @return \phpbu\App\Backup\Sync
228
     */
229 View Code Duplication
    public function createSync($alias, $conf = [])
230
    {
231
        /** @var \phpbu\App\Backup\Sync $sync */
232 13
        $sync = $this->create('sync', $alias);
233
        if (!($sync instanceof Sync)) {
234 13
            throw new Exception(sprintf('sync \'%s\' has to implement the \'Sync\' interface', $alias));
235 13
        }
236 13
        $sync->setup($conf);
237 12
        return $sync;
238 1
    }
239
240 11
    /**
241 11
     * Cleaner Factory.
242
     *
243
     * @param  string $alias
244
     * @param  array  $conf
245
     * @throws \phpbu\App\Exception
246
     * @return \phpbu\App\Backup\Cleaner
247
     */
248 View Code Duplication
    public function createCleaner($alias, $conf = [])
249 19
    {
250
        /** @var \phpbu\App\Backup\Cleaner $cleaner */
251 19
        $cleaner = $this->create('cleaner', $alias);
252 1
        if (!($cleaner instanceof Cleaner)) {
253
            throw new Exception(sprintf('cleaner \'%s\' has to implement the \'Cleaner\' interface', $alias));
254 18
        }
255
        $cleaner->setup($conf);
256
        return $cleaner;
257
    }
258
259
    /**
260
     * Extend the backup factory.
261
     *
262
     * @param  string  $type        Type to create 'adapter', 'source', 'check', 'sync' or 'cleaner'
263
     * @param  string  $alias       Name the class is registered at
264
     * @param  string  $fqcn        Full Qualified Class Name
265
     * @param  boolean $force       Overwrite already registered class
266
     * @throws \phpbu\App\Exception
267
     */
268 View Code Duplication
    public static function register($type, $alias, $fqcn, $force = false)
269
    {
270
        self::checkType($type);
271
        $alias = strtolower($alias);
272
273
        if (!$force && isset(self::$classMap[$type][$alias])) {
274
            throw new Exception(sprintf('%s is already registered use force parameter to overwrite', $type));
275
        }
276
        self::$classMap[$type][$alias] = $fqcn;
277
    }
278
279
    /**
280
     * Throws an exception if type is invalid.
281
     *
282
     * @param  string $type
283
     * @throws \phpbu\App\Exception
284
     */
285
    private static function checkType($type)
286
    {
287
        if (!isset(self::$classMap[$type])) {
288
            throw new Exception(
289
                'invalid type, please use only \'' . implode('\', \'', array_keys(self::$classMap)) . '\''
290
            );
291
        }
292
    }
293
}
294