ElasticSynonymExtension   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onAfterWrite() 0 13 1
A onAfterDelete() 0 10 1
1
<?php
2
/**
3
 * class ElasticSynonymExtension|Firesphere\ElasticSearch\Extensions\ElasticSynonymExtension Synonym updates for Elastic
4
 * pushed to Elastic
5
 *
6
 * @package Firesphere\Elastic\Search
7
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
8
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
9
 */
10
11
namespace Firesphere\ElasticSearch\Extensions;
12
13
use Firesphere\ElasticSearch\Models\SynonymSet;
14
use Firesphere\ElasticSearch\Services\ElasticCoreService;
15
use Firesphere\SearchBackend\Models\SearchSynonym;
16
use Psr\Container\NotFoundExceptionInterface;
17
use SilverStripe\Core\Injector\Injector;
18
use SilverStripe\ORM\DataExtension;
19
20
/**
21
 * Class \Firesphere\ElasticSearch\Extensions\ElasticSynonymExtension
22
 *
23
 * @property SearchSynonym|ElasticSynonymExtension $owner
24
 */
25
class ElasticSynonymExtension extends DataExtension
26
{
27
    /**
28
     * Add or update this synonym in Elastic
29
     *
30
     * @throws NotFoundExceptionInterface
31
     */
32
    public function onAfterWrite()
33
    {
34
        $service = Injector::inst()->get(ElasticCoreService::class);
35
        /** @var SearchSynonym|ElasticSynonymExtension $owner */
36
        $owner = $this->owner;
37
        $syn = $service->getClient()->synonyms();
38
        /** @var SynonymSet $set */
39
        $set = SynonymSet::get()->first();
40
        $syn->putSynonymRule([
41
            'set_id'  => $set->Key,
42
            'rule_id' => $owner->getModifiedID(),
0 ignored issues
show
Bug introduced by
The method getModifiedID() does not exist on Firesphere\ElasticSearch...ElasticSynonymExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
            'rule_id' => $owner->/** @scrutinizer ignore-call */ getModifiedID(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
            'body'    => [
44
                'synonyms' => $owner->getCombinedSynonym()
0 ignored issues
show
Bug introduced by
The method getCombinedSynonym() does not exist on Firesphere\ElasticSearch...ElasticSynonymExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
                'synonyms' => $owner->/** @scrutinizer ignore-call */ getCombinedSynonym()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
            ]
46
        ]);
47
    }
48
49
    /**
50
     * When deleting a synonym from the CMS, delete it as a rule
51
     *
52
     * @throws NotFoundExceptionInterface
53
     */
54
    public function onAfterDelete()
55
    {
56
        $service = Injector::inst()->get(ElasticCoreService::class);
57
        $syn = $service->getClient()->synonyms();
58
        /** @var SearchSynonym $owner */
59
        $owner = $this->owner;
60
        /** @var SynonymSet $set */
61
        $set = SynonymSet::get()->first();
62
        $syn->deleteSynonymRule(['set_id' => $set->Key, 'rule_id' => $owner->getModifiedId()]);
63
        parent::onAfterDelete();
64
    }
65
}
66