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\Rotator; |
14
|
|
|
|
15
|
|
|
use RunOpenCode\Backup\Contract\BackupInterface; |
16
|
|
|
use RunOpenCode\Backup\Contract\RotatorInterface; |
17
|
|
|
use RunOpenCode\Backup\Utils\Filesize; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class MinCountMaxSizeRotator |
21
|
|
|
* |
22
|
|
|
* Rotator that nominates old backups when total backup size exceed maximum allowed backup size, but it will keep minimum |
23
|
|
|
* required number of backups. |
24
|
|
|
* |
25
|
|
|
* @package RunOpenCode\Backup\Rotator |
26
|
|
|
*/ |
27
|
|
|
final class MinCountMaxSizeRotator implements RotatorInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var integer |
31
|
|
|
*/ |
32
|
|
|
private $maxSize; |
33
|
|
|
/** |
34
|
|
|
* @var integer |
35
|
|
|
*/ |
36
|
|
|
private $minCount; |
37
|
|
|
|
38
|
4 |
|
public function __construct($minCount, $maxSize) |
39
|
|
|
{ |
40
|
4 |
|
if ($minCount < 1) { |
41
|
|
|
throw new \InvalidArgumentException('At least one file should be set as minimum backup count.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
$this->minCount = $minCount; |
45
|
4 |
|
$this->maxSize = Filesize::getBytes($maxSize); |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
4 |
|
public function nominate(array $backups) |
52
|
|
|
{ |
53
|
4 |
|
$list = array(); |
54
|
4 |
|
$currentSize = 0; |
55
|
|
|
/** |
56
|
|
|
* @var BackupInterface $backup |
57
|
|
|
*/ |
58
|
4 |
|
foreach ($backups as $backup) { |
59
|
4 |
|
$list[$backup->getCreatedAt()->getTimestamp()] = $backup; |
60
|
4 |
|
$currentSize += $backup->getSize(); |
61
|
2 |
|
} |
62
|
4 |
|
if ($currentSize > $this->maxSize) { |
63
|
4 |
|
ksort($list); |
64
|
4 |
|
$nominations = array(); |
65
|
|
|
/** |
66
|
|
|
* @var BackupInterface $backup |
67
|
|
|
*/ |
68
|
4 |
|
foreach ($list as $backup) { |
69
|
4 |
|
if (count($list) - count($nominations) <= $this->minCount) { |
70
|
2 |
|
break; |
71
|
|
|
} |
72
|
4 |
|
$nominations[] = $backup; |
73
|
4 |
|
$currentSize -= $backup->getSize(); |
74
|
4 |
|
if ($currentSize <= $this->maxSize) { |
75
|
3 |
|
break; |
76
|
|
|
} |
77
|
2 |
|
} |
78
|
4 |
|
return $nominations; |
79
|
|
|
} else { |
80
|
|
|
return array(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|