Passed
Push — main ( 87db12...b71501 )
by Simon
01:24
created

ElasticSynonymExtension::onAfterDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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 Elastic\Elasticsearch\Exception\ClientResponseException;
14
use Elastic\Elasticsearch\Exception\MissingParameterException;
15
use Elastic\Elasticsearch\Exception\ServerResponseException;
16
use Firesphere\ElasticSearch\Models\SynonymSet;
17
use Firesphere\ElasticSearch\Services\ElasticCoreService;
18
use Firesphere\SearchBackend\Models\SearchSynonym;
19
use Psr\Container\NotFoundExceptionInterface;
20
use SilverStripe\Core\Injector\Injector;
21
use SilverStripe\Forms\FieldList;
22
use SilverStripe\ORM\DataExtension;
23
24
/**
25
 * Class \Firesphere\ElasticSearch\Extensions\ElasticSynonymExtension
26
 *
27
 * @property SearchSynonym|ElasticSynonymExtension $owner
28
 */
29
class ElasticSynonymExtension extends DataExtension
30
{
31
    /**
32
     * Add or update this synonym in Elastic
33
     *
34
     * @throws NotFoundExceptionInterface
35
     */
36
    public function onAfterWrite()
37
    {
38
        $service = Injector::inst()->get(ElasticCoreService::class);
39
        /** @var SearchSynonym|ElasticSynonymExtension $owner */
40
        $owner = $this->owner;
41
        $syn = $service->getClient()->synonyms();
42
        /** @var SynonymSet $set */
43
        $set = SynonymSet::get()->first();
44
        $syn->putSynonymRule([
45
            'set_id'  => $set->Key,
46
            '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

46
            '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...
47
            'body'    => [
48
                '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

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