Completed
Push — master ( 1f3ae1...7a77f2 )
by Nikola
02:24
created

MinCountMaxSizeRotator::nominate()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6.0045

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 19
cts 20
cp 0.95
rs 8.439
cc 6
eloc 19
nc 10
nop 1
crap 6.0045
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like \RunOpenCode\Backup\Util...ize::getBytes($maxSize) can also be of type double. However, the property $maxSize is declared as type integer. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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 4
        }
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 2
                    break;
76
                }
77 4
            }
78 4
            return $nominations;
79
        } else {
80
            return array();
81
        }
82
    }
83
}
84