Skip to content

Commit

Permalink
Update for scrutinizer issues. pt. 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Firesphere committed Oct 20, 2023
1 parent 87db121 commit f49a25d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
25 changes: 10 additions & 15 deletions src/Extensions/ElasticSynonymExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Firesphere\ElasticSearch\Models\SynonymSet;
use Firesphere\ElasticSearch\Services\ElasticCoreService;
use Firesphere\SearchBackend\Models\SearchSynonym;
use Psr\Container\NotFoundExceptionInterface;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataExtension;
Expand All @@ -27,48 +28,42 @@
*/
class ElasticSynonymExtension extends DataExtension
{
public function updateCMSFields(FieldList $fields)
{
$fields->removeByName('SynonymSets');
parent::updateCMSFields($fields);
}

/**
* Add or update this synonym in Elastic
*
* @throws ClientResponseException
* @throws ServerResponseException
* @throws MissingParameterException
* @throws NotFoundExceptionInterface
*/
public function onAfterWrite()
{
$service = Injector::inst()->get(ElasticCoreService::class);
/** @var SearchSynonym|ElasticSynonymExtension $owner */
$owner = $this->owner;

Check warning on line 40 in src/Extensions/ElasticSynonymExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Extensions/ElasticSynonymExtension.php#L40

Added line #L40 was not covered by tests
$syn = $service->getClient()->synonyms();
/** @var SynonymSet $set */
$set = SynonymSet::get()->first();
$syn->putSynonymRule([
'set_id' => $set->Key,
'rule_id' => $this->owner->getModifiedID(),
'rule_id' => $owner->getModifiedID(),

Check warning on line 46 in src/Extensions/ElasticSynonymExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Extensions/ElasticSynonymExtension.php#L46

Added line #L46 was not covered by tests
'body' => [
'synonyms' => $this->owner->getCombinedSynonym()
'synonyms' => $owner->getCombinedSynonym()

Check warning on line 48 in src/Extensions/ElasticSynonymExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Extensions/ElasticSynonymExtension.php#L48

Added line #L48 was not covered by tests
]
]);
}

/**
* When deleting a synonym from the CMS, delete it as a rule
*
* @throws ClientResponseException
* @throws ServerResponseException
* @throws MissingParameterException
* @throws NotFoundExceptionInterface
*/
public function onAfterDelete()
{
$service = Injector::inst()->get(ElasticCoreService::class);
$syn = $service->getClient()->synonyms();
/** @var SearchSynonym $owner */
$owner = $this->owner;

Check warning on line 63 in src/Extensions/ElasticSynonymExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Extensions/ElasticSynonymExtension.php#L63

Added line #L63 was not covered by tests
/** @var SynonymSet $set */
$set = SynonymSet::get()->first();
$syn->deleteSynonymRule(['set_id' => $set->Key, 'rule_id' => $this->owner->getModifiedId()]);
$syn->deleteSynonymRule(['set_id' => $set->Key, 'rule_id' => $owner->getModifiedId()]);

Check warning on line 66 in src/Extensions/ElasticSynonymExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Extensions/ElasticSynonymExtension.php#L66

Added line #L66 was not covered by tests
parent::onAfterDelete();
}
}
17 changes: 16 additions & 1 deletion src/Tasks/ElasticIndexTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class ElasticIndexTask extends BuildTask
*/
protected $index;

/**
* @var int
*/
protected $groups = 0;

/**
* ElasticIndexTask constructor. Sets up the document factory
*
Expand Down Expand Up @@ -142,7 +147,7 @@ public function run($request)
$time = gmdate('H:i:s', (time() - $start));
$this->getLogger()->info(sprintf('Time taken: %s', $time));

return $groups;
$this->groups = $groups;
}

/**
Expand Down Expand Up @@ -217,6 +222,16 @@ public function setDebug(bool $debug, bool $force = false): self
return $this;
}

public function getGroups(): int
{
return $this->groups;
}

public function setGroups(int $groups): void

Check warning on line 230 in src/Tasks/ElasticIndexTask.php

View check run for this annotation

Codecov / codecov/patch

src/Tasks/ElasticIndexTask.php#L230

Added line #L230 was not covered by tests
{
$this->groups = $groups;

Check warning on line 232 in src/Tasks/ElasticIndexTask.php

View check run for this annotation

Codecov / codecov/patch

src/Tasks/ElasticIndexTask.php#L232

Added line #L232 was not covered by tests
}

/**
* Index a single class for a given index. {@link static::indexClassForIndex()}
*
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Tasks/ElasticIndexTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public function testRun()
$page->publishSingle();
$task = new ElasticIndexTask();
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask');
$result = $task->run($request);
$task->run($request);

$this->assertGreaterThan(0, $result);
$this->assertGreaterThan(0, $task->getGroups());
$this->assertinstanceOf(ElasticIndex::class, $task->getIndex());
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask', ['clear' => true]);
$result = $task->run($request);
$task->run($request);

$this->assertGreaterThan(0, $result);
$this->assertGreaterThan(0, $task->getGroups());
$this->assertinstanceOf(ElasticIndex::class, $task->getIndex());
}
}

0 comments on commit f49a25d

Please sign in to comment.