Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 41 additions & 43 deletions Adapter/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Develo\Typesense\Adapter;

use Devloops\Typesence\Client as TypeSenseClient;
use \Magento\Store\Model\ScopeInterface as ScopeConfig;
use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Framework\Encryption\EncryptorInterface;
use Typesense\Client as TypeSenseClient;
use Develo\Typesense\Services\ConfigService;

/**
* Class Client
Expand All @@ -17,58 +15,36 @@
*/
class Client
{

/**
* Config paths
* @var ConfigService
*/
private const TYPESENSE_API_KEY = 'typesense_general/settings/admin_api_key';
private const TYPESENSE_NODES = 'typesense_general/settings/nodes';
private ConfigService $configService;

/**
* @var TypeSenseClient
* @var TypeSenseClient|null
*/
private $typeSenseClient;

/**
* encryptor
*/
private $encryptor;
private ?TypeSenseClient $typeSenseClient = null;

/**
* Initialise Typesense Client with Magento config
*
* @param ConfigService $configService
* @throws \Typesense\Exceptions\ConfigError
*/
public function __construct(
EncryptorInterface $encryptor,
ScopeConfigInterface $scopeConfig
ConfigService $configService,
)
{
$apiKey = $scopeConfig->getValue(SELF::TYPESENSE_API_KEY,ScopeConfig::SCOPE_STORE);
$apiKey = $encryptor->decrypt($apiKey);

$nodes = $scopeConfig->getValue(SELF::TYPESENSE_NODES,ScopeConfig::SCOPE_STORE);

$client = new TypeSenseClient(
[
"api_key" => $apiKey,
"nodes"=>
[
[
"host" => $nodes,
"port" => "443",
"protocol" => "https",
"api_key" => $apiKey
]
]
]
);

$this->typeSenseClient = $client;
{
$this->configService = $configService;
}

/**
* @inheirtDoc
* @param string $indexName
* @return array
* @throws \Http\Client\Exception
* @throws \Typesense\Exceptions\TypesenseClientError
*/
public function deleteIndex($indexName)
public function deleteIndex(string $indexName): array
{
return $this->typeSenseClient->collections[$indexName]->delete();
}
Expand All @@ -78,10 +54,32 @@ public function deleteIndex($indexName)
*/
public function addData($indexName, $data)
{
return $this->typeSenseClient->collections[$indexName]->documents->create_many($data, ['action' => 'upsert']);
return $this->getTypesenseClient()->collections[$indexName]->getDocuments()->import($data, ['action' => 'upsert']);
}

public function getTypesenseClient(){
/**
* @return TypeSenseClient
*/
public function getTypesenseClient(): TypeSenseClient
{
if (is_null($this->typeSenseClient)) {
$client = new TypeSenseClient(
[
"api_key" => $this->configService->getApiKey(),
"nodes" =>
[
[
"host" => $this->configService->getNodes(),
"port" => $this->configService->getPort(),
"protocol" => $this->configService->getProtocol(),
"api_key" => $this->configService->getApiKey()
]
]
]
);

$this->typeSenseClient = $client;
}
return $this->typeSenseClient;
}
}
Expand Down
13 changes: 7 additions & 6 deletions Model/Config/Source/TypeSenseIndexMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
namespace Develo\Typesense\Model\Config\Source;

/**
* Algolia custom sort order field
* Typesense module indexation methods
*/
class TypeSenseIndexMethod implements \Magento\Framework\Data\OptionSourceInterface
{
const METHOD_TYPESENSE = 'typesense';
const METHOD_ALGOLIA = 'algolia';
const METHOD_BOTH = 'typesense_algolia';
/**
* @var array
*/
private $methods = [
'typesense' => 'Typesense Only',
'typesense_algolia' => 'Both Typesense and Aglolia',
'algolia' => 'Algolia Only'
self::METHOD_TYPESENSE => 'Typesense Only',
self::METHOD_BOTH => 'Both Typesense and Aglolia',
self::METHOD_ALGOLIA => 'Algolia Only'
];

/**
Expand All @@ -22,14 +25,12 @@ class TypeSenseIndexMethod implements \Magento\Framework\Data\OptionSourceInterf
public function toOptionArray()
{
$options = [];

foreach ($this->methods as $key => $value) {
$options[] = [
'value' => $key,
'label' => __($value),
];
}

return $options;
}
}
35 changes: 35 additions & 0 deletions Model/Config/Source/TypeSenseProtocol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Develo\Typesense\Model\Config\Source;

/**
* Typesense protocols
*/
class TypeSenseProtocol implements \Magento\Framework\Data\OptionSourceInterface
{
const HTTP = 'http';
const HTTPS = 'https';

/**
* @var array
*/
private $protocols = [
self::HTTP => 'HTTP',
self::HTTPS => 'HTTPS',
];

/**
* @return array
*/
public function toOptionArray()
{
$options = [];
foreach ($this->protocols as $key => $value) {
$options[] = [
'value' => $key,
'label' => __($value),
];
}
return $options;
}
}
68 changes: 0 additions & 68 deletions Plugin/Backend/Algolia/AlgoliaSearch/Helper/AlgoliaHelper.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Develo\Typesense\Plugin\Backend\Algolia\AlgoliaSearch\Helper;

use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
use Develo\Typesense\Adapter\Client;
use Develo\Typesense\Services\ConfigService;
use Develo\Typesense\Model\Config\Source\TypeSenseIndexMethod;

class AlgoliaHelperPlugin
{
/**
* @var ConfigService
*/
protected ConfigService $configService;

/**
* @var Client $typesenseClient
*/
protected Client $typesenseClient;

/**
* @param ConfigService $configService
* @param Client $client
*/
public function __construct(
ConfigService $configService,
Client $client
)
{
$this->configService = $configService;
$this->typesenseClient = $client;
}

/**
* Indexes data if config is set todo, will index into algolia or typesense or both
*/
public function aroundAddObjects(
\Algolia\AlgoliaSearch\Helper\AlgoliaHelper $subject,
\Closure $proceed,
$objects,
$indexName
)
{
if ($this->configService->isEnabled()) {
$result = [];
$indexMethod = $this->configService->getIndexMethod();
switch ($indexMethod) {
case TypeSenseIndexMethod::METHOD_ALGOLIA:
$result = $proceed();
break;
case TypeSenseIndexMethod::METHOD_BOTH:
$this->typesenseClient->addData($indexName, $objects);
$result = $proceed();
break;
case TypeSenseIndexMethod::METHOD_TYPESENSE:
default:
$this->typesenseClient->addData($indexName, $objects);
break;
}
} else {
$result = $proceed();
}
return $result;
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Composer install this module, it will include the Algolia module as a dependancy
### Installation

```shell
composer install develodesign/magento2-module-typesense
composer require develodesign/magento2-module-typesense
```

Add Typesene Configuration
Expand Down
Loading