PruningTemplatesTemplate   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 35
c 7
b 0
f 0
dl 0
loc 136
rs 10
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A normaliseOffset() 0 7 2
A __construct() 0 5 1
A normaliseLimit() 0 7 2
A addVersionNumberToArray() 0 8 2
A getToDelete() 0 3 1
A normaliseWhere() 0 4 1
A setToDelete() 0 5 1
A getBaseQuery() 0 6 1
A setBaseTable() 0 5 1
A getUniqueKey() 0 3 1
1
<?php
2
3
namespace Sunnysideup\VersionPruner;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\Queries\SQLSelect;
7
8
abstract class PruningTemplatesTemplate
9
{
10
    /**
11
     * @var int
12
     */
13
    private const DEFAULT_ALWAYS_KEEP = 12;
14
15
    /**
16
     * @var int
17
     */
18
    private const DEFAULT_MAX_DELETE_IN_ONE_GO = 1000;
19
20
    /**
21
     * @var string[]
22
     */
23
    private const BASE_FIELDS = [
24
        'ID',
25
        'Version',
26
        'LastEdited',
27
    ];
28
29
    /**
30
     * Versioned DataObject.
31
     *
32
     * @var DataObject
33
     */
34
    protected $object;
35
36
    /**
37
     * the table that contains the fields like Version and ClassName
38
     * does not include the _Version bit.
39
     *
40
     * @var string
41
     */
42
    protected $baseTable = '';
43
44
    /**
45
     * array of items to delete.
46
     *
47
     * @var array
48
     */
49
    protected $toDelete = [];
50
51
    /**
52
     * @param mixed $object
53
     */
54
    public function __construct($object, array $toDelete)
55
    {
56
        $this->object = $object;
57
        $this->toDelete[$this->getUniqueKey()] = $toDelete;
58
        $this->baseTable = $this->object->baseTable();
59
    }
60
61
    /**
62
     * adds / removes records to be deleted.
63
     */
64
    abstract public function run(?bool $verbose = false);
65
66
    abstract public function getTitle(): string;
67
68
    abstract public function getDescription(): string;
69
70
    public function getToDelete(): array
71
    {
72
        return $this->toDelete[$this->getUniqueKey()];
73
    }
74
75
    public function setBaseTable(): self
76
    {
77
        $this->baseTable = $this->object->baseTable();
78
79
        return $this;
80
    }
81
82
    public function setToDelete(array $toDelete): self
83
    {
84
        $this->toDelete[$this->getUniqueKey()] = $toDelete;
85
86
        return $this;
87
    }
88
89
    /**
90
     * we use this unique key to accidentally mix up records.
91
     */
92
    protected function getUniqueKey(): string
93
    {
94
        return $this->object->ClassName . '_' . $this->object->ID;
95
    }
96
97
    /**
98
     * we keep adding to array ...
99
     *
100
     * @param mixed $records
101
     */
102
    protected function addVersionNumberToArray(array $array, $records, ?string $field = 'Version'): array
103
    {
104
        $myArray = [];
105
        foreach ($records as $record) {
106
            $myArray[$record[$field]] = $record[$field];
107
        }
108
109
        return $array + $myArray;
110
    }
111
112
    protected function getBaseQuery(?array $additionalFieldsToSelect = []): SQLSelect
113
    {
114
        return (new SQLSelect())
115
            ->setFrom($this->baseTable . '_Versions') // important, of course!
116
            ->setSelect(array_merge(self::BASE_FIELDS, $additionalFieldsToSelect)) // not sure if we need this.
117
            ->setOrderBy('"ID" DESC') // important - we always work backwards
118
        ;
119
    }
120
121
    protected function normaliseWhere(array $array): array
122
    {
123
        return $array + [
124
            '"RecordID" = ?' => $this->object->ID,
125
        ];
126
    }
127
128
    protected function normaliseLimit(?int $int = self::DEFAULT_MAX_DELETE_IN_ONE_GO): int
129
    {
130
        if ($int > self::DEFAULT_MAX_DELETE_IN_ONE_GO) {
131
            $int = self::DEFAULT_MAX_DELETE_IN_ONE_GO;
132
        }
133
134
        return $int;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $int could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
135
    }
136
137
    protected function normaliseOffset(?int $int = self::DEFAULT_ALWAYS_KEEP): int
138
    {
139
        if ($int < self::DEFAULT_ALWAYS_KEEP) {
140
            $int = self::DEFAULT_ALWAYS_KEEP;
141
        }
142
143
        return $int;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $int could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
144
    }
145
}
146