1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ichaber\SSSwiftype\Service; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use SilverStripe\Core\Extensible; |
7
|
|
|
use SilverStripe\Core\Injector\Injectable; |
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
9
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Credit: [Bernard Hamlin](https://github.com/blueo) and [Mojmir Fendek](https://github.com/mfendeksilverstripe) |
13
|
|
|
* |
14
|
|
|
* Class SwiftypeCredentials |
15
|
|
|
* |
16
|
|
|
* @package Ichaber\SSSwiftype\Service |
17
|
|
|
*/ |
18
|
|
|
class SwiftypeCredentials |
19
|
|
|
{ |
20
|
|
|
use Extensible; |
21
|
|
|
use Injectable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var LoggerInterface |
25
|
|
|
*/ |
26
|
|
|
private $logger; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $enabled = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
private $engineSlug; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string|null |
40
|
|
|
*/ |
41
|
|
|
private $domainID; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string|null |
45
|
|
|
*/ |
46
|
|
|
private $apiKey; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string|null |
50
|
|
|
*/ |
51
|
|
|
private $message; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function isEnabled(): bool |
57
|
|
|
{ |
58
|
|
|
return $this->enabled; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param bool|null $enabled |
63
|
|
|
*/ |
64
|
|
|
public function setEnabled(?bool $enabled): void |
65
|
|
|
{ |
66
|
|
|
$this->enabled = (bool) $enabled; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string|null |
71
|
|
|
*/ |
72
|
|
|
public function getEngineSlug(): ?string |
73
|
|
|
{ |
74
|
|
|
return $this->engineSlug; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string|null $engineSlug |
79
|
|
|
*/ |
80
|
|
|
public function setEngineSlug(?string $engineSlug): void |
81
|
|
|
{ |
82
|
|
|
$this->engineSlug = $engineSlug; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string|null |
87
|
|
|
*/ |
88
|
|
|
public function getDomainID(): ?string |
89
|
|
|
{ |
90
|
|
|
return $this->domainID; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string|null $domainID |
95
|
|
|
*/ |
96
|
|
|
public function setDomainID(?string $domainID): void |
97
|
|
|
{ |
98
|
|
|
$this->domainID = $domainID; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string|null |
103
|
|
|
*/ |
104
|
|
|
public function getAPIKey(): ?string |
105
|
|
|
{ |
106
|
|
|
return $this->apiKey; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string|null $apiKey |
111
|
|
|
*/ |
112
|
|
|
public function setAPIKey(?string $apiKey): void |
113
|
|
|
{ |
114
|
|
|
$this->apiKey = $apiKey; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string|null |
119
|
|
|
*/ |
120
|
|
|
public function getMessage(): ?string |
121
|
|
|
{ |
122
|
|
|
return $this->message; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $message |
127
|
|
|
*/ |
128
|
|
|
public function setMessage(string $message): void |
129
|
|
|
{ |
130
|
|
|
$this->message = $message; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Gets the Swiftype credentials |
135
|
|
|
* |
136
|
|
|
* @param mixed|null $additionalData If set, we assume that you want to populate your Credentials through extension |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function __construct($additionalData = null) |
140
|
|
|
{ |
141
|
|
|
if ($additionalData !== null) { |
142
|
|
|
// You've supplied the class with $additionalData, so, please populate your Credentials data through the |
143
|
|
|
// populateCredentials extensions point. It will have access to your $additionalData also |
144
|
|
|
$this->invokeWithExtensions('populateCredentials', $additionalData); |
145
|
|
|
} else { |
146
|
|
|
// Default functionality is to grab Credentials from SiteConfig |
147
|
|
|
/** @var SiteConfig $config */ |
148
|
|
|
$config = SiteConfig::current_site_config(); |
149
|
|
|
|
150
|
|
|
// You might want to implement this via Environment variables or something. Just make sure SiteConfig has |
151
|
|
|
// access to that variable, and return it here |
152
|
|
|
$this->setEnabled((bool) $config->relField('SwiftypeEnabled')); |
153
|
|
|
|
154
|
|
|
// If you have multiple Engines per site (maybe you use Fluent with a different Engine on each Locale), then |
155
|
|
|
// this provides some basic ability to have different credentials returned based on the application state |
156
|
|
|
$this->setEngineSlug($config->relField('SwiftypeEngineSlug')); |
157
|
|
|
$this->setDomainID($config->relField('SwiftypeDomainID')); |
158
|
|
|
$this->setAPIKey($config->relField('SwiftypeAPIKey')); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (!$this->isEnabled()) { |
162
|
|
|
$this->disable( |
163
|
|
|
'Swiftype is disabled. It can be enabled under Settings > Swiftype Search' |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (!$this->getEngineSlug()) { |
170
|
|
|
$this->disable( |
171
|
|
|
'Swiftype Engine Slug value has not been set. Settings > Swiftype Search > Swiftype Engine Slug' |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (!$this->getDomainID()) { |
178
|
|
|
$this->disable( |
179
|
|
|
'Swiftype Domain ID has not been set. Settings > Swiftype Search > Swiftype Domain ID' |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
return; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (!$this->getAPIKey()) { |
186
|
|
|
$this->disable( |
187
|
|
|
'Swiftype API Key has not been set. Settings > Swiftype Search > Swiftype Production API Key' |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
return; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $message |
196
|
|
|
*/ |
197
|
|
|
protected function disable(string $message): void |
198
|
|
|
{ |
199
|
|
|
$trace = debug_backtrace(); |
200
|
|
|
|
201
|
|
|
// array_shift for adding context (for RaygunHandler) by using the last item on the stack trace. |
202
|
|
|
$this->getLogger()->warning($message, array_shift($trace)); |
203
|
|
|
|
204
|
|
|
$this->setMessage($message); |
205
|
|
|
$this->setEnabled(false); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return LoggerInterface |
210
|
|
|
*/ |
211
|
|
|
protected function getLogger(): LoggerInterface |
212
|
|
|
{ |
213
|
|
|
if (!$this->logger) { |
214
|
|
|
$this->logger = Injector::inst()->get(LoggerInterface::class); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this->logger; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|