Skip to content

Commit

Permalink
feat: Check for null resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefanie Gevaert committed Jan 16, 2024
1 parent f448dba commit 3481b27
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 5.3.0 - 2024-01-16

### Added

- Check for null resolvers

## 5.2.0 - 2024-01-03 (Happy 2024)

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "craftpulse/craft-typesense",
"description": "Craft Plugin that synchronises with Typesense",
"type": "craft-plugin",
"version": "5.2.0",
"version": "5.3.0",
"keywords": [
"craft",
"cms",
Expand Down
22 changes: 13 additions & 9 deletions src/Typesense.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function init()
/**
* @inheritdoc
*/
// public function getSettings()
// public function getSettings()
// {
// return parent::getSettings();
// }
Expand Down Expand Up @@ -185,7 +185,7 @@ public function getCpNavItem(): ?array
];
}

// if (Craft::$app->getUser()->checkPermission('typesense:collections')) {
// if (Craft::$app->getUser()->checkPermission('typesense:collections')) {
// $subNavs['documents'] = [
// 'label' => Craft::t('typesense', 'Documents'),
// 'url' => 'typesense/documents',
Expand Down Expand Up @@ -237,7 +237,7 @@ protected function installCpEventListeners()
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
function(RegisterUrlRulesEvent $event) {
function (RegisterUrlRulesEvent $event) {
Craft::debug(
'UrlManager::EVENT_REGISTER_CP_URL_RULES',
__METHOD__
Expand All @@ -254,7 +254,7 @@ function(RegisterUrlRulesEvent $event) {
Event::on(
UserPermissions::class,
UserPermissions::EVENT_REGISTER_PERMISSIONS,
function(RegisterUserPermissionsEvent $event) {
function (RegisterUserPermissionsEvent $event) {
Craft::debug(
'UserPermissions::EVENT_REGISTER_PERMISSIONS',
__METHOD__
Expand Down Expand Up @@ -349,7 +349,7 @@ private function _registerEventHandlers(): void
Event::on(
$event[0],
$event[1],
function(ElementEvent $event) {
function (ElementEvent $event) {
// Ignore any element that is not an entry
if (!($event->element instanceof Entry)) {
return;
Expand Down Expand Up @@ -392,8 +392,12 @@ function(ElementEvent $event) {
Craft::info('Typesense edit / add / delete document based of: ' . $entry->title, __METHOD__);

try {
self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->upsert($collection->schema['resolver']($entry));
} catch(ObjectNotFound | ServerError $e) {
$resolver = $collection->schema['resolver']($entry);

if ($resolver) {
self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->upsert($resolver);
}
} catch (ObjectNotFound | ServerError $e) {
Craft::$app->session->setFlash('error', Craft::t('typesense', 'There was an issue saving your action, check the logs for more info'));
Craft::error($e->getMessage(), __METHOD__);
}
Expand All @@ -412,7 +416,7 @@ function(ElementEvent $event) {
Event::on(
Elements::class,
Elements::EVENT_AFTER_DELETE_ELEMENT,
function(ElementEvent $event) {
function (ElementEvent $event) {
$entry = $event->element;
$section = $entry->section->handle ?? null;
$id = $entry->id;
Expand Down Expand Up @@ -447,7 +451,7 @@ function(ElementEvent $event) {

private function _registerVariable(): void
{
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) {
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function (Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('typesense', [
Expand Down
16 changes: 10 additions & 6 deletions src/jobs/SyncDocumentsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class SyncDocumentsJob extends BaseJob
// Public Methods
// =========================================================================

public function execute($queue) :void
public function execute($queue): void
{
$upsertIds = [];
$collection = CollectionHelper::getCollection($this->criteria['index']);
$collectionTypesense = Typesense::$plugin->getCollections()->getCollectionByCollectionRetrieve($this->criteria['index']);
$client = Typesense::$plugin->getClient()->client();

if($client !== false && !is_null($collection)) {
if ($client !== false && !is_null($collection)) {

// delete collections if the action is flush
if ($collectionTypesense !== [] && $this->criteria['type'] === 'Flush') {
Expand All @@ -74,11 +74,15 @@ public function execute($queue) :void
//fetch each document of entry to update
foreach ($entries as $i => $entry) {

$doc = $client->collections[$this->criteria['index']]
->documents
->upsert($collection->schema['resolver']($entry));
$resolver = $collection->schema['resolver']($entry);

$upsertIds[] = $doc['id'];
if ($resolver) {
$doc = $client->collections[$this->criteria['index']]
->documents
->upsert($resolver);

$upsertIds[] = $doc['id'];
}

$this->setProgress(
$queue,
Expand Down

0 comments on commit 3481b27

Please sign in to comment.