Drafts::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 17
rs 9.9666
c 2
b 0
f 0
1
<?php
2
3
namespace Sunnysideup\VersionPruner\PruningTemplates;
4
5
use Sunnysideup\VersionPruner\PruningTemplatesTemplate;
6
7
class Drafts extends PruningTemplatesTemplate
8
{
9
    private $keepDraftCount = 10;
10
11
    public function getTitle(): string
12
    {
13
        return 'Prune drafts';
14
    }
15
16
    public function getDescription(): string
17
    {
18
        return 'Keep ' . $this->keepDraftCount . ' drafts and delete all other drafts.';
19
    }
20
21
    /**
22
     * here for legacy reasons.
23
     */
24
    public function setkeepDrafts(int $keepDraftCount): self
25
    {
26
        $this->keepDraftCount = $keepDraftCount;
27
28
        return $this;
29
    }
30
31
    public function setkeepDraftCount(int $keepDraftCount): self
32
    {
33
        $this->keepDraftCount = $keepDraftCount;
34
35
        return $this;
36
    }
37
38
    public function run(?bool $verbose = false)
39
    {
40
        // remove drafts keeping `keep_drafts`
41
        if ($this->keepDraftCount > 0) {
42
            $query = $this->getBaseQuery(['WasPublished'])
43
                ->addWhere(
44
                    [
45
                        'RecordID = ' . $this->object->ID,
46
                        'WasPublished = 0',
47
                    ]
48
                )
49
                ->setLimit($this->normaliseLimit(), $this->normaliseOffset($this->keepDraftCount))
50
            ;
51
52
            $this->toDelete[$this->getUniqueKey()] += $this->addVersionNumberToArray(
53
                $this->toDelete[$this->getUniqueKey()],
54
                $query->execute()
55
            );
56
        }
57
    }
58
}
59