DeleteFiles::getTitle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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