1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Backup package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2015 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* This project is fork of "kbond/php-backup", for full credits info, please |
11
|
|
|
* view CREDITS file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
namespace RunOpenCode\Backup\Backup; |
14
|
|
|
|
15
|
|
|
use RunOpenCode\Backup\Contract\FileInterface; |
16
|
|
|
use RunOpenCode\Backup\Contract\BackupInterface; |
17
|
|
|
use RunOpenCode\Backup\Exception\Exception; |
18
|
|
|
use RunOpenCode\Backup\Utils\Filename; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Backup. |
22
|
|
|
* |
23
|
|
|
* Backup is abstraction of collection of files for backup. |
24
|
|
|
*/ |
25
|
|
|
final class Backup implements BackupInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var FileInterface[] |
34
|
|
|
*/ |
35
|
|
|
private $files; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
private $size; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \DateTimeInterface |
44
|
|
|
*/ |
45
|
|
|
private $createdAt; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \DateTimeInterface |
49
|
|
|
*/ |
50
|
|
|
private $modifiedAt; |
51
|
|
|
|
52
|
|
|
public function __construct($name, array $files = array(), $size = 0, $createdAt = null, $modifiedAt = null) |
53
|
56 |
|
{ |
54
|
|
|
$this->name = Filename::sanitize($name); |
55
|
56 |
|
$this->size = $size; |
56
|
56 |
|
$this->createdAt = is_null($createdAt) ? new \DateTime('now') : $createdAt; |
57
|
56 |
|
$this->modifiedAt = is_null($modifiedAt) ? new \DateTime('now') : $modifiedAt; |
58
|
56 |
|
|
59
|
|
|
if (count($files)) { |
60
|
56 |
|
$this->setFiles($files); |
61
|
18 |
|
} else { |
62
|
9 |
|
$this->files = array(); |
63
|
38 |
|
} |
64
|
|
|
} |
65
|
56 |
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getName() |
70
|
48 |
|
{ |
71
|
|
|
return $this->name; |
72
|
48 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function setName($name) |
78
|
6 |
|
{ |
79
|
|
|
$this->name = Filename::sanitize($name); |
80
|
6 |
|
|
81
|
|
|
return $this; |
82
|
6 |
|
} |
83
|
|
|
|
84
|
|
|
public function addFiles(array $files) |
85
|
8 |
|
{ |
86
|
|
|
foreach ($files as $file) { |
87
|
8 |
|
$this->addFile($file); |
88
|
6 |
|
} |
89
|
4 |
|
|
90
|
|
|
return $this; |
91
|
8 |
|
} |
92
|
|
|
|
93
|
|
|
public function addFile(FileInterface $file) |
94
|
6 |
|
{ |
95
|
|
|
$this->files[] = $file; |
96
|
6 |
|
$this->size += $file->getSize(); |
97
|
6 |
|
|
98
|
|
|
return $this; |
99
|
6 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getFiles() |
105
|
30 |
|
{ |
106
|
|
|
return $this->files; |
107
|
30 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function setFiles(array $files) |
113
|
24 |
|
{ |
114
|
|
|
$this->files = $files; |
115
|
24 |
|
$this->size = 0; |
116
|
24 |
|
foreach ($this->files as $file) { |
117
|
24 |
|
$this->size += $file->getSize(); |
118
|
22 |
|
} |
119
|
12 |
|
|
120
|
|
|
return $this; |
121
|
24 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function getSize() |
127
|
8 |
|
{ |
128
|
|
|
return $this->size; |
129
|
8 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function setSize($size) |
135
|
|
|
{ |
136
|
|
|
$this->size = $size; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function getCreatedAt() |
145
|
10 |
|
{ |
146
|
|
|
return (!is_null($this->createdAt)) ? clone $this->createdAt : null; |
147
|
10 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public function setCreatedAt($createdAt) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
if (!is_int($createdAt) && !$createdAt instanceof \DateTime) { |
155
|
|
|
throw new \InvalidArgumentException(sprintf('Integer or \DateTime expected, got: "%s".', Exception::typeOf($createdAt))); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$this->createdAt = is_int($createdAt) ? date_timestamp_set(new \DateTime(), $createdAt) : $createdAt; |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function getModifiedAt() |
166
|
|
|
{ |
167
|
|
|
return (!is_null($this->modifiedAt)) ? clone $this->modifiedAt : null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
public function setModifiedAt($modifiedAt) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
if (!is_int($modifiedAt) && !$modifiedAt instanceof \DateTime) { |
176
|
|
|
throw new \InvalidArgumentException(sprintf('Integer or \DateTime expected, got: "%s".', Exception::typeOf($modifiedAt))); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->modifiedAt = is_int($modifiedAt) ? date_timestamp_set(new \DateTime(), $modifiedAt) : $modifiedAt; |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
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.