MaxCountRotator::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.032
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
18
/**
19
 * Class MaxCountRotator
20
 *
21
 * Rotator that nominates old backups when number of current backups exceed allowed number of backups.
22
 *
23
 * @package RunOpenCode\Backup\Rotator
24
 */
25
final class MaxCountRotator implements RotatorInterface
26
{
27
    /**
28
     * @var integer
29
     */
30
    private $count;
31
32 4
    public function __construct($count)
33
    {
34 4
        if ($count < 1) {
35
            throw new \InvalidArgumentException('You need to allow at least one backup file to be created.');
36
        }
37 4
        $this->count = $count;
38 4
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 4
    public function nominate(array $backups)
44
    {
45 4
        if (($currentCount = count($backups)) > $this->count) {
46
47 4
            $list = array();
48
49
            /**
50
             * @var BackupInterface $backup
51
             */
52 4
            foreach ($backups as $backup) {
53 4
                $list[$backup->getCreatedAt()->getTimestamp()] = $backup;
54 2
            }
55
56 4
            ksort($list);
57
58 4
            $nominations = array();
59
60
            /**
61
             * @var BackupInterface $backup
62
             */
63 4
            foreach ($list as $backup) {
64 4
                $nominations[] = $backup;
65 4
                $currentCount--;
66
67 4
                if ($currentCount <= $this->count) {
68 4
                    break;
69
                }
70 2
            }
71
72 4
            return $nominations;
73
74
        } else {
75
            return array();
76
        }
77
    }
78
}
79