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 LogicException; |
17
|
|
|
use Illuminate\Support\Str; |
18
|
|
|
use Algolia\AlgoliaSearch\Index; |
19
|
|
|
use Algolia\ScoutExtended\Repositories\UserDataRepository; |
20
|
|
|
use Algolia\ScoutExtended\Repositories\LocalSettingsRepository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @internal |
24
|
|
|
*/ |
25
|
|
|
final class Status |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \Algolia\ScoutExtended\Settings\Encrypter |
29
|
|
|
*/ |
30
|
|
|
private $encrypter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Algolia\ScoutExtended\Repositories\UserDataRepository |
34
|
|
|
*/ |
35
|
|
|
private $userDataRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Algolia\ScoutExtended\Repositories\LocalSettingsRepository |
39
|
|
|
*/ |
40
|
|
|
private $localRepository; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Algolia\ScoutExtended\Settings\Settings |
44
|
|
|
*/ |
45
|
|
|
private $remoteSettings; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Algolia\AlgoliaSearch\Index |
49
|
|
|
*/ |
50
|
|
|
private $index; |
51
|
|
|
|
52
|
|
|
public const LOCAL_NOT_FOUND = 'localNotFound'; |
53
|
|
|
|
54
|
|
|
public const REMOTE_NOT_FOUND = 'remoteNotFound'; |
55
|
|
|
|
56
|
|
|
public const BOTH_ARE_EQUAL = 'bothAreEqual'; |
57
|
|
|
|
58
|
|
|
public const LOCAL_GOT_UPDATED = 'localGotUpdated'; |
59
|
|
|
|
60
|
|
|
public const REMOTE_GOT_UPDATED = 'remoteGotUpdated'; |
61
|
|
|
|
62
|
|
|
public const BOTH_GOT_UPDATED = 'bothGotUpdated'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Status constructor. |
66
|
|
|
* |
67
|
|
|
* @param \Algolia\ScoutExtended\Repositories\LocalSettingsRepository $localRepository |
68
|
|
|
* @param \Algolia\ScoutExtended\Settings\Encrypter $encrypter |
69
|
|
|
* @param \Algolia\ScoutExtended\Settings\Settings $remoteSettings |
70
|
|
|
* @param \Algolia\AlgoliaSearch\Index $index |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
3 |
|
public function __construct( |
75
|
|
|
LocalSettingsRepository $localRepository, |
76
|
|
|
UserDataRepository $userDataRepository, |
77
|
|
|
Encrypter $encrypter, |
78
|
|
|
Settings $remoteSettings, |
79
|
|
|
Index $index |
80
|
|
|
) { |
81
|
3 |
|
$this->encrypter = $encrypter; |
82
|
3 |
|
$this->localRepository = $localRepository; |
83
|
3 |
|
$this->userDataRepository = $userDataRepository; |
84
|
3 |
|
$this->remoteSettings = $remoteSettings; |
85
|
3 |
|
$this->index = $index; |
86
|
3 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
3 |
|
public function localNotFound(): bool |
92
|
|
|
{ |
93
|
3 |
|
return ! $this->localRepository->exists($this->index); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
2 |
|
public function remoteNotFound(): bool |
100
|
|
|
{ |
101
|
2 |
|
return empty($this->userDataRepository->getSettingsHash($this->index)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
1 |
|
public function bothAreEqual(): bool |
108
|
|
|
{ |
109
|
1 |
|
return $this->encrypter->encrypt($this->localRepository->find($this->index)) === |
110
|
1 |
|
$this->userDataRepository->getSettingsHash($this->index) && |
111
|
1 |
|
$this->encrypter->encrypt($this->remoteSettings) === $this->userDataRepository->getSettingsHash($this->index); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function localGotUpdated(): bool |
118
|
|
|
{ |
119
|
|
|
return $this->encrypter->encrypt($this->localRepository->find($this->index)) !== |
120
|
|
|
$this->userDataRepository->getSettingsHash($this->index) && |
121
|
|
|
$this->encrypter->encrypt($this->remoteSettings) === $this->userDataRepository->getSettingsHash($this->index); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function remoteGotUpdated(): bool |
128
|
|
|
{ |
129
|
|
|
return $this->encrypter->encrypt($this->localRepository->find($this->index)) === |
130
|
|
|
$this->userDataRepository->getSettingsHash($this->index) && |
131
|
|
|
$this->encrypter->encrypt($this->remoteSettings) !== $this->userDataRepository->getSettingsHash($this->index); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function bothGotUpdated(): bool |
138
|
|
|
{ |
139
|
|
|
return $this->encrypter->encrypt($this->localRepository->find($this->index)) !== |
140
|
|
|
$this->userDataRepository->getSettingsHash($this->index) && |
141
|
|
|
$this->encrypter->encrypt($this->remoteSettings) !== $this->userDataRepository->getSettingsHash($this->index); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the current state. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
3 |
|
public function toString(): string |
150
|
|
|
{ |
151
|
|
|
$methods = [ |
152
|
3 |
|
self::LOCAL_NOT_FOUND, |
153
|
3 |
|
self::REMOTE_NOT_FOUND, |
154
|
3 |
|
self::BOTH_ARE_EQUAL, |
155
|
3 |
|
self::LOCAL_GOT_UPDATED, |
156
|
3 |
|
self::REMOTE_GOT_UPDATED, |
157
|
3 |
|
self::BOTH_GOT_UPDATED, |
158
|
|
|
]; |
159
|
|
|
|
160
|
3 |
|
foreach ($methods as $method) { |
161
|
3 |
|
if ($this->{$method}()) { |
162
|
3 |
|
return $method; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
throw new LogicException('This should not happen'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get a human description of the current status. |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
1 |
|
public function toHumanString(): string |
175
|
|
|
{ |
176
|
1 |
|
$string = Str::snake($this->toString()); |
177
|
|
|
|
178
|
1 |
|
return Str::ucfirst(str_replace('_', ' ', $string)); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|