1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LilyLabs\DBBackup; |
4
|
|
|
|
5
|
|
|
use LilyLabs\DBBackup\Contracts\BackupFile; |
6
|
|
|
use DateTime; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Represents a database backup file generated |
11
|
|
|
* |
12
|
|
|
* @package LilyLabs\DBBackup |
13
|
|
|
* @author Abraham Chávez |
14
|
|
|
**/ |
15
|
|
|
class DBBackupFile implements BackupFile |
16
|
|
|
{ |
17
|
|
|
private $path; |
18
|
|
|
|
19
|
|
|
public function __construct(string $path) |
20
|
|
|
{ |
21
|
|
|
$this->path = $path; |
22
|
|
|
if (!$this->exists()) { |
23
|
|
|
throw new Exception("The specified file don't exists [{$this->path}]"); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function exists() : bool |
28
|
|
|
{ |
29
|
|
|
return file_exists($this->path); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function name() : string |
33
|
|
|
{ |
34
|
|
|
return pathinfo($this->path, PATHINFO_FILENAME); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function basename() : string |
38
|
|
|
{ |
39
|
|
|
return pathinfo($this->path, PATHINFO_BASENAME); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function dirname() : string |
43
|
|
|
{ |
44
|
|
|
return pathinfo($this->path, PATHINFO_DIRNAME); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function extension() : string |
48
|
|
|
{ |
49
|
|
|
return pathinfo($this->path, PATHINFO_EXTENSION); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function path() : string |
53
|
|
|
{ |
54
|
|
|
return $this->path; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function move(string $destination) : BackupFile |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if (!@rename($this->path, $destination)) { |
60
|
|
|
$error = error_get_last(); |
61
|
|
|
$message = strip_tags($error['message']); |
62
|
|
|
throw new Exception("The backup file cannot be moved [{$message}]"); |
63
|
|
|
} |
64
|
|
|
return new DBBackupFile($destination); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function copy(string $destination) : BackupFile |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (!@copy($this->path, $destination)) { |
70
|
|
|
$error = error_get_last(); |
71
|
|
|
$message = strip_tags($error['message']); |
72
|
|
|
throw new Exception("The backup file cannot be copied [{$message}]"); |
73
|
|
|
} |
74
|
|
|
return new DBBackupFile($destination); |
75
|
|
|
} |
76
|
|
|
} // END class BasicBackupFileNameGenerator |
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.