Passed
Push — master ( 9e896a...990cec )
by Nicolaas
02:00
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
use SilverStripe\ORM\DB;
8
9
class UserChanged extends PruningTemplatesTemplate
10
{
11
    protected $keepVersions = 3;
12
13
    public function setKeepVersions(int $keepVersions): self
14
    {
15
        $this->keepVersions = $keepVersions;
16
17
        return $this;
18
    }
19
20
    public function getTitle(): string
21
    {
22
        return 'Prune automated saves';
23
    }
24
25
    public function getDescription(): string
26
    {
27
        return 'Delete versions that are not edited by a logged-in user.';
28
    }
29
30
    public function run(?bool $verbose = false)
31
    {
32
        $rows = DB::query('SELECT * FROM SiteTree_Versions WHERE AuthorID > 0 AND RecordID = '.$this->object->ID);
0 ignored issues
show
Unused Code introduced by
The assignment to $rows is dead and can be removed.
Loading history...
33
        $this->markOlderItemsWithoutAuthor();
34
    }
35
36
    /**
37
     * these can be deleted.
38
     *
39
     * @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...
40
     */
41
    protected function markOlderItemsWithoutAuthor()
42
    {
43
44
        $filter['"AuthorID" = ?'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$filter was never initialized. Although not strictly required by PHP, it is generally a good practice to add $filter = array(); before regardless.
Loading history...
45
        $query = $this->getBaseQuery(['AuthorID'])
46
            ->addWhere($this->normaliseWhere($filter))
47
            ->setLimit($this->normaliseLimit(), $this->normaliseOffset($this->keepVersions))
48
        ;
49
        $this->toDelete[$this->getUniqueKey()] += $this->addVersionNumberToArray(
50
            $this->toDelete[$this->getUniqueKey()],
51
            $query->execute()
52
        );
53
    }
54
55
}
56