OnlyLastOnes   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setkeepCount() 0 5 1
A run() 0 16 2
A getDescription() 0 3 1
A getTitle() 0 3 1
1
<?php
2
3
namespace Sunnysideup\VersionPruner\PruningTemplates;
4
5
use Sunnysideup\VersionPruner\PruningTemplatesTemplate;
6
7
class OnlyLastOnes extends PruningTemplatesTemplate
8
{
9
    private $keepCount = 3;
10
11
    public function getTitle(): string
12
    {
13
        return 'Prune all the last few';
14
    }
15
16
    public function getDescription(): string
17
    {
18
        return 'Keep ' . $this->keepCount . ' records and delete all other.';
19
    }
20
21
    public function setkeepCount(int $keepCount): self
22
    {
23
        $this->keepCount = $keepCount;
24
25
        return $this;
26
    }
27
28
    public function run(?bool $verbose = false)
29
    {
30
        // remove drafts keeping `keep_drafts`
31
        if ($this->keepCount > 0) {
32
            $query = $this->getBaseQuery(['WasPublished'])
33
                ->addWhere(
34
                    [
35
                        'RecordID = ' . $this->object->ID,
36
                    ]
37
                )
38
                ->setLimit($this->normaliseLimit(), $this->normaliseOffset($this->keepCount))
39
            ;
40
41
            $this->toDelete[$this->getUniqueKey()] += $this->addVersionNumberToArray(
42
                $this->toDelete[$this->getUniqueKey()],
43
                $query->execute()
44
            );
45
        }
46
    }
47
}
48