Passed
Push — master ( ae54b4...f2603d )
by Robert
03:33
created

LiveEngageLaravel::requestV1()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.5726

Importance

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