OnlyLastOnes::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
rs 10
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