Drafts   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 48
rs 10
c 2
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A getTitle() 0 3 1
A setkeepDraftCount() 0 5 1
A setkeepDrafts() 0 5 1
A run() 0 17 2
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