DeleteFiles   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasBeenDeleted() 0 8 1
A getTitle() 0 3 1
A getDescription() 0 3 1
A run() 0 10 2
1
<?php
2
3
namespace Sunnysideup\VersionPruner\PruningTemplates;
4
5
use SilverStripe\ORM\Queries\SQLSelect;
6
use Sunnysideup\VersionPruner\PruningTemplatesTemplate;
7
8
class DeleteFiles extends PruningTemplatesTemplate
9
{
10
    public function getTitle(): string
11
    {
12
        return 'File specific pruning';
13
    }
14
15
    public function getDescription(): string
16
    {
17
        return 'All versions are deleted for files that have been deleted.';
18
    }
19
20
    /**
21
     * Prune versions of deleted files/folders.
22
     */
23
    public function run(?bool $verbose = false)
24
    {
25
        if ($this->hasBeenDeleted()) {
26
            $query = $this->getBaseQuery()
27
                ->addWhere(['"RecordID" = ?' => $this->object->ID])
28
            ;
29
            //starting from "keepVersions" - going backwards in time
30
            $this->toDelete[$this->getUniqueKey()] += $this->addVersionNumberToArray(
31
                $this->toDelete[$this->getUniqueKey()],
32
                $query->execute()
33
            );
34
        }
35
    }
36
37
    protected function hasBeenDeleted(): bool
38
    {
39
        return (new SQLSelect())
40
            ->setFrom($this->baseTable . '_Versions')
41
            ->setSelect(['RecordID, WasDeleted'])
42
            ->addWhere($this->normaliseWhere(['"WasDeleted" = ?' => 1]))
43
            ->setLimit(1)
44
            ->count('ID') > 0;
45
    }
46
}
47