1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Scout Extended. |
7
|
|
|
* |
8
|
|
|
* (c) Algolia Team <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Algolia\ScoutExtended\Repositories; |
15
|
|
|
|
16
|
|
|
use Algolia\AlgoliaSearch\Exceptions\NotFoundException; |
17
|
|
|
use Algolia\AlgoliaSearch\SearchClient; |
18
|
|
|
use Algolia\AlgoliaSearch\SearchIndex; |
19
|
|
|
use Algolia\ScoutExtended\Settings\Settings; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
final class RemoteSettingsRepository |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Settings that may be know by other names. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $aliases = [ |
32
|
|
|
'attributesToIndex' => 'searchableAttributes', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \Algolia\AlgoliaSearch\SearchClient |
37
|
|
|
*/ |
38
|
|
|
private $client; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $defaults; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* RemoteRepository constructor. |
47
|
|
|
* |
48
|
|
|
* @param \Algolia\AlgoliaSearch\SearchClient $client |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
14 |
|
public function __construct(SearchClient $client) |
53
|
|
|
{ |
54
|
14 |
|
$this->client = $client; |
55
|
14 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the default settings. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
12 |
|
public function defaults(): array |
63
|
|
|
{ |
64
|
12 |
|
if ($this->defaults === null) { |
65
|
12 |
|
$indexName = 'temp-laravel-scout-extended'; |
66
|
12 |
|
$index = $this->client->initIndex($indexName); |
67
|
12 |
|
$this->defaults = $this->getSettingsRaw($index); |
68
|
12 |
|
$index->delete(); |
69
|
|
|
} |
70
|
|
|
|
71
|
12 |
|
return $this->defaults; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Find the settings of the given Index. |
76
|
|
|
* |
77
|
|
|
* @param \Algolia\AlgoliaSearch\SearchIndex $index |
78
|
|
|
* |
79
|
|
|
* @return \Algolia\ScoutExtended\Settings\Settings |
80
|
|
|
*/ |
81
|
12 |
|
public function find(SearchIndex $index): Settings |
82
|
|
|
{ |
83
|
12 |
|
return new Settings($this->getSettingsRaw($index), $this->defaults()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \Algolia\AlgoliaSearch\SearchIndex $index |
88
|
|
|
* @param \Algolia\ScoutExtended\Settings\Settings $settings |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
3 |
|
public function save(SearchIndex $index, Settings $settings): void |
93
|
|
|
{ |
94
|
3 |
|
$index->setSettings($settings->compiled())->wait(); |
95
|
3 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \Algolia\AlgoliaSearch\SearchIndex $index |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
12 |
|
public function getSettingsRaw(SearchIndex $index): array |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
12 |
|
$settings = $index->getSettings(); |
106
|
|
|
} catch (NotFoundException $e) { |
107
|
|
|
$index->saveObject(['objectID' => 'temp'])->wait(); |
108
|
|
|
$settings = $index->getSettings(); |
109
|
|
|
|
110
|
|
|
$index->clearObjects(); |
111
|
|
|
} |
112
|
|
|
|
113
|
12 |
|
foreach (self::$aliases as $from => $to) { |
114
|
12 |
|
if (array_key_exists($from, $settings)) { |
115
|
|
|
$settings[$to] = $settings[$from]; |
116
|
12 |
|
unset($settings[$from]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
12 |
|
return $settings; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|