Passed
Push — master ( 8652e1...ae54b4 )
by Robert
03:22
created

LiveEngageLaravel   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Test Coverage

Coverage 70.13%

Importance

Changes 0
Metric Value
wmc 39
dl 0
loc 327
ccs 108
cts 154
cp 0.7013
rs 8.2857
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 3 1
A __set() 0 3 1
A __construct() 0 5 2
B login() 0 25 1
A skills() 0 5 1
A retry() 0 5 1
B requestV2() 0 24 4
A limit() 0 5 1
A nonInteractive() 0 4 1
B history() 0 23 4
A domain() 0 7 1
A retrieveMsgHistory() 0 19 3
A key() 0 5 1
B requestV1() 0 26 4
B messagingHistory() 0 23 4
A account() 0 5 1
A visitor() 0 12 2
A getAgentStatus() 0 15 2
A requestClient() 0 22 1
A active() 0 4 1
A retrieveHistory() 0 20 2
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\MessagingInfo;
12
use LivePersonInc\LiveEngageLaravel\Models\Payload;
13
use LivePersonInc\LiveEngageLaravel\Models\Visitor;
14
use LivePersonInc\LiveEngageLaravel\Models\Agent;
15
use LivePersonInc\LiveEngageLaravel\Models\Campaign;
16
use LivePersonInc\LiveEngageLaravel\Models\Engagement;
17
use LivePersonInc\LiveEngageLaravel\Models\Conversation;
18
use LivePersonInc\LiveEngageLaravel\Collections\EngagementHistory;
19
use LivePersonInc\LiveEngageLaravel\Collections\AgentParticipants;
20
use LivePersonInc\LiveEngageLaravel\Exceptions\LiveEngageException;
21
use LivePersonInc\LiveEngageLaravel\Collections\ConversationHistory;
22
23
class LiveEngageLaravel
24
{
25
	private $account = false;
26
	private $results = [];
27
	private $skills = [];
28
	private $next = false;
29
	private $prev = false;
30
	private $start;
31
	private $end;
32
	private $config = 'services.liveperson.default';
33
	private $version = '1.0';
34
	private $history_limit = 50;
35
	private $history = false;
36
	private $context = 'interactionHistoryRecords';
37
	private $interactive = true;
38
	private $ended = true;
39
	private $bearer = false;
40
41
	private $domain = false;
42
43
	private $retry_limit = 5;
44
	private $retry_counter = 0;
45
46
	public function __get($attribute)
47
	{
48
		return $this->$attribute;
49
	}
50
51
	public function __set($attribute, $value)
52
	{
53
		return $this->$attribute = $value;
54
	}
55
56 7
	public function __construct()
57
	{
58 7
		$this->account = config("{$this->config}.account");
59
		//$this->domain = config("{$this->config}.domain");
60 7
		$this->version = config("{$this->config}.version") ?: $this->version;
61 7
	}
62
63 1
	public function key($key = 'default')
64
	{
65 1
		$this->config = "services.liveperson.$key";
66
67 1
		return $this;
68
	}
69
	
70 1
	public function nonInteractive()
71
	{
72 1
		$this->interactive = false;
73 1
		return $this;
74
	}
75
	
76 1
	public function active()
77
	{
78 1
		$this->ended = false;
79 1
		return $this;
80
	}
81
82 1
	public function limit($limit)
83
	{
84 1
		$this->history_limit = $limit;
85
86 1
		return $this;
87
	}
88
89 1
	public function skills($skills)
90
	{
91 1
		$this->skills = $skills;
92
93 1
		return $this;
94
	}
95
96 1
	public function retry($limit)
97
	{
98 1
		$this->retry_limit = $limit;
99
100 1
		return $this;
101
	}
102
103 1
	public function account($accountid)
104
	{
105 1
		$this->account = $accountid;
106
107 1
		return $this;
108
	}
109
110 1
	public function domain($service)
111
	{
112 1
		$response = $this->requestV1("https://api.liveperson.net/api/account/{$this->account}/service/{$service}/baseURI.json?version={$this->version}", 'GET');
113
		
114 1
		$this->domain = $response->baseURI;
115
116 1
		return $this;
117
	}
118
119
	public function visitor($visitorID, $sessionID, $setData = false)
120
	{
121
		$this->domain('smt');
122
123
		if ($setData) {
124
			$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/events?v=1&sid={$sessionID}";
125
126
			return $this->requestV1($url, 'POST', $setData);
127
		} else {
128
			$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/state?v=1&sid={$sessionID}";
129
130
			return $this->requestV1($url, 'GET');
131
		}
132
	}
133
134 1
	final public function retrieveHistory(Carbon $start, Carbon $end, $url = false)
135
	{
136 1
		$this->domain('engHistDomain');
137
138 1
		$url = $url ?: "https://{$this->domain}/interaction_history/api/account/{$this->account}/interactions/search?limit={$this->history_limit}&offset=0";
139
140 1
		$start_str = $start->toW3cString();
141 1
		$end_str = $end->toW3cString();
142
143 1
		$data = new Payload([
144 1
			'interactive' => $this->interactive,
145 1
			'ended' => $this->ended,
146
			'start' => [
147 1
				'from' => strtotime($start_str) . '000',
148 1
				'to' => strtotime($end_str) . '000',
149
			],
150 1
			'skillIds' => $this->skills
151
		]);
152
153 1
		return $this->requestV1($url, 'POST', $data);
154
	}
155
156 1
	final public function retrieveMsgHistory(Carbon $start, Carbon $end, $url = false)
157
	{
158 1
		$this->domain('msgHist');
159
160 1
		$url = $url ?: "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/search?limit={$this->history_limit}&offset=0&sort=start:desc";
161
162 1
		$start_str = $start->toW3cString();
163 1
		$end_str = $end->toW3cString();
164
165 1
		$data = new Payload([
166 1
			'status' => $this->ended ? ['CLOSE'] : ['OPEN', 'CLOSE'],
167
			'start' => [
168 1
				'from' => strtotime($start_str) . '000',
169 1
				'to' => strtotime($end_str) . '000',
170
			],
171 1
			'skillIds' => $this->skills
172
		]);
173
		
174 1
		return $this->requestV1($url, 'POST', $data);
175
	}
176
	
177 1
	public function getAgentStatus($skills)
178
	{
179 1
		$skills = is_array($skills) ? $skills : [$skills];
180
	
181 1
		$this->domain('msgHist');
182
		
183 1
		$url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/agent-view/status";
184
		
185 1
		$data = ['skillIds' => $skills];
186
		
187 1
		$response = $this->requestV1($url, 'POST', $data);
188 1
		$collection = new AgentParticipants($response->agentStatusRecords);
189 1
		$collection->metaData = new MetaData((array) $response->_metadata);
190
		
191 1
		return $collection;
192
		
193
	}
194
195 1
	public function messagingHistory(Carbon $start = null, Carbon $end = null)
196
	{
197 1
		$this->retry_counter = 0;
198
199 1
		$start = $start ?: (new Carbon())->today();
200 1
		$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59);
201
202 1
		$results_object = $this->retrieveMsgHistory($start, $end);
203
		
204 1
		if ($results_object) {
205
		
206 1
			$results_object->_metadata->start = $start;
207 1
			$results_object->_metadata->end = $end;
208
		
209 1
			$meta = new MetaData((array) $results_object->_metadata);
210
			
211 1
			$collection = new ConversationHistory($results_object->conversationHistoryRecords);
212 1
			$collection->metaData = $meta;
213
			
214 1
			return $collection;
215
			
216
		} else {
217
			return false;
218
		}
219
	}
220
221 1
	public function history(Carbon $start = null, Carbon $end = null)
222
	{
223 1
		$this->retry_counter = 0;
224
225 1
		$start = $start ?: (new Carbon())->today();
226 1
		$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59);
227
228 1
		$results_object = $this->retrieveHistory($start, $end);
229
		
230 1
		if ($results_object) {
231
		
232 1
			$results_object->_metadata->start = $start;
233 1
			$results_object->_metadata->end = $end;
234
		
235 1
			$meta = new MetaData((array) $results_object->_metadata);
236
			
237 1
			$collection = new EngagementHistory($results_object->interactionHistoryRecords);
238 1
			$collection->metaData = $meta;
239
			
240 1
			return $collection;
241
			
242
		} else {
243
			return false;
244
		}
245
	}
246
	
247
	public function login()
248
	{
249
		$this->domain('agentVep');
250
		
251
		$consumer_key = config("{$this->config}.key");
252
		$consumer_secret = config("{$this->config}.secret");
253
		$token = config("{$this->config}.token");
254
		$secret = config("{$this->config}.token_secret");
255
		$username = config("{$this->config}.user_name");
256
		
257
		$auth = [
258
			'username'		  => $username,
259
			'appKey'			=> $consumer_key,
260
			'secret'			=> $consumer_secret,
261
			'accessToken'		=> $token,
262
			'accessTokenSecret' => $secret,
263
		];
264
		
265
		$url = "https://{$this->domain}/api/account/{$this->account}/login?v=1.3";
266
		
267
		$response = $this->requestV1($url, 'POST', $auth);
268
		
269
		$this->bearer = $response->bearer;
270
		
271
		return $this;
272
	}
273
	
274
	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...
275
	{
276
		if (!$this->bearer) {
277
			$this->login();
278
		}
279
		
280
		$client = new Client();
281
		$args = [
282
			'headers' => [
283
				'Authorization' => $this->bearer
284
			]
285
		];
286
		
287
		if ($payload !== false) {
288
			$args['body'] = json_encode($payload);
289
		}
290
		
291
		try {
292
			$res = $client->request($method, $url, $args);
293
		} catch (\Exception $e) {
294
			throw $e;
295
		} 
296
		
297
		return json_decode($res->getBody());
298
	}
299
	
300 2
	private function requestClient()
301
	{
302 2
		$consumer_key = config("{$this->config}.key");
303 2
		$consumer_secret = config("{$this->config}.secret");
304 2
		$token = config("{$this->config}.token");
305 2
		$secret = config("{$this->config}.token_secret");
306
307 2
		$stack = HandlerStack::create();
308 2
		$auth = new Oauth1([
309 2
			'consumer_key'	=> $consumer_key,
310 2
			'consumer_secret' => $consumer_secret,
311 2
			'token'		   => $token,
312 2
			'token_secret'	=> $secret,
313 2
			'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC,
314
		]);
315 2
		$stack->push($auth);
316
317 2
		$client = new Client([
318 2
			'handler' => $stack,
319
		]);
320
		
321 2
		return $client;
322
	}
323
	
324 1
	private function requestV1($url, $method, $payload = [])
325
	{
326 1
		$client = $this->requestClient();
327
328
		$args = [
329 1
			'auth' => 'oauth',
330
			'headers' => [
331
				'content-type' => 'application/json',
332
			],
333 1
			'body' => json_encode($payload)
334
		];
335
336
		try {
337 1
			$res = $client->request($method, $url, $args);
338 1
			$response = json_decode($res->getBody());
339
		} catch (\Exception $e) {
340
			if ($this->retry_counter < $this->retry_limit || $this->retry_limit == -1) {
341
				usleep(1500);
342
				$this->retry_counter++;
343
				$response = $this->requestV1($url, $payload);
344
			} else {
345
				throw $e; //new LiveEngageException("Retry limit has been exceeded ($this->retry_limit)", 100);
346
			}
347
		}
348
349 1
		return $response;
350
	}
351
}
352