BackupAbstract::setBackupFilesystemInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Cornford\Backup;
4
5
use Cornford\Backup\Contracts\BackupEngineInterface;
6
use Cornford\Backup\Contracts\BackupFilesystemInterface;
7
use Cornford\Backup\Contracts\BackupInterface;
8
use Cornford\Backup\Exceptions\BackupArgumentException;
9
10
abstract class BackupAbstract implements BackupInterface
11
{
12
    /**
13
     * Backup engine instance.
14
     *
15
     * @var \Cornford\Backup\Contracts\BackupEngineInterface
16
     */
17
    protected static $backupEngineInstance;
18
19
    /**
20
     * Backup filesystem instance.
21
     *
22
     * @var \Cornford\Backup\Contracts\BackupFilesystemInterface
23
     */
24
    protected $backupFilesystemInstance;
25
26
    /**
27
     * Backup options.
28
     *
29
     * @var array
30
     */
31
    protected $options;
32
33
    /**
34
     * Backup enabled.
35
     *
36
     * @var bool
37
     */
38
    protected $enabled;
39
40
    /**
41
     * Backup path.
42
     *
43
     * @var string
44
     */
45
    protected $path;
46
47
    /**
48
     * Backup compression.
49
     *
50
     * @var bool
51
     */
52
    protected $compress;
53
54
    /**
55
     * Backup filename.
56
     *
57
     * @var string
58
     */
59
    protected $filename;
60
61
    /**
62
     * Backup working filepath.
63
     *
64
     * @var string
65
     */
66
    protected $workingFilepath;
67
68
    /**
69
     * Construct Backup object.
70
     *
71
     * @param BackupEngineInterface     $backupEngineInterface
72
     * @param BackupFilesystemInterface $backupFilesystemInterface
73
     * @param array                     $options
74
     *
75
     * @throws BackupArgumentException
76
     */
77
    public function __construct(
78
        BackupEngineInterface $backupEngineInterface,
79
        BackupFilesystemInterface $backupFilesystemInterface,
80
        array $options = []
81
    ) {
82
        $this->setBackupEngineInstance($backupEngineInterface);
83
        $this->setBackupFilesystemInstance($backupFilesystemInterface);
84
        $this->setOptions($options);
85
86
        if (!isset($options['enabled'])) {
87
            throw new BackupArgumentException('Database enabled setting is required.');
88
        }
89
90
        if (!isset($options['path'])) {
91
            throw new BackupArgumentException('Database backup path is required.');
92
        }
93
94
        if (!isset($options['filename'])) {
95
            throw new BackupArgumentException('Database backup filename is required.');
96
        }
97
98
        if (!isset($options['compress'])) {
99
            throw new BackupArgumentException('Database compression setting is required.');
100
        }
101
102
        if (!isset($options['processors'])) {
103
            throw new BackupArgumentException('Database engine processor settings are required.');
104
        }
105
106
        $this->setEnabled($this->options['enabled']);
107
        $this->setPath($this->options['path']);
108
        $this->setFilename($this->options['filename']);
109
        $this->setCompress($this->options['compress']);
110
111
        $exportCommand = $this->backupFilesystemInstance
112
            ->locateCommand(self::$backupEngineInstance->getExportProcess());
113
114
        $backupInstance =  $this->getBackupEngineInstance();
115
116 View Code Duplication
        if ($exportCommand === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
117
            $exportCommand = isset($options['processors'][$this->getBackupEngineName()]['export']) ?
118
                $options['processors'][$this->getBackupEngineName()]['export'] . $backupInstance->getExportProcess() :
119
                null;
120
        }
121
122
        $restoreCommand = $this->backupFilesystemInstance
123
            ->locateCommand(self::$backupEngineInstance->getRestoreProcess());
124
125 View Code Duplication
        if ($restoreCommand === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
126
            $restoreCommand = isset($options['processors'][$this->getBackupEngineName()]['restore']) ?
127
                $options['processors'][$this->getBackupEngineName()]['restore'] . $backupInstance->getRestoreProcess() :
128
                null;
129
        }
130
131
        self::$backupEngineInstance->setExportCommand($exportCommand);
0 ignored issues
show
Bug introduced by
It seems like $exportCommand can also be of type false or null; however, Cornford\Backup\Contract...ace::setExportCommand() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
132
        self::$backupEngineInstance->setRestoreCommand($restoreCommand);
0 ignored issues
show
Bug introduced by
It seems like $restoreCommand can also be of type false or null; however, Cornford\Backup\Contract...ce::setRestoreCommand() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
133
    }
134
135
    /**
136
     * Set the backup engine instance.
137
     *
138
     * @param BackupEngineInterface $value
139
     *
140
     * @return void
141
     */
142
    public function setBackupEngineInstance(BackupEngineInterface $value): void
143
    {
144
        self::$backupEngineInstance = $value;
145
    }
146
147
    /**
148
     * Get the backup engine instance.
149
     *
150
     * @return BackupEngineInterface
151
     */
152
    public function getBackupEngineInstance(): BackupEngineInterface
153
    {
154
        return self::$backupEngineInstance;
155
    }
156
157
    /**
158
     * Set the backup filesystem instance.
159
     *
160
     * @param BackupFilesystemInterface $value
161
     *
162
     * @return void
163
     */
164
    public function setBackupFilesystemInstance(BackupFilesystemInterface $value): void
165
    {
166
        $this->backupFilesystemInstance = $value;
167
    }
168
169
    /**
170
     * Get the backup filesystem instance.
171
     *
172
     * @return BackupFilesystemInterface
173
     */
174
    public function getBackupFilesystemInstance(): BackupFilesystemInterface
175
    {
176
        return $this->backupFilesystemInstance;
177
    }
178
179
    /**
180
     * Get the backup engine name.
181
     *
182
     * @return string
183
     */
184
    private function getBackupEngineName(): string
185
    {
186
        $instance = self::$backupEngineInstance;
187
188
        return $instance::ENGINE_NAME;
189
    }
190
191
    /**
192
     * Set the backup options.
193
     *
194
     * @param array $value
195
     *
196
     * @return void
197
     */
198
    public function setOptions(array $value = []): void
199
    {
200
        $this->options = $value;
201
    }
202
203
    /**
204
     * Get the backup options.
205
     *
206
     * @return array
207
     */
208
    public function getOptions(): array
209
    {
210
        return $this->options;
211
    }
212
213
    /**
214
     * Set the backup enabled.
215
     *
216
     * @param bool $value
217
     *
218
     * @return void
219
     */
220
    public function setEnabled($value): void
221
    {
222
        $this->enabled = $value;
223
    }
224
225
    /**
226
     * Get the backup enabled.
227
     *
228
     * @return bool
229
     */
230
    public function getEnabled(): bool
231
    {
232
        return $this->enabled;
233
    }
234
235
    /**
236
     * Set the backup path.
237
     *
238
     * @param string $value
239
     *
240
     * @return void
241
     */
242
    public function setPath($value): void
243
    {
244
        $this->path = $value;
245
    }
246
247
    /**
248
     * Get the backup path.
249
     *
250
     * @return string
251
     */
252
    public function getPath(): string
253
    {
254
        $path = getcwd() . DIRECTORY_SEPARATOR . $this->path;
255
256
        if (substr($this->path, 0, 1) == DIRECTORY_SEPARATOR || substr($this->path, 1, 1) == ':') {
257
            $path = $this->path;
258
        }
259
260
        return realpath($path);
261
    }
262
263
    /**
264
     * Set the backup compression state.
265
     *
266
     * @param bool $value
267
     *
268
     * @return void
269
     */
270
    public function setCompress($value): void
271
    {
272
        $this->compress = $value;
273
    }
274
275
    /**
276
     * Get the backup compression state.
277
     *
278
     * @return bool
279
     */
280
    public function getCompress(): bool
281
    {
282
        return $this->compress;
283
    }
284
285
    /**
286
     * Set the backup filename.
287
     *
288
     * @param string $value
289
     *
290
     * @return void
291
     */
292
    public function setFilename($value): void
293
    {
294
        $this->filename = $value;
295
    }
296
297
    /**
298
     * Get the backup filename.
299
     *
300
     * @return string
301
     */
302
    public function getFilename(): string
303
    {
304
        return $this->filename;
305
    }
306
307
    /**
308
     * Set the filepath we are currently working with.
309
     *
310
     * @param string $filepath
311
     *
312
     * @return void
313
     */
314
    protected function setWorkingFilepath($filepath): void
315
    {
316
        $this->workingFilepath = $filepath;
317
    }
318
319
    /**
320
     * Get the filepath we are currently working with.
321
     *
322
     * @return string
323
     */
324
    public function getWorkingFilepath(): string
325
    {
326
        return $this->workingFilepath;
327
    }
328
329
    /**
330
     * Export database to file.
331
     *
332
     * @param string $filename
333
     *
334
     * @return bool
335
     */
336
    abstract public function export($filename = null): bool;
337
338
    /**
339
     * Restore database from file path.
340
     *
341
     * @param string $filepath
342
     *
343
     * @return bool
344
     */
345
    abstract public function restore($filepath): bool;
346
347
    /**
348
     * Get database restoration files.
349
     *
350
     * @param string $path
351
     *
352
     * @return array
353
     */
354
    abstract public function getRestorationFiles($path = null): array;
355
356
    /**
357
     * Get database process output.
358
     *
359
     * @return string
360
     */
361
    abstract public function getProcessOutput(): string;
362
}
363