Passed
Push — master ( 6eec4e...0daa52 )
by Nicolaas
08:19
created

UserChanged::getItemsToKeep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 17
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Sunnysideup\VersionPruner\PruningTemplates;
4
5
use Sunnysideup\VersionPruner\PruningTemplatesTemplate;
6
7
class UserChanged extends PruningTemplatesTemplate
8
{
9
    protected $keepVersions = 10;
10
11
    protected $fieldsWithChangesToKeep = [
12
        'AuthorID',
13
    ];
14
15
    public function setKeepVersions(int $keepVersions): self
16
    {
17
        $this->keepVersions = $keepVersions;
18
19
        return $this;
20
    }
21
22
    public function getTitle(): string
23
    {
24
        return 'Prune automated saves';
25
    }
26
27
    public function getDescription(): string
28
    {
29
        return 'Delete versions that are not edited by a logged-in user.';
30
    }
31
32
    public function run()
33
    {
34
        $this->markOlderItemsWithoutAuthor();
35
    }
36
37
    /**
38
     * these can be deleted.
39
     *
40
     * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
41
     */
42
    protected function markOlderItemsWithoutAuthor()
43
    {
44
45
        foreach ($this->fieldsWithChangesToKeep as $field) {
46
            $filter['"' . $field . '" = ?'] = 0;
47
        }
48
49
        $query = $this->getBaseQuery($this->fieldsWithChangesToKeep)
50
            ->addWhere($this->normaliseWhere($filter))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filter seems to be defined by a foreach iteration on line 45. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
51
            ->setLimit($this->normaliseLimit(), $this->normaliseOffset($this->keepVersions))
52
        ;
53
54
        $this->toDelete[$this->getUniqueKey()] = $this->addVersionNumberToArray(
55
            $this->toDelete[$this->getUniqueKey()],
56
            $query->execute()
57
        );
58
    }
59
60
    protected function getItemsToKeep(): array
61
    {
62
        // Get the most recent Version IDs of all published pages to ensure
63
        // we leave at least X versions even if a URLSegment or ParentID
64
        // has changed.
65
        $query = $this->getBaseQuery([])
66
            ->addWhere(
67
                [
68
                    '"RecordID" = ?' => $this->object->ID,
69
                ]
70
            )
71
            ->setLimit($this->keepVersions, 0)
72
        ;
73
74
        return $this->addVersionNumberToArray(
75
            [],
76
            $query->execute()
77
        );
78
    }
79
}
80