UserChanged   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 3 1
A markOlderItemsWithoutAuthor() 0 10 1
A getDescription() 0 3 1
A setKeepVersions() 0 5 1
A run() 0 4 1
1
<?php
2
3
namespace Sunnysideup\VersionPruner\PruningTemplates;
4
5
use SilverStripe\ORM\DB;
6
use Sunnysideup\VersionPruner\PruningTemplatesTemplate;
7
8
class UserChanged extends PruningTemplatesTemplate
9
{
10
    protected $keepVersions = 3;
11
12
    public function setKeepVersions(int $keepVersions): self
13
    {
14
        $this->keepVersions = $keepVersions;
15
16
        return $this;
17
    }
18
19
    public function getTitle(): string
20
    {
21
        return 'Prune automated saves';
22
    }
23
24
    public function getDescription(): string
25
    {
26
        return 'Delete versions that are not edited by a logged-in user.';
27
    }
28
29
    public function run(?bool $verbose = false)
30
    {
31
        DB::query('SELECT * FROM SiteTree_Versions WHERE AuthorID > 0 AND RecordID = ' . $this->object->ID);
32
        $this->markOlderItemsWithoutAuthor();
33
    }
34
35
    /**
36
     * these can be deleted.
37
     */
38
    protected function markOlderItemsWithoutAuthor()
39
    {
40
        $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...
41
        $query = $this->getBaseQuery(['AuthorID'])
42
            ->addWhere($this->normaliseWhere($filter))
43
            ->setLimit($this->normaliseLimit(), $this->normaliseOffset($this->keepVersions))
44
        ;
45
        $this->toDelete[$this->getUniqueKey()] += $this->addVersionNumberToArray(
46
            $this->toDelete[$this->getUniqueKey()],
47
            $query->execute()
48
        );
49
    }
50
}
51