diff --git a/Adapter/Client.php b/Adapter/Client.php
index cba5506..9d66e35 100644
--- a/Adapter/Client.php
+++ b/Adapter/Client.php
@@ -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
@@ -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();
}
@@ -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;
}
}
diff --git a/Model/Config/Source/TypeSenseIndexMethod.php b/Model/Config/Source/TypeSenseIndexMethod.php
index 9af75ea..256199f 100644
--- a/Model/Config/Source/TypeSenseIndexMethod.php
+++ b/Model/Config/Source/TypeSenseIndexMethod.php
@@ -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'
];
/**
@@ -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;
}
}
diff --git a/Model/Config/Source/TypeSenseProtocol.php b/Model/Config/Source/TypeSenseProtocol.php
new file mode 100644
index 0000000..ab222ce
--- /dev/null
+++ b/Model/Config/Source/TypeSenseProtocol.php
@@ -0,0 +1,35 @@
+ 'HTTP',
+ self::HTTPS => 'HTTPS',
+ ];
+
+ /**
+ * @return array
+ */
+ public function toOptionArray()
+ {
+ $options = [];
+ foreach ($this->protocols as $key => $value) {
+ $options[] = [
+ 'value' => $key,
+ 'label' => __($value),
+ ];
+ }
+ return $options;
+ }
+}
diff --git a/Plugin/Backend/Algolia/AlgoliaSearch/Helper/AlgoliaHelper.php b/Plugin/Backend/Algolia/AlgoliaSearch/Helper/AlgoliaHelper.php
deleted file mode 100644
index 46f971a..0000000
--- a/Plugin/Backend/Algolia/AlgoliaSearch/Helper/AlgoliaHelper.php
+++ /dev/null
@@ -1,68 +0,0 @@
-typesenseClient = $client;
- $this->scopeConfig = $scopeConfig;
- }
-
- /**
- * 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
- ) {
- $result = [];
- $indexMethod = $this->scopeConfig->getValue(SELF::TYPESENSE_INDEX_METHID,ScopeConfig::SCOPE_STORE);
- switch ($indexMethod) {
- case "algolia":
- $result = $proceed();
- break;
- case "typesense_algolia":
- $this->typesenseClient->addData($indexName, $objects);
- $result = $proceed();
- break;
- case "typesense":
- default:
- $this->typesenseClient->addData($indexName,$objects);
- break;
- }
-
- return $result;
- }
-}
\ No newline at end of file
diff --git a/Plugin/Backend/Algolia/AlgoliaSearch/Helper/AlgoliaHelperPlugin.php b/Plugin/Backend/Algolia/AlgoliaSearch/Helper/AlgoliaHelperPlugin.php
new file mode 100644
index 0000000..a30bb4f
--- /dev/null
+++ b/Plugin/Backend/Algolia/AlgoliaSearch/Helper/AlgoliaHelperPlugin.php
@@ -0,0 +1,71 @@
+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;
+ }
+}
diff --git a/README.md b/README.md
index 2cad8a0..53913fc 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/Services/ConfigService.php b/Services/ConfigService.php
new file mode 100644
index 0000000..cf8bbe9
--- /dev/null
+++ b/Services/ConfigService.php
@@ -0,0 +1,107 @@
+scopeConfig = $scopeConfig;
+ $this->encryptor = $encryptor;
+ }
+
+ /**
+ * @return int|null
+ */
+ public function isEnabled(): ?int
+ {
+ return (int) $this->scopeConfig->getValue(self::TYPESENSE_ENABLED, ScopeConfig::SCOPE_STORE);
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getCloudId(): ?string
+ {
+ return $this->scopeConfig->getValue(self::TYPESENSE_CLOUD_ID, ScopeConfig::SCOPE_STORE);
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getApiKey(): ?string
+ {
+ $value = $this->scopeConfig->getValue(SELF::TYPESENSE_API_KEY, ScopeConfig::SCOPE_STORE);
+ return $this->encryptor->decrypt($value);
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getSearchOnlyKey(): ?string
+ {
+ $value = $this->scopeConfig->getValue(SELF::TYPESENSE_SEARCH_ONLY_KEY_KEY, ScopeConfig::SCOPE_STORE);
+ return $this->encryptor->decrypt($value);
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getNodes(): ?string
+ {
+ return $this->scopeConfig->getValue(self::TYPESENSE_NODES, ScopeConfig::SCOPE_STORE);
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getPort(): ?string
+ {
+ return $this->scopeConfig->getValue(self::TYPESENSE_PORT, ScopeConfig::SCOPE_STORE);
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getProtocol(): ?string
+ {
+ return $this->scopeConfig->getValue(self::TYPESENSE_PROTOCOL, ScopeConfig::SCOPE_STORE);
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getIndexMethod(): ?string
+ {
+ return $this->scopeConfig->getValue(self::TYPESENSE_INDEX_METHOD, ScopeConfig::SCOPE_STORE);
+ }
+}
diff --git a/composer.json b/composer.json
index 0cd3f1a..1cc70f9 100644
--- a/composer.json
+++ b/composer.json
@@ -5,7 +5,8 @@
"license": "OSL-3.0",
"require": {
"algolia/algoliasearch-magento-2": "^3.9",
- "typesense/typesense-php": "^4.8"
+ "typesense/typesense-php": "^4.8",
+ "php-http/curl-client": "^2.2"
},
"authors": [
{
@@ -18,7 +19,11 @@
}
],
"autoload": {
- "files": ["registration.php"],
- "psr-4": {"Develo\\Typesense\\": ""}
+ "files": [
+ "registration.php"
+ ],
+ "psr-4": {
+ "Develo\\Typesense\\": ""
+ }
}
}
diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml
index 941c0a5..d589390 100644
--- a/etc/adminhtml/di.xml
+++ b/etc/adminhtml/di.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml
index c441d6f..3a32151 100644
--- a/etc/adminhtml/system.xml
+++ b/etc/adminhtml/system.xml
@@ -15,29 +15,38 @@
Enable Typesense Adapter to override Algolias indexer
Magento\Config\Model\Config\Source\Enabledisable
-
-
- Where should Algolia data be indexed.
- Develo\Typesense\Model\Config\Source\TypeSenseIndexMethod
-
-
+
-
+
Magento\Config\Model\Config\Backend\Encrypted
typesense_general/settings/admin_api_key
-
+
-
+
+
+
+
+
+
+
+ Communication protocol.
+ Develo\Typesense\Model\Config\Source\TypeSenseProtocol
+
+
+
+ Where should Algolia data be indexed.
+ Develo\Typesense\Model\Config\Source\TypeSenseIndexMethod
+
diff --git a/etc/config.xml b/etc/config.xml
index b94c588..943d37b 100644
--- a/etc/config.xml
+++ b/etc/config.xml
@@ -11,6 +11,8 @@
1
+
+
diff --git a/etc/di.xml b/etc/di.xml
index 61681f6..12efa73 100644
--- a/etc/di.xml
+++ b/etc/di.xml
@@ -3,6 +3,5 @@
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
-