Passed
Push — master ( 00795d...64ec34 )
by Robert
08:15
created

LiveEngageLaravel::limit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LivePersonInc\LiveEngageLaravel;
4
5
use Carbon\Carbon;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Subscriber\Oauth\Oauth1;
9
use LivePersonInc\LiveEngageLaravel\Models\Info;
10
use LivePersonInc\LiveEngageLaravel\Models\MetaData;
11
use LivePersonInc\LiveEngageLaravel\Models\AccountStatus;
12
use LivePersonInc\LiveEngageLaravel\Models\MessagingInfo;
13
use LivePersonInc\LiveEngageLaravel\Models\Payload;
14
use LivePersonInc\LiveEngageLaravel\Models\Visitor;
15
use LivePersonInc\LiveEngageLaravel\Models\Agent;
16
use LivePersonInc\LiveEngageLaravel\Models\Campaign;
17
use LivePersonInc\LiveEngageLaravel\Models\Engagement;
18
use LivePersonInc\LiveEngageLaravel\Models\Conversation;
19
use LivePersonInc\LiveEngageLaravel\Collections\EngagementHistory;
20
use LivePersonInc\LiveEngageLaravel\Collections\AgentParticipants;
21
use LivePersonInc\LiveEngageLaravel\Exceptions\LiveEngageException;
22
use LivePersonInc\LiveEngageLaravel\Collections\ConversationHistory;
23
24
class LiveEngageLaravel
25
{
26
	private $account = false;
27
	private $results = [];
28
	private $skills = [];
29
	private $next = false;
30
	private $prev = false;
31
	private $start;
32
	private $end;
33
	private $config = 'services.liveperson.default';
34
	private $version = '1.0';
35
	private $history_limit = 50;
36
	private $history = false;
37
	private $context = 'interactionHistoryRecords';
38
	private $interactive = true;
39
	private $ended = true;
40
	private $bearer = false;
41
42
	private $domain = false;
43
44
	private $retry_limit = 5;
45
	private $retry_counter = 0;
46
47 5
	public function __get($attribute)
48
	{
49 5
		return $this->$attribute;
50
	}
51
52 8
	public function __construct()
53
	{
54 8
		$this->account = config("{$this->config}.account");
55
		//$this->domain = config("{$this->config}.domain");
56 8
		$this->version = config("{$this->config}.version") ?: $this->version;
57 8
	}
58
59 1
	public function key($key = 'default')
60
	{
61 1
		$this->config = "services.liveperson.$key";
62 1
		$this->__construct();
63
64 1
		return $this;
65
	}
66
	
67 1
	public function nonInteractive()
68
	{
69 1
		$this->interactive = false;
70 1
		return $this;
71
	}
72
	
73 1
	public function active()
74
	{
75 1
		$this->ended = false;
76 1
		return $this;
77
	}
78
79 1
	public function limit($limit)
80
	{
81 1
		$this->history_limit = $limit;
82
83 1
		return $this;
84
	}
85
86 1
	public function skills($skills)
87
	{
88 1
		$this->skills = $skills;
89
90 1
		return $this;
91
	}
92
93 1
	public function retry($limit)
94
	{
95 1
		$this->retry_limit = $limit;
96
97 1
		return $this;
98
	}
99
100 1
	public function account($accountid)
101
	{
102 1
		$this->account = $accountid;
103
104 1
		return $this;
105
	}
106
107 1
	public function domain($service)
108
	{
109 1
		$response = $this->requestV1("https://api.liveperson.net/api/account/{$this->account}/service/{$service}/baseURI.json?version={$this->version}", 'GET');
110
		
111 1
		$this->domain = $response->baseURI;
112
113 1
		return $this;
114
	}
115
116
	public function visitor($visitorID, $sessionID, $setData = false)
117
	{
118
		$this->domain('smt');
119
120
		if ($setData) {
121
			$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/events?v=1&sid={$sessionID}";
122
123
			return $this->requestV1($url, 'POST', $setData);
124
		} else {
125
			$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/state?v=1&sid={$sessionID}";
126
127
			return $this->requestV1($url, 'GET');
128
		}
129
	}
130
131 1
	final public function retrieveHistory(Carbon $start, Carbon $end, $url = false)
132
	{
133 1
		$this->domain('engHistDomain');
134
135 1
		$url = $url ?: "https://{$this->domain}/interaction_history/api/account/{$this->account}/interactions/search?limit={$this->history_limit}&offset=0";
136
137 1
		$start_str = $start->toW3cString();
138 1
		$end_str = $end->toW3cString();
139
140 1
		$data = new Payload([
141 1
			'interactive' => $this->interactive,
142 1
			'ended' => $this->ended,
143
			'start' => [
144 1
				'from' => strtotime($start_str) . '000',
145 1
				'to' => strtotime($end_str) . '000',
146
			],
147 1
			'skillIds' => $this->skills
148
		]);
149
150 1
		return $this->requestV1($url, 'POST', $data);
151
	}
152
153 1
	final public function retrieveMsgHistory(Carbon $start, Carbon $end, $url = false)
154
	{
155 1
		$this->domain('msgHist');
156
157 1
		$url = $url ?: "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/search?limit={$this->history_limit}&offset=0&sort=start:desc";
158
159 1
		$start_str = $start->toW3cString();
160 1
		$end_str = $end->toW3cString();
161
162 1
		$data = new Payload([
163 1
			'status' => $this->ended ? ['CLOSE'] : ['OPEN', 'CLOSE'],
164
			'start' => [
165 1
				'from' => strtotime($start_str) . '000',
166 1
				'to' => strtotime($end_str) . '000',
167
			],
168 1
			'skillIds' => $this->skills
169
		]);
170
		
171 1
		return $this->requestV1($url, 'POST', $data);
172
	}
173
	
174 1
	public function getAgentStatus($skills)
175
	{
176 1
		$skills = is_array($skills) ? $skills : [$skills];
177
	
178 1
		$this->domain('msgHist');
179
		
180 1
		$url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/agent-view/status";
181
		
182 1
		$data = ['skillIds' => $skills];
183
		
184 1
		$response = $this->requestV1($url, 'POST', $data);
185 1
		$collection = new AgentParticipants($response->agentStatusRecords);
186 1
		$collection->metaData = new MetaData((array) $response->_metadata);
187
		
188 1
		return $collection;
189
		
190
	}
191
	
192 1
	public function messagingHistory(Carbon $start = null, Carbon $end = null)
193
	{
194 1
		$this->retry_counter = 0;
195
196 1
		$start = $start ?: (new Carbon())->today();
197 1
		$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59);
198
199 1
		$results_object = $this->retrieveMsgHistory($start, $end);
200
		
201 1
		$results_object->_metadata->start = $start;
202 1
		$results_object->_metadata->end = $end;
203
	
204 1
		$meta = new MetaData((array) $results_object->_metadata);
205
		
206 1
		$collection = new ConversationHistory($results_object->conversationHistoryRecords);
207 1
		$collection->metaData = $meta;
208
		
209 1
		return $collection;
210
			
211
	}
212
213 1
	public function history(Carbon $start = null, Carbon $end = null)
214
	{
215 1
		$this->retry_counter = 0;
216
217 1
		$start = $start ?: (new Carbon())->today();
218 1
		$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59);
219
220 1
		$results_object = $this->retrieveHistory($start, $end);
221
		
222 1
		$results_object->_metadata->start = $start;
223 1
		$results_object->_metadata->end = $end;
224
	
225 1
		$meta = new MetaData((array) $results_object->_metadata);
226
		
227 1
		$collection = new EngagementHistory($results_object->interactionHistoryRecords);
228 1
		$collection->metaData = $meta;
229
		
230 1
		return $collection;
231
			
232
	}
233
	
234 1
	public function status()
235
	{
236 1
		$url = "https://status.liveperson.com/json?site={$this->account}";
237
		
238 1
		$response = $this->requestV1($url, 'GET');
239
		
240 1
		return new AccountStatus((array) $response);
241
	}
242
	
243
	public function login()
244
	{
245
		$this->domain('agentVep');
246
		
247
		$consumer_key = config("{$this->config}.key");
248
		$consumer_secret = config("{$this->config}.secret");
249
		$token = config("{$this->config}.token");
250
		$secret = config("{$this->config}.token_secret");
251
		$username = config("{$this->config}.user_name");
252
		
253
		$auth = [
254
			'username'		  => $username,
255
			'appKey'			=> $consumer_key,
256
			'secret'			=> $consumer_secret,
257
			'accessToken'		=> $token,
258
			'accessTokenSecret' => $secret,
259
		];
260
		
261
		$url = "https://{$this->domain}/api/account/{$this->account}/login?v=1.3";
262
		
263
		$response = $this->requestV1($url, 'POST', $auth);
264
		
265
		$this->bearer = $response->bearer;
266
		
267
		return $this;
268
	}
269
	
270
	private function requestV2($url, $method, $payload = false)
0 ignored issues
show
Unused Code introduced by
The method requestV2() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
271
	{
272
		$this->login();
273
		
274
		$client = new Client();
275
		$args = [
276
			'headers' => [
277
				'content-type' => 'application/json',
278
			],
279
			'body' => json_encode($payload)
280
		];
281
		
282
		try {
283
			$res = $client->request($method, $url, $args);
284
		} catch (\Exception $e) {
285
			throw $e;
286
		} 
287
		
288
		return json_decode($res->getBody());
289
	}
290
	
291 2
	private function requestClient()
292
	{
293 2
		$consumer_key = config("{$this->config}.key");
294 2
		$consumer_secret = config("{$this->config}.secret");
295 2
		$token = config("{$this->config}.token");
296 2
		$secret = config("{$this->config}.token_secret");
297
298 2
		$stack = HandlerStack::create();
299 2
		$auth = new Oauth1([
300 2
			'consumer_key'	=> $consumer_key,
301 2
			'consumer_secret' => $consumer_secret,
302 2
			'token'		   => $token,
303 2
			'token_secret'	=> $secret,
304 2
			'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC,
305
		]);
306 2
		$stack->push($auth);
307
308 2
		$client = new Client([
309 2
			'handler' => $stack,
310
		]);
311
		
312 2
		return $client;
313
	}
314
	
315 1
	private function requestV1($url, $method, $payload = [])
316
	{
317 1
		$client = $this->requestClient();
318
319
		$args = [
320 1
			'auth' => 'oauth',
321
			'headers' => [
322
				'content-type' => 'application/json',
323
			],
324 1
			'body' => json_encode($payload)
325
		];
326
327
		try {
328 1
			$res = $client->request($method, $url, $args);
329 1
			$response = json_decode($res->getBody());
330
		} catch (\Exception $e) {
331
			if ($this->retry_counter < $this->retry_limit || $this->retry_limit == -1) {
332
				usleep(1500);
333
				$this->retry_counter++;
334
				$response = $this->requestV1($url, $payload);
335
			} else {
336
				throw $e; //new LiveEngageException("Retry limit has been exceeded ($this->retry_limit)", 100);
337
			}
338
		}
339
340 1
		return $response;
341
	}
342
}
343