Skip to content

Commit

Permalink
Merge pull request #6 from brilliancenw/develop-4
Browse files Browse the repository at this point in the history
modifications to support Craft v4
  • Loading branch information
markmiddleton authored Nov 8, 2022
2 parents 8edf88a + 68ee75f commit 19a6827
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
],
"require": {
"craftcms/cms": ">=3.4.18",
"craftcms/cms": "^4",
"algolia/algoliasearch-client-php": "^3"
},
"autoload": {
Expand Down
11 changes: 8 additions & 3 deletions src/jobs/AlgoliaSyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Craft;
use craft\queue\BaseJob;

use Algolia\AlgoliaSearch\SearchClient;

/**
* AlgoliaSyncTask job
*
Expand Down Expand Up @@ -74,7 +76,10 @@ class AlgoliaSyncTask extends BaseJob
public function execute($queue): void
{

$client = new \AlgoliaSearch\Client(AlgoliaSync::$plugin->settings->getAlgoliaApp(), AlgoliaSync::$plugin->settings->getAlgoliaAdmin());
$client = SearchClient::create(
AlgoliaSync::$plugin->settings->getAlgoliaApp(),
AlgoliaSync::$plugin->settings->getAlgoliaAdmin()
);

foreach ($this->algoliaIndex AS $index) {

Expand All @@ -84,11 +89,11 @@ public function execute($queue): void

SWITCH ($this->algoliaFunction) {
CASE 'insert':
$response = $clientIndex->addObject($this->algoliaRecord);
$clientIndex->saveObject($this->algoliaRecord)->wait();
break;

CASE 'delete':
$response = $clientIndex->deleteObject($this->algoliaObjectID);
$clientIndex->deleteObject($this->algoliaObjectID);
break;
}

Expand Down
11 changes: 7 additions & 4 deletions src/services/AlgoliaSyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use craft\elements\User;

use craft\helpers\MoneyHelper;
//use craft\web\twig\Environment;
//use Twig\Environment as env;

use brilliance\algoliasync\events\beforeAlgoliaSyncEvent;

Expand Down Expand Up @@ -528,7 +526,7 @@ public function getAlgoliaIndex($element): array
{
$returnIndex = [];
$eventInfo = AlgoliaSync::$plugin->algoliaSyncService->getEventElementInfo($element, false);
$envName = strtolower(App::env('CRAFT_ENVIRONMENT') ?? App::env('ENVIRONMENT') ?? 'site');
$envName = $this->getEnvironment();

foreach ($eventInfo['sectionHandle'] AS $handle) {
$returnIndex[] = $envName.'_'.$eventInfo['type'].'_'.$handle;
Expand All @@ -539,7 +537,7 @@ public function getAlgoliaIndex($element): array

// AlgoliaSync::$plugin->algoliaSyncService->getAlgoliaSupportedElements()
public function getAlgoliaSupportedElements(): array {
$env = strtolower(App::env('CRAFT_ENVIRONMENT') ?? App::env('ENVIRONMENT') ?? 'site');
$env = $this->getEnvironment();

// all Channel Sections
$entriesConfig = array();
Expand Down Expand Up @@ -628,4 +626,9 @@ public function getAlgoliaSupportedElements(): array {
['label' => 'Tag Groups', 'handle' => 'tag', 'data' => $tagGroupsConfig]
];
}

public function getEnvironment(): string {
return strtolower(App::env('CRAFT_ENVIRONMENT') ?? App::env('ENVIRONMENT') ?? 'site');
}

}

0 comments on commit 19a6827

Please sign in to comment.