From 03869eb8e1e1274da1a0761c400dc64e967a4e44 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Thu, 15 Feb 2024 17:05:47 +0100 Subject: [PATCH] ECS fixes --- src/Scout.php | 22 +++++++++---------- .../controllers/scout/IndexController.php | 10 ++++----- src/events/AfterIndexImport.php | 3 --- src/jobs/ImportIndex.php | 4 ++-- src/jobs/MakeSearchable.php | 9 +++----- src/utilities/ScoutUtility.php | 2 +- 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/Scout.php b/src/Scout.php index 4291882..d0416b6 100644 --- a/src/Scout.php +++ b/src/Scout.php @@ -46,12 +46,12 @@ public static function editions(): array public function init() { - Craft::$app->onInit(function () { + Craft::$app->onInit(function() { parent::init(); self::$plugin = $this; - Craft::$container->setSingleton(SearchClient::class, function () { + Craft::$container->setSingleton(SearchClient::class, function() { $config = SearchConfig::create( self::$plugin->getSettings()->getApplicationId(), self::$plugin->getSettings()->getAdminApiKey() @@ -101,7 +101,7 @@ private function registerUtility(): void Event::on( Utilities::class, Utilities::EVENT_REGISTER_UTILITIES, - function (RegisterComponentTypesEvent $event) { + function(RegisterComponentTypesEvent $event) { $event->types[] = ScoutUtility::class; } ); @@ -113,7 +113,7 @@ private function registerBehaviors(): void Event::on( Element::class, Element::EVENT_DEFINE_BEHAVIORS, - function (DefineBehaviorsEvent $event) { + function(DefineBehaviorsEvent $event) { $event->behaviors['searchable'] = SearchableBehavior::class; } ); @@ -125,7 +125,7 @@ private function registerVariables(): void Event::on( CraftVariable::class, CraftVariable::EVENT_INIT, - function (Event $event) { + function(Event $event) { /** @var CraftVariable $variable */ $variable = $event->sender; $variable->set('scout', ScoutVariable::class); @@ -154,7 +154,7 @@ private function registerEventHandlers(): void Event::on( $event[0], $event[1], - function (ElementEvent $event) { + function(ElementEvent $event) { /** @var SearchableBehavior $element */ $element = $event->element; if (!$element->hasMethod('searchable') || !$element->shouldBeSearchable()) { @@ -168,7 +168,7 @@ function (ElementEvent $event) { ->push( new IndexElement([ 'id' => $element->id, - 'siteId' => $element->site ? $element->site->id : null + 'siteId' => $element->site ? $element->site->id : null, ]) ); } else { @@ -181,7 +181,7 @@ function (ElementEvent $event) { Event::on( Elements::class, Elements::EVENT_BEFORE_DELETE_ELEMENT, - function (ElementEvent $event) { + function(ElementEvent $event) { if (!Scout::$plugin->getSettings()->indexRelations) { $this->beforeDeleteRelated = new Collection(); @@ -203,7 +203,7 @@ function (ElementEvent $event) { ->push( new DeindexElement([ 'id' => $element->id, - 'siteId' => $element->site ? $element->site->id : null + 'siteId' => $element->site ? $element->site->id : null, ]) ); } @@ -213,7 +213,7 @@ function (ElementEvent $event) { Event::on( Elements::class, Elements::EVENT_AFTER_DELETE_ELEMENT, - function (ElementEvent $event) { + function(ElementEvent $event) { // Skip this step if we already ran the DeIndex function earlier if (Scout::$plugin->getSettings()->queue) { return; @@ -226,7 +226,7 @@ function (ElementEvent $event) { } if ($this->beforeDeleteRelated) { - $this->beforeDeleteRelated->each(function (Element $relatedElement) { + $this->beforeDeleteRelated->each(function(Element $relatedElement) { /* @var SearchableBehavior $relatedElement */ if ($relatedElement->hasMethod('searchable')) { $relatedElement->searchable(false); diff --git a/src/console/controllers/scout/IndexController.php b/src/console/controllers/scout/IndexController.php index 5dee513..9b1cb89 100644 --- a/src/console/controllers/scout/IndexController.php +++ b/src/console/controllers/scout/IndexController.php @@ -35,9 +35,9 @@ public function actionFlush($index = '') } $engines = Scout::$plugin->getSettings()->getEngines(); - $engines->filter(function (Engine $engine) use ($index) { + $engines->filter(function(Engine $engine) use ($index) { return !$engine->scoutIndex->replicaIndex && ($index === '' || $engine->scoutIndex->indexName === $index); - })->each(function (Engine $engine) { + })->each(function(Engine $engine) { $engine->flush(); $this->stdout("Flushed index {$engine->scoutIndex->indexName}\n", Console::FG_GREEN); }); @@ -49,9 +49,9 @@ public function actionImport($index = '') { $engines = Scout::$plugin->getSettings()->getEngines(); - $engines->filter(function (Engine $engine) use ($index) { + $engines->filter(function(Engine $engine) use ($index) { return !$engine->scoutIndex->replicaIndex && ($index === '' || $engine->scoutIndex->indexName === $index); - })->each(function (Engine $engine) { + })->each(function(Engine $engine) { if ($this->queue) { Craft::$app->getQueue() ->ttr(Scout::$plugin->getSettings()->ttr) @@ -59,7 +59,7 @@ public function actionImport($index = '') ->push(new ImportIndex([ 'indexName' => $engine->scoutIndex->indexName, ])); - $this->stdout("Added ImportIndex job for '{$engine->scoutIndex->indexName}' to the queue".PHP_EOL, Console::FG_GREEN); + $this->stdout("Added ImportIndex job for '{$engine->scoutIndex->indexName}' to the queue" . PHP_EOL, Console::FG_GREEN); } else { $totalElements = $engine->scoutIndex->criteria->count(); $elementsUpdated = 0; diff --git a/src/events/AfterIndexImport.php b/src/events/AfterIndexImport.php index 438fb75..a2f6ecd 100644 --- a/src/events/AfterIndexImport.php +++ b/src/events/AfterIndexImport.php @@ -3,7 +3,6 @@ namespace rias\scout\events; use craft\base\Element; -use rias\scout\ScoutIndex; use yii\base\Event; /** @@ -17,6 +16,4 @@ class AfterIndexImport extends Event * @var string */ public string $indexName; - - } diff --git a/src/jobs/ImportIndex.php b/src/jobs/ImportIndex.php index dbcf71f..845b9fc 100644 --- a/src/jobs/ImportIndex.php +++ b/src/jobs/ImportIndex.php @@ -18,7 +18,7 @@ class ImportIndex extends BaseJob public function execute($queue): void { /** @var Engine $engine */ - $engine = Scout::$plugin->getSettings()->getEngines()->first(function (Engine $engine) { + $engine = Scout::$plugin->getSettings()->getEngines()->first(function(Engine $engine) { return $engine->scoutIndex->indexName === $this->indexName; }); @@ -39,7 +39,7 @@ public function execute($queue): void } $event = new AfterIndexImport([ - 'indexName' => $this->indexName + 'indexName' => $this->indexName, ]); Event::trigger(self::class, self::EVENT_AFTER_INDEX_IMPORT, $event); diff --git a/src/jobs/MakeSearchable.php b/src/jobs/MakeSearchable.php index 5332157..081a220 100644 --- a/src/jobs/MakeSearchable.php +++ b/src/jobs/MakeSearchable.php @@ -23,7 +23,6 @@ class MakeSearchable extends BaseJob public function execute($queue): void { - $element = $this->getElement(); if ($element) { // Enabled element found @@ -35,14 +34,12 @@ public function execute($queue): void } else { // Element not found, checking if it was disabled and needs to be de-indexed. $element = $this->getAnyElement(); - if($element){ - if(!$element->shouldBeSearchable()) { + if ($element) { + if (!$element->shouldBeSearchable()) { $element->unsearchable(); } } } - - } protected function defaultDescription(): string @@ -91,7 +88,7 @@ protected function getEngine() protected function getIndex() { - return Scout::$plugin->getSettings()->getIndices()->first(function (ScoutIndex $scoutindex) { + return Scout::$plugin->getSettings()->getIndices()->first(function(ScoutIndex $scoutindex) { return $scoutindex->indexName === $this->indexName; }); } diff --git a/src/utilities/ScoutUtility.php b/src/utilities/ScoutUtility.php index b28a064..9e665aa 100644 --- a/src/utilities/ScoutUtility.php +++ b/src/utilities/ScoutUtility.php @@ -30,7 +30,7 @@ public static function contentHtml(): string $engines = Scout::$plugin->getSettings()->getEngines(); - $stats = $engines->map(function (Engine $engine) { + $stats = $engines->map(function(Engine $engine) { $stats = [ 'name' => $engine->scoutIndex->indexName, 'replicaIndex' => $engine->scoutIndex->replicaIndex,