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\Settings; |
15
|
|
|
|
16
|
|
|
use Algolia\AlgoliaSearch\Index; |
17
|
|
|
use Algolia\ScoutExtended\Repositories\UserDataRepository; |
18
|
|
|
use Algolia\ScoutExtended\Repositories\LocalSettingsRepository; |
19
|
|
|
use Algolia\ScoutExtended\Repositories\RemoteSettingsRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
class Synchronizer |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \Algolia\ScoutExtended\Settings\Compiler |
28
|
|
|
*/ |
29
|
|
|
private $compiler; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Algolia\ScoutExtended\Settings\Encrypter |
33
|
|
|
*/ |
34
|
|
|
private $encrypter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Algolia\ScoutExtended\Repositories\LocalSettingsRepository |
38
|
|
|
*/ |
39
|
|
|
private $localRepository; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository |
43
|
|
|
*/ |
44
|
|
|
private $remoteRepository; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \Algolia\ScoutExtended\Repositories\UserDataRepository |
48
|
|
|
*/ |
49
|
|
|
private $userDataRepository; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Synchronizer constructor. |
53
|
|
|
* |
54
|
|
|
* @param \Algolia\ScoutExtended\Settings\Compiler $compiler |
55
|
|
|
* @param \Algolia\ScoutExtended\Settings\Encrypter $encrypter |
56
|
|
|
* @param \Algolia\ScoutExtended\Repositories\LocalSettingsRepository $localRepository |
57
|
|
|
* @param \Algolia\ScoutExtended\Repositories\RemoteSettingsRepository $remoteRepository |
58
|
|
|
* @param \Algolia\ScoutExtended\Repositories\UserDataRepository $userDataRepository |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
3 |
|
public function __construct( |
63
|
|
|
Compiler $compiler, |
64
|
|
|
Encrypter $encrypter, |
65
|
|
|
LocalSettingsRepository $localRepository, |
66
|
|
|
RemoteSettingsRepository $remoteRepository, |
67
|
|
|
UserDataRepository $userDataRepository |
68
|
|
|
) { |
69
|
3 |
|
$this->compiler = $compiler; |
70
|
3 |
|
$this->encrypter = $encrypter; |
71
|
3 |
|
$this->localRepository = $localRepository; |
72
|
3 |
|
$this->remoteRepository = $remoteRepository; |
73
|
3 |
|
$this->userDataRepository = $userDataRepository; |
74
|
3 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Analyses the settings of the given index. |
78
|
|
|
* |
79
|
|
|
* @param \Algolia\AlgoliaSearch\Index $index |
80
|
|
|
* |
81
|
|
|
* @return \Algolia\ScoutExtended\Settings\Status |
82
|
|
|
*/ |
83
|
3 |
|
public function analyse(Index $index): Status |
84
|
|
|
{ |
85
|
3 |
|
$remoteSettings = $this->remoteRepository->find($index); |
86
|
|
|
|
87
|
3 |
|
return new Status($this->localRepository, $this->encrypter, $remoteSettings, $index); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Downloads the settings of the given index. |
92
|
|
|
* |
93
|
|
|
* @param \Algolia\AlgoliaSearch\Index $index |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
1 |
|
public function download(Index $index): void |
98
|
|
|
{ |
99
|
1 |
|
$settings = $this->remoteRepository->find($index); |
100
|
|
|
|
101
|
1 |
|
$path = $this->localRepository->getPath($index); |
102
|
|
|
|
103
|
1 |
|
$this->compiler->compile($settings, $path); |
104
|
|
|
|
105
|
|
|
$settingsHash = $this->encrypter->encrypt($settings); |
106
|
|
|
|
107
|
|
|
$this->userDataRepository->save($index, ['settingsHash' => $settingsHash]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Uploads the settings of the given index. |
112
|
|
|
* |
113
|
|
|
* @param \Algolia\AlgoliaSearch\Index $index |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function upload(Index $index): void |
118
|
|
|
{ |
119
|
|
|
$settings = $this->localRepository->find($index); |
120
|
|
|
|
121
|
|
|
$settingsHash = $this->encrypter->encrypt($settings); |
122
|
|
|
|
123
|
|
|
$this->userDataRepository->save($index, ['settingsHash' => $settingsHash]); |
124
|
|
|
$this->remoteRepository->save($index, $settings); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|