Completed
Push — master ( 8deccf...0fd7bd )
by
unknown
03:42 queued 02:08
created

SearchUpdater_ObjectHandler::onAfterDelete()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 0
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Search\Extensions;
4
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
7
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\ORM\DataObject;
10
11
/**
12
 * Delete operations do not use database manipulations.
13
 *
14
 * If a delete has been requested, force a write on objects that should be
15
 * indexed.  This causes the object to be marked for deletion from the index.
16
 */
17
18
class SearchUpdater_ObjectHandler extends DataExtension
19
{
20
    public function onAfterDelete()
21
    {
22
        // Calling delete() on empty objects does nothing
23
        if (!$this->owner->ID) {
24
            return;
25
        }
26
27
        // Force SearchUpdater to mark this record as dirty
28
        // Note: Some extensions require entire hierarchy passed to augmentWrite()
29
        $manipulation = array();
30
        foreach (ClassInfo::ancestry($this->owner) as $class) {
31
            if (!is_subclass_of($class, DataObject::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \SilverStripe\ORM\DataObject::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
32
                continue;
33
            }
34
35
            $tableName = DataObject::getSchema()->tableName($class);
36
            $manipulation[$tableName] = array(
37
                'fields' => array(),
38
                'id' => $this->owner->ID,
39
                'class' => $class,
40
                // Note: 'delete' command not actually handled by manipulations,
41
                // but added so that SearchUpdater can detect the deletion
42
                'command' => 'delete'
43
            );
44
        }
45
46
        $this->owner->extend('augmentWrite', $manipulation);
47
48
        SearchUpdater::handle_manipulation($manipulation);
49
    }
50
51
    /**
52
     * Forces this object to trigger a re-index in the current state
53
     */
54
    public function triggerReindex()
55
    {
56
        if (!$this->owner->ID) {
57
            return;
58
        }
59
60
        $id = $this->owner->ID;
61
        $class = $this->owner->ClassName;
62
        $state = SearchVariant::current_state($class);
63
        $base = DataObject::getSchema()->baseDataClass($class);
64
        $key = "$id:$base:".serialize($state);
65
66
        $statefulids = array(array(
67
            'id' => $id,
68
            'state' => $state
69
        ));
70
71
        $writes = array(
72
            $key => array(
73
                'base' => $base,
74
                'class' => $class,
75
                'id' => $id,
76
                'statefulids' => $statefulids,
77
                'fields' => array()
78
            )
79
        );
80
81
        SearchUpdater::process_writes($writes);
82
    }
83
}
84