RemoveUnreferencedCSPDocumentJob   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 31
c 2
b 1
f 0
dl 0
loc 64
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getItemsList() 0 5 1
A process() 0 41 5
A setup() 0 5 1
A getTitle() 0 3 1
1
<?php
2
namespace Signify\Jobs;
3
4
use Signify\Models\CSPDocument;
5
use Signify\Models\CSPViolation;
6
use Signify\Reports\CSPViolationsReport;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\ORM\DataList;
9
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
10
use SilverStripe\ORM\DB;
11
12
class RemoveUnreferencedCSPDocumentJob extends AbstractQueuedJob
13
{
14
15
    public function setup()
16
    {
17
        $this->lastSeenID = -1;
0 ignored issues
show
Bug Best Practice introduced by
The property lastSeenID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
        $this->documentsDeleted = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property documentsDeleted does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
        $this->totalSteps = CSPDocument::get()->count();
20
    }
21
22
    public function process()
23
    {
24
        $batchSize = Config::inst()->get(CSPViolationsReport::class, 'deletion_batch_size');
25
26
        $deleted = 0;
27
        $delta = 0;
28
29
        // Wrapped in a transaction for performance only.
30
        try {
31
            DB::get_conn()->transactionStart();
32
33
            $documents = $this->getItemsList()->limit($batchSize);
34
35
            $lastDocument = $documents->last();
36
            if ($lastDocument) {
37
                $this->lastSeenID = $lastDocument->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property lastSeenID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
                unset($lastDocument);
39
            }
40
41
            /** @var CSPViolation $document */
42
            foreach ($documents as $document) {
43
                if (!$document->CSPViolations()->first()) {
44
                    # See https://github.com/silverstripe/silverstripe-framework/issues/1903
45
                    $document->CSPViolations()->removeAll();
46
47
                    $document->delete();
48
                    ++$deleted;
49
                }
50
                ++$delta;
51
            }
52
        }
53
        finally {
54
            DB::get_conn()->transactionEnd();
55
        }
56
57
        $this->documentsDeleted += $deleted;
58
        $this->currentStep += $delta;
59
60
        if ($delta < $batchSize) {
61
            $this->isComplete = true;
62
            print 'Removed ' . number_format($this->documentsDeleted) . ' unreferenced document URIs.';
63
        }
64
    }
65
66
    public function getTitle()
67
    {
68
        return 'Remove unreferenced CSP Document URIs';
69
    }
70
71
    private function getItemsList(): DataList
72
    {
73
        return CSPDocument::get()
74
            ->filter(['ID:GreaterThan' => $this->lastSeenID])
75
            ->sort('ID');
76
    }
77
}
78
79