1
|
|
|
<?php namespace Cornford\Pokenotifier; |
2
|
|
|
|
3
|
|
|
use Cornford\Pokenotifier\Contracts\NotifyingBaseInterface; |
4
|
|
|
use Cornford\Pokenotifier\Exceptions\NotifierException; |
5
|
|
|
use DirectoryIterator; |
6
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
7
|
|
|
use Maknz\Slack\Client as SlackClient; |
8
|
|
|
|
9
|
|
|
abstract class NotifierBase implements NotifyingBaseInterface |
10
|
|
|
{ |
11
|
|
|
const REQUEST_TIMEOUT = 120; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Slack Client. |
15
|
|
|
* |
16
|
|
|
* @var SlackClient |
17
|
|
|
*/ |
18
|
|
|
private $slackClient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Guzzle Client. |
22
|
|
|
* |
23
|
|
|
* @var GuzzleClient |
24
|
|
|
*/ |
25
|
|
|
private $guzzleClient; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Directory Iterator. |
29
|
|
|
* |
30
|
|
|
* @var DirectoryIterator |
31
|
|
|
*/ |
32
|
|
|
private $directoryIterator; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Application configuration. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $applicationConfiguration; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Pokemon configuration. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $pokemonConfiguration; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor. |
50
|
|
|
* |
51
|
|
|
* @param GuzzleClient $guzzleClient |
52
|
|
|
* @param SlackClient $slackClient |
53
|
|
|
* @param DirectoryIterator $directoryIterator |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
GuzzleClient $guzzleClient = null, |
57
|
|
|
SlackClient $slackClient = null, |
58
|
|
|
DirectoryIterator $directoryIterator = null |
59
|
|
|
) { |
60
|
|
|
$this->loadApplicationConfiguration(); |
61
|
|
|
$this->loadPokemonConfiguration(); |
62
|
|
|
|
63
|
|
|
if ($guzzleClient === null) { |
64
|
|
|
$this->createGuzzleClient(); |
65
|
|
|
} else { |
66
|
|
|
$this->setGuzzleClient($guzzleClient); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($slackClient === null) { |
70
|
|
|
$this->createSlackClient(); |
71
|
|
|
} else { |
72
|
|
|
$this->setSlackClient($slackClient); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($directoryIterator === null) { |
76
|
|
|
$this->createDirectoryIterator(); |
77
|
|
|
} else { |
78
|
|
|
$this->setDirectoryIterator($directoryIterator); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Load Application configuration array. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function loadApplicationConfiguration() |
88
|
|
|
{ |
89
|
|
|
$this->applicationConfiguration = $this->getConfigurationFile( __DIR__ . '/../../config/application.php'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get Application configuration array. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getApplicationConfiguration() |
98
|
|
|
{ |
99
|
|
|
return $this->applicationConfiguration; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get Pokemon configuration array. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function loadPokemonConfiguration() |
108
|
|
|
{ |
109
|
|
|
$this->pokemonConfiguration = $this->getConfigurationFile(__DIR__ . '/../../config/pokemon.php'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get Pokemon configuration array. |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getPokemonConfiguration() |
118
|
|
|
{ |
119
|
|
|
return $this->pokemonConfiguration; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get configuration file. |
124
|
|
|
* |
125
|
|
|
* @param string $filepath |
126
|
|
|
* |
127
|
|
|
* @throws NotifierException |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
private function getConfigurationFile($filepath) |
132
|
|
|
{ |
133
|
|
|
if (!is_file($filepath)) { |
134
|
|
|
throw new NotifierException('Unable to locate configuration file: ' . $filepath); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return include $filepath; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Create Guzzle client. |
142
|
|
|
* |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
public function createGuzzleClient() |
146
|
|
|
{ |
147
|
|
|
$configuration = $this->getApplicationConfiguration(); |
148
|
|
|
|
149
|
|
|
$this->setGuzzleClient( |
150
|
|
|
new GuzzleClient([ |
151
|
|
|
'base_url' => sprintf('%s://%s:%s', $configuration['pokemongo-protocol'], $configuration['pokemongo-ip'], $configuration['pokemongo-port']), |
152
|
|
|
'defaults' => [ |
153
|
|
|
'connect_timeout' => self::REQUEST_TIMEOUT, |
154
|
|
|
'timeout' => self::REQUEST_TIMEOUT, |
155
|
|
|
] |
156
|
|
|
]) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get Guzzle client. |
162
|
|
|
* |
163
|
|
|
* @return GuzzleClient |
164
|
|
|
*/ |
165
|
|
|
public function getGuzzleClient() |
166
|
|
|
{ |
167
|
|
|
return $this->guzzleClient; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Set Guzzle client. |
172
|
|
|
* |
173
|
|
|
* @param GuzzleClient $class |
174
|
|
|
* |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
public function setGuzzleClient(GuzzleClient $class) |
178
|
|
|
{ |
179
|
|
|
$this->guzzleClient = $class; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Create Slack client. |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function createSlackClient() |
188
|
|
|
{ |
189
|
|
|
$configuration = $this->getApplicationConfiguration(); |
190
|
|
|
|
191
|
|
|
$this->setSlackClient(new SlackClient($configuration['slack-webhook'], [], $this->getGuzzleClient())); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get Slack client. |
196
|
|
|
* |
197
|
|
|
* @return SlackClient |
198
|
|
|
*/ |
199
|
|
|
public function getSlackClient() |
200
|
|
|
{ |
201
|
|
|
return $this->slackClient; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set Slack client. |
206
|
|
|
* |
207
|
|
|
* @param SlackClient $class |
208
|
|
|
* |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
public function setSlackClient(SlackClient $class) |
212
|
|
|
{ |
213
|
|
|
$this->slackClient = $class; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Create Directory Iterator. |
218
|
|
|
* |
219
|
|
|
* @return DirectoryIterator |
220
|
|
|
*/ |
221
|
|
|
public function createDirectoryIterator() |
222
|
|
|
{ |
223
|
|
|
$configuration = $this->getApplicationConfiguration(); |
224
|
|
|
|
225
|
|
|
$this->setDirectoryIterator(new DirectoryIterator($configuration['cache-directory'])); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get Directory Iterator. |
230
|
|
|
* |
231
|
|
|
* @return DirectoryIterator |
232
|
|
|
*/ |
233
|
|
|
public function getDirectoryIterator() |
234
|
|
|
{ |
235
|
|
|
return $this->directoryIterator; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set Directory Iterator. |
240
|
|
|
* |
241
|
|
|
* @param DirectoryIterator $class |
242
|
|
|
* |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
public function setDirectoryIterator(DirectoryIterator $class) |
246
|
|
|
{ |
247
|
|
|
$this->directoryIterator = $class; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
} |