Skip to content

Commit 0e81c96

Browse files
authored
Refactor 20.02.2023 (#4)
1 parent 60b0f45 commit 0e81c96

File tree

11 files changed

+290
-130
lines changed

11 files changed

+290
-130
lines changed

Adapter/Client.php

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Develo\Typesense\Adapter;
44

5-
use Devloops\Typesence\Client as TypeSenseClient;
6-
use \Magento\Store\Model\ScopeInterface as ScopeConfig;
7-
use \Magento\Framework\App\Config\ScopeConfigInterface;
8-
use \Magento\Framework\Encryption\EncryptorInterface;
5+
use Typesense\Client as TypeSenseClient;
6+
use Develo\Typesense\Services\ConfigService;
97

108
/**
119
* Class Client
@@ -17,58 +15,36 @@
1715
*/
1816
class Client
1917
{
20-
2118
/**
22-
* Config paths
19+
* @var ConfigService
2320
*/
24-
private const TYPESENSE_API_KEY = 'typesense_general/settings/admin_api_key';
25-
private const TYPESENSE_NODES = 'typesense_general/settings/nodes';
21+
private ConfigService $configService;
2622

2723
/**
28-
* @var TypeSenseClient
24+
* @var TypeSenseClient|null
2925
*/
30-
private $typeSenseClient;
31-
32-
/**
33-
* encryptor
34-
*/
35-
private $encryptor;
26+
private ?TypeSenseClient $typeSenseClient = null;
3627

3728
/**
3829
* Initialise Typesense Client with Magento config
30+
*
31+
* @param ConfigService $configService
32+
* @throws \Typesense\Exceptions\ConfigError
3933
*/
4034
public function __construct(
41-
EncryptorInterface $encryptor,
42-
ScopeConfigInterface $scopeConfig
35+
ConfigService $configService,
4336
)
44-
{
45-
$apiKey = $scopeConfig->getValue(SELF::TYPESENSE_API_KEY,ScopeConfig::SCOPE_STORE);
46-
$apiKey = $encryptor->decrypt($apiKey);
47-
48-
$nodes = $scopeConfig->getValue(SELF::TYPESENSE_NODES,ScopeConfig::SCOPE_STORE);
49-
50-
$client = new TypeSenseClient(
51-
[
52-
"api_key" => $apiKey,
53-
"nodes"=>
54-
[
55-
[
56-
"host" => $nodes,
57-
"port" => "443",
58-
"protocol" => "https",
59-
"api_key" => $apiKey
60-
]
61-
]
62-
]
63-
);
64-
65-
$this->typeSenseClient = $client;
37+
{
38+
$this->configService = $configService;
6639
}
6740

6841
/**
69-
* @inheirtDoc
42+
* @param string $indexName
43+
* @return array
44+
* @throws \Http\Client\Exception
45+
* @throws \Typesense\Exceptions\TypesenseClientError
7046
*/
71-
public function deleteIndex($indexName)
47+
public function deleteIndex(string $indexName): array
7248
{
7349
return $this->typeSenseClient->collections[$indexName]->delete();
7450
}
@@ -81,7 +57,29 @@ public function addData($indexName, $data)
8157
return $this->typeSenseClient->collections[$indexName]->documents->create_many($data, ['action' => 'upsert']);
8258
}
8359

84-
public function getTypesenseClient(){
60+
/**
61+
* @return TypeSenseClient
62+
*/
63+
public function getTypesenseClient(): TypeSenseClient
64+
{
65+
if (is_null($this->typeSenseClient)) {
66+
$client = new TypeSenseClient(
67+
[
68+
"api_key" => $this->configService->getApiKey(),
69+
"nodes" =>
70+
[
71+
[
72+
"host" => $this->configService->getNodes(),
73+
"port" => $this->configService->getPort(),
74+
"protocol" => $this->configService->getProtocol(),
75+
"api_key" => $this->configService->getApiKey()
76+
]
77+
]
78+
]
79+
);
80+
81+
$this->typeSenseClient = $client;
82+
}
8583
return $this->typeSenseClient;
8684
}
8785
}

Model/Config/Source/TypeSenseIndexMethod.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
namespace Develo\Typesense\Model\Config\Source;
44

55
/**
6-
* Algolia custom sort order field
6+
* Typesense module indexation methods
77
*/
88
class TypeSenseIndexMethod implements \Magento\Framework\Data\OptionSourceInterface
99
{
10+
const METHOD_TYPESENSE = 'typesense';
11+
const METHOD_ALGOLIA = 'algolia';
12+
const METHOD_BOTH = 'typesense_algolia';
1013
/**
1114
* @var array
1215
*/
1316
private $methods = [
14-
'typesense' => 'Typesense Only',
15-
'typesense_algolia' => 'Both Typesense and Aglolia',
16-
'algolia' => 'Algolia Only'
17+
self::METHOD_TYPESENSE => 'Typesense Only',
18+
self::METHOD_BOTH => 'Both Typesense and Aglolia',
19+
self::METHOD_ALGOLIA => 'Algolia Only'
1720
];
1821

1922
/**
@@ -22,14 +25,12 @@ class TypeSenseIndexMethod implements \Magento\Framework\Data\OptionSourceInterf
2225
public function toOptionArray()
2326
{
2427
$options = [];
25-
2628
foreach ($this->methods as $key => $value) {
2729
$options[] = [
2830
'value' => $key,
2931
'label' => __($value),
3032
];
3133
}
32-
3334
return $options;
3435
}
3536
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Develo\Typesense\Model\Config\Source;
4+
5+
/**
6+
* Typesense protocols
7+
*/
8+
class TypeSenseProtocol implements \Magento\Framework\Data\OptionSourceInterface
9+
{
10+
const HTTP = 'http';
11+
const HTTPS = 'https';
12+
13+
/**
14+
* @var array
15+
*/
16+
private $protocols = [
17+
self::HTTP => 'HTTP',
18+
self::HTTPS => 'HTTPS',
19+
];
20+
21+
/**
22+
* @return array
23+
*/
24+
public function toOptionArray()
25+
{
26+
$options = [];
27+
foreach ($this->protocols as $key => $value) {
28+
$options[] = [
29+
'value' => $key,
30+
'label' => __($value),
31+
];
32+
}
33+
return $options;
34+
}
35+
}

Plugin/Backend/Algolia/AlgoliaSearch/Helper/AlgoliaHelper.php

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Develo\Typesense\Plugin\Backend\Algolia\AlgoliaSearch\Helper;
9+
10+
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
11+
use Develo\Typesense\Adapter\Client;
12+
use Develo\Typesense\Services\ConfigService;
13+
use Develo\Typesense\Model\Config\Source\TypeSenseIndexMethod;
14+
15+
class AlgoliaHelperPlugin
16+
{
17+
/**
18+
* @var ConfigService
19+
*/
20+
protected ConfigService $configService;
21+
22+
/**
23+
* @var Client $typesenseClient
24+
*/
25+
protected Client $typesenseClient;
26+
27+
/**
28+
* @param ConfigService $configService
29+
* @param Client $client
30+
*/
31+
public function __construct(
32+
ConfigService $configService,
33+
Client $client
34+
)
35+
{
36+
$this->configService = $configService;
37+
$this->typesenseClient = $client;
38+
}
39+
40+
/**
41+
* Indexes data if config is set todo, will index into algolia or typesense or both
42+
*/
43+
public function aroundAddObjects(
44+
\Algolia\AlgoliaSearch\Helper\AlgoliaHelper $subject,
45+
\Closure $proceed,
46+
$objects,
47+
$indexName
48+
)
49+
{
50+
if ($this->configService->isEnabled()) {
51+
$result = [];
52+
$indexMethod = $this->configService->getIndexMethod();
53+
switch ($indexMethod) {
54+
case TypeSenseIndexMethod::METHOD_ALGOLIA:
55+
$result = $proceed();
56+
break;
57+
case TypeSenseIndexMethod::METHOD_BOTH:
58+
$this->typesenseClient->addData($indexName, $objects);
59+
$result = $proceed();
60+
break;
61+
case TypeSenseIndexMethod::METHOD_TYPESENSE:
62+
default:
63+
$this->typesenseClient->addData($indexName, $objects);
64+
break;
65+
}
66+
} else {
67+
$result = $proceed();
68+
}
69+
return $result;
70+
}
71+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Composer install this module, it will include the Algolia module as a dependancy
4646
### Installation
4747

4848
```shell
49-
composer install develodesign/magento2-module-typesense
49+
composer require develodesign/magento2-module-typesense
5050
```
5151

5252
Add Typesene Configuration

0 commit comments

Comments
 (0)