Completed
Pull Request — master (#14)
by Gordon
02:08
created

IndexingExtension::onBeforeWrite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
1
<?php declare(strict_types = 1);
2
3
/**
4
 * Created by PhpStorm.
5
 * User: gordon
6
 * Date: 24/3/2561
7
 * Time: 20:36 น.
8
 */
9
10
namespace Suilven\FreeTextSearch\Extension;
11
12
use SilverStripe\Core\Extension;
13
use SilverStripe\ORM\DataExtension;
14
use SilverStripe\SiteConfig\SiteConfig;
15
use Suilven\FreeTextSearch\Factory\IndexerFactory;
16
17
class IndexingExtension extends DataExtension
18
{
19
    private static $db= [
20
      'IsDirtyFreeTextSearch' => 'Boolean'
21
    ];
22
23
24
    public function onBeforeWrite()
25
    {
26
        parent::onBeforeWrite();
27
28
        $config = SiteConfig::current_site_config();
29
        if ($config->FreeTextSearchIndexingModeInBulk === true) {
30
            $this->owner->IsDirtyFreeTextSearch = true;
31
        }
32
33
    }
34
35
    public function onAfterWrite(): void
36
    {
37
        $this->owner->onAfterWrite();
38
39
        $config = SiteConfig::current_site_config();
40
        if ($config->FreeTextSearchIndexingModeInBulk === false) {
41
            $factory = new IndexerFactory();
42
            $indexer = $factory->getIndexer();
43
44
            $indexer->index($this->owner);
45
            $this->owner->IsDirtyFreeTextSearch = false;
46
            $this->owner->write();
47
        }
48
    }
49
}
50