BulkDeleteTask::run()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.8666
1
<?php
2
3
namespace Firesphere\ElasticSearch\Tasks;
4
5
use Exception;
6
use Firesphere\ElasticSearch\Services\ElasticCoreService;
7
use Psr\Container\NotFoundExceptionInterface;
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\BuildTask;
11
12
/**
13
 * Example task to empty an Elastic index
14
 */
15
class BulkDeleteTask extends BuildTask
16
{
17
    /**
18
     * URLSegment of this task
19
     *
20
     * @var string
21
     */
22
    private static $segment = 'Truncastic';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
23
    /**
24
     * My name
25
     *
26
     * @var string
27
     */
28
    protected $title = 'Truncate elastic core';
29
    /**
30
     * What do I do?
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Try to remove everything from an Elastic core, to start fresh.';
35
    /**
36
     * @var bool
37
     */
38
    protected $enabled = false;
39
40
    /**
41
     * @param $request
42
     * @return void
43
     * @throws NotFoundExceptionInterface
44
     */
45
    public function run($request)
46
    {
47
        $service = Injector::inst()->get(ElasticCoreService::class);
48
        $pages = SiteTree::get();
49
        foreach ($pages as $page) {
50
            $q = [
51
                'index' => 'search-health',
52
                'body'  => [
53
                    'query' => [
54
                        'match' => [
55
                            'ObjectID' => $page->ID
56
                        ]
57
                    ]
58
                ]
59
            ];
60
            try {
61
                $service->getClient()->deleteByQuery($q);
62
            } catch (Exception $e) {
63
                // noop, just continue
64
            }
65
        }
66
    }
67
}
68