Passed
Push — sheepy/introspection ( 69e16c...c6c7ca )
by Marco
05:28
created

SolrIndexJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Jobs;
5
6
use Exception;
7
use Firesphere\SolrSearch\Indexes\BaseIndex;
8
use Firesphere\SolrSearch\Services\SolrCoreService;
9
use Firesphere\SolrSearch\Tasks\SolrIndexTask;
10
use ReflectionException;
11
use SilverStripe\Control\HTTPRequest;
12
use SilverStripe\Core\Injector\Injector;
13
use stdClass;
14
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
15
use Symbiote\QueuedJobs\Services\QueuedJobService;
16
17
/**
18
 * Class SolrIndexJob
19
 * @package Firesphere\SolrSearch\Jobs
20
 */
21
class SolrIndexJob extends AbstractQueuedJob
22
{
23
24
    /**
25
     * The class that should be indexed.
26
     * If set, the task should run the given class with the given group
27
     * The class should be popped off the array at the end of each subset
28
     * so the next class becomes the class to index.
29
     *
30
     * Rinse and repeat for each class in the index, until this array is empty
31
     *
32
     * @var array
33
     */
34
    protected $classToIndex = [];
35
36
    /**
37
     * The indexes that need to run.
38
     * @var array
39
     */
40
    protected $indexes;
41
42
    /**
43
     * @return string
44
     */
45
    public function getTitle()
46 3
    {
47
        return 'Index groups to Solr search';
48 3
    }
49
50 3
    /**
51
     * Do some processing yourself!
52
     * @return self
53
     * @throws Exception
54
     */
55 2
    public function process()
56
    {
57 2
        $data = $this->jobData;
58
59
        $this->configureRun($data);
60
61
        $this->currentStep = $this->currentStep ?: 0;
62
        $index = Injector::inst()->get($this->indexes[0]);
63
        $this->classToIndex = count($this->classToIndex) ? $this->classToIndex : $index->getClasses();
64
        $indexArgs = [
65 1
            'group' => $this->currentStep,
66
            'index' => $this->indexes[0],
67 1
            'class' => $this->classToIndex[0]
68
        ];
69 1
        /** @var SolrIndexTask $task */
70
        $task = Injector::inst()->get(SolrIndexTask::class);
71 1
        $request = new HTTPRequest(
72 1
            'GET',
73 1
            '/dev/tasks/SolrIndexTask',
74
            $indexArgs
75 1
        );
76 1
77 1
        $result = $task->run($request);
78
        $this->totalSteps = $result;
79
        // If the result is false, the job should fail too
80
        // Thus, only set to true if the result isn't false :)
81 1
        $this->isComplete = true;
82 1
83 1
        /** @var self $this */
84 1
        return $this;
85 1
    }
86
87
    /**
88 1
     * @param stdClass|null $data
89 1
     * @throws ReflectionException
90
     */
91
    protected function configureRun($data)
92 1
    {
93
        // If null gets passed in, it goes a bit wonky with the check for indexes
94
        if (!$data) {
95 1
            $data = new stdClass();
96
            $data->indexes = null;
97
        }
98
        if (!isset($data->indexes) || !count($data->indexes)) { // If indexes are set, don't load them.
99
            $this->indexes = (new SolrCoreService())->getValidIndexes();
100
        } else {
101
            $this->setIndexes($data->indexes);
102 1
            $this->setClassToIndex($data->classToIndex);
103
        }
104
    }
105 1
106 1
    /**
107 1
     * Set up the next job if needed
108
     */
109 1
    public function afterComplete()
110 1
    {
111
        [$currentStep, $totalSteps] = $this->getNextSteps();
112 1
        // If there are no indexes left to run, let's call it a day
113 1
        if (count($this->indexes)) {
114
            $nextJob = new self();
115 1
            $jobData = new stdClass();
116
117
            $jobData->classToIndex = $this->getClassToIndex();
118
            $jobData->indexes = $this->getIndexes();
119
            $nextJob->setJobData($totalSteps, $currentStep, false, $jobData, []);
120 1
121
            // Add a wee break to let the system recover from this heavy operation
122 1
            Injector::inst()->get(QueuedJobService::class)
123
                ->queueJob($nextJob, date('Y-m-d H:i:00', strtotime('+1 minutes')));
124 1
        }
125 1
        parent::afterComplete();
126 1
    }
127
128 1
    /**
129 1
     * @return array
130 1
     */
131
    protected function getNextSteps(): array
132
    {
133 1
        $currentStep = $this->currentStep + 1;
134 1
        $totalSteps = $this->totalSteps;
135
        // No more steps to execute on this class, let's go to the next class
136 1
        if ($this->currentStep >= $this->totalSteps) {
137 1
            array_shift($this->classToIndex);
138
            // Reset the current step, a complete new set of data is coming
139
            $currentStep = 0;
140
            $totalSteps = 1;
141
        }
142 3
        // If there are no classes left in this index, go to the next index
143
        if (!count($this->classToIndex)) {
144 3
            array_shift($this->indexes);
145
            // Reset the current step, a complete new set of data is coming
146
            $currentStep = 0;
147
            $totalSteps = 1;
148
        }
149
150
        return [$currentStep, $totalSteps];
151 3
    }
152
153 3
    /**
154
     * @return array
155 3
     */
156
    public function getClassToIndex(): array
157
    {
158
        return $this->classToIndex;
159
    }
160
161 3
    /**
162
     * @param array $classToIndex
163 3
     * @return SolrIndexJob
164
     */
165
    public function setClassToIndex($classToIndex)
166
    {
167
        $this->classToIndex = $classToIndex;
168
169
        return $this;
170 3
    }
171
172 3
    /**
173
     * @return array
174 3
     */
175
    public function getIndexes(): array
176
    {
177
        return $this->indexes;
178
    }
179
180 1
    /**
181
     * @param array $indexes
182 1
     * @return SolrIndexJob
183 1
     */
184
    public function setIndexes($indexes)
185 1
    {
186 1
        $this->indexes = $indexes;
187
188 1
        return $this;
189 1
    }
190
}
191