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 MaxSizeRotator |
21
|
|
|
* |
22
|
|
|
* Rotator that nominates old backups when total backup size exceed maximum allowed backup size. |
23
|
|
|
* |
24
|
|
|
* @package RunOpenCode\Backup\Rotator |
25
|
|
|
*/ |
26
|
|
|
final class MaxSizeRotator implements RotatorInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $maxSize; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string|int $maxSize |
35
|
|
|
*/ |
36
|
4 |
|
public function __construct($maxSize) |
37
|
|
|
{ |
38
|
4 |
|
$this->maxSize = Filesize::getBytes($maxSize); |
|
|
|
|
39
|
4 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
4 |
|
public function nominate(array $backups) |
45
|
|
|
{ |
46
|
4 |
|
$list = array(); |
47
|
4 |
|
$currentSize = 0; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var BackupInterface $backup |
51
|
|
|
*/ |
52
|
4 |
|
foreach ($backups as $backup) { |
53
|
4 |
|
$list[$backup->getCreatedAt()->getTimestamp()] = $backup; |
54
|
4 |
|
$currentSize += $backup->getSize(); |
55
|
4 |
|
} |
56
|
|
|
|
57
|
4 |
|
if ($currentSize > $this->maxSize) { |
58
|
|
|
|
59
|
4 |
|
ksort($list); |
60
|
|
|
|
61
|
4 |
|
$nominations = array(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var BackupInterface $backup |
65
|
|
|
*/ |
66
|
4 |
|
foreach ($list as $backup) { |
67
|
|
|
|
68
|
4 |
|
$nominations[] = $backup; |
69
|
4 |
|
$currentSize -= $backup->getSize(); |
70
|
|
|
|
71
|
4 |
|
if ($currentSize <= $this->maxSize) { |
72
|
4 |
|
break; |
73
|
|
|
} |
74
|
4 |
|
} |
75
|
|
|
|
76
|
4 |
|
return $nominations; |
77
|
|
|
|
78
|
|
|
} else { |
79
|
|
|
return array(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.