Completed
Push — master ( 2fc4c7...b0bc56 )
by Sebastian
03:38
created

OnePerGroup::keep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
namespace phpbu\App\Backup\Cleaner\Stepwise\Keeper;
3
4
use phpbu\App\Backup\Cleaner\Stepwise\Keeper;
5
use phpbu\App\Backup\File;
6
7
/**
8
 * Keep one backup per date group class
9
 *
10
 * @package    phpbu
11
 * @subpackage Backup
12
 * @author     Sebastian Feldmann <[email protected]>
13
 * @copyright  Sebastian Feldmann <[email protected]>
14
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
15
 * @link       http://phpbu.de/
16
 * @since      Class available since Release 5.0.0
17
 */
18
class OnePerGroup implements Keeper
19
{
20
    /**
21
     * Grouping date format f.e. 'Ymd'.
22
     *
23
     * @var string
24
     */
25
    private $group;
26
27
    /**
28
     * List of groups containing the files.
29
     *
30
     * @var \phpbu\App\Backup\File[]
31
     */
32
    private $groups = [];
33
34
    /**
35
     * OnePerGroup constructor.
36
     *
37
     * @param string $group
38
     */
39
    public function __construct(string $group)
40
    {
41
        $this->group = $group;
42
    }
43
44
    /**
45
     * Decides if given file should be kept.
46
     *
47
     * @param  \phpbu\App\Backup\File $file
48
     * @return bool
49
     */
50
    public function keep(File $file) : bool
51
    {
52
        $group                  = date($this->group, $file->getMTime());
53
        $this->groups[$group][] = $file;
54
55
        // keep only the first file
56
        return count($this->groups[$group]) < 2;
57
    }
58
}
59