Passed
Push — hans/dirty-class-cleaning ( 0c80b6...a820f9 )
by Simon
06:12
created

ClearDirtyClassesTask   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 26 5
A createDeleteList() 0 10 2
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Tasks;
5
6
use Exception;
7
use Firesphere\SolrSearch\Extensions\DataObjectExtension;
8
use Firesphere\SolrSearch\Helpers\SolrLogger;
9
use Firesphere\SolrSearch\Models\DirtyClass;
10
use Firesphere\SolrSearch\Services\SolrCoreService;
11
use Firesphere\SolrSearch\Traits\LoggerTrait;
12
use GuzzleHttp\Exception\GuzzleException;
13
use SilverStripe\Control\HTTPRequest;
14
use SilverStripe\Dev\BuildTask;
15
use SilverStripe\ORM\ArrayList;
16
use SilverStripe\ORM\DataList;
17
use SilverStripe\ORM\ValidationException;
18
19
/**
20
 * Class ClearDirtyClasses
21
 * Clear out classes that were not succesfully updated or deleted in Solr.
22
 *
23
 * Any classes that failed to index properly or be removed properly need to be cleaned out regularly
24
 * This task takes care of doing this. It can be run directly via /dev/tasks, or via a queued job
25
 *
26
 * @package Firesphere\SolrSearch\Tasks
27
 */
28
class ClearDirtyClassesTask extends BuildTask
29
{
30
    use LoggerTrait;
31
    /**
32
     * @var string URLSegment
33
     */
34
    private static $segment = 'SolrClearDirtyClasses';
1 ignored issue
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
35
    /**
36
     * @var string Title
37
     */
38
    protected $title = 'Fix broken items in the Solr cores';
39
    /**
40
     * @var string Description
41
     */
42
    protected $description = 'Clear out classes that are marked as dirty on Solr.';
43
44
    /**
45
     * Clean up Dirty Classes in the index
46
     *
47
     * @param HTTPRequest $request
48
     * @return void
49
     * @throws GuzzleException
50
     * @throws \ReflectionException
51
     * @throws ValidationException
52
     */
53
    public function run($request)
54
    {
55
        /** @var DataList|DirtyClass $classes */
56
        $classes = DirtyClass::get();
57
        /** @var SolrCoreService $service */
58
        $service = new SolrCoreService();
59
        $solrLogger = new SolrLogger();
60
        foreach ($classes as $class) {
61
            $dirtyClass = $class->Class;
62
            $ids = json_decode($class->IDs);
63
            try {
64
                if ($class->Type === DataObjectExtension::WRITE) {
65
                    $dirtyClasses = $dirtyClass::get()->byIDs($ids);
66
                    $service->updateItems($dirtyClasses, SolrCoreService::UPDATE_TYPE);
67
                }
68
                if ($class->Type === DataObjectExtension::DELETE) {
69
                    $deletions = $this->createDeleteList($ids, $dirtyClass);
70
                    $service->updateItems($deletions, SolrCoreService::DELETE_TYPE);
71
                }
72
                $class->delete();
73
            } catch (Exception $exception) {
74
                $this->getLogger()->error($exception->getMessage());
75
                continue;
76
            }
77
        }
78
        $solrLogger->saveSolrLog('Index');
79
    }
80
81
    /**
82
     * Create an ArrayList of the dirty items to be deleted from Solr
83
     *
84
     * @param array $items
85
     * @param string $dirtyClass
86
     * @return ArrayList
87
     */
88
    public function createDeleteList($items, $dirtyClass): ArrayList
89
    {
90
        /** @var ArrayList $deletions */
91
        $deletions = ArrayList::create();
92
        foreach ($items as $item) {
93
            $dirtItem = $dirtyClass::create(['ID' => $item]);
94
            $deletions->push($dirtItem);
95
        }
96
97
        return $deletions;
98
    }
99
}
100