Passed
Branch development (e6bda3)
by Robert
03:47
created

LiveEngageLaravel::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
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\Skill;
17
use LivePersonInc\LiveEngageLaravel\Models\Campaign;
18
use LivePersonInc\LiveEngageLaravel\Models\Engagement;
19
use LivePersonInc\LiveEngageLaravel\Models\Conversation;
20
use LivePersonInc\LiveEngageLaravel\Models\Message;
21
use LivePersonInc\LiveEngageLaravel\Collections\EngagementHistory;
22
use LivePersonInc\LiveEngageLaravel\Collections\Skills;
23
use LivePersonInc\LiveEngageLaravel\Collections\AgentParticipants;
24
use LivePersonInc\LiveEngageLaravel\Exceptions\LiveEngageException;
25
use LivePersonInc\LiveEngageLaravel\Collections\ConversationHistory;
26
27
/**
28
 * LiveEngageLaravel class holds all of the root package functions.
29
 *
30
 * All "setting" functions will return `$this` so method can be chained. Most methods will return a class object or Laravel collection.
31
 */
32
class LiveEngageLaravel
33
{
34
	private $account = false;
35
	private $results = [];
36
	private $skills = [];
37
	private $next = false;
38
	private $prev = false;
39
	private $start;
40
	private $end;
41
	private $config = 'services.liveperson.default';
42
	private $version = '1.0';
43
	private $history_limit = 50;
44
	private $history = false;
45
	private $context = 'interactionHistoryRecords';
46
	private $interactive = true;
47
	private $ended = true;
48
	private $bearer = false;
49
	private $revision = 0;
50
51
	private $domain = false;
52
53
	private $retry_limit = 5;
54
	private $retry_counter = 0;
55
56
	/**
57
	 * __get magic function to retrieve private properties of the class.
58
	 * 
59
	 * @access public
60
	 * @param mixed $attribute
61
	 * @return mixed
62
	 */
63 5
	public function __get($attribute)
64
	{
65 5
		return $this->$attribute;
66
	}
67
68
	/**
69
	 * __construct function.
70
	 * 
71
	 * @access public
72
	 * @return void
73
	 */
74 8
	public function __construct()
75
	{
76 8
		$this->account = config("{$this->config}.account");
77
		//$this->domain = config("{$this->config}.domain");
78 8
		$this->version = config("{$this->config}.version") ?: $this->version;
79 8
	}
80
81
	/**
82
	 * key function sets the keyset the class should use. Setting this once will be stored for script execution, but not for the session.
83
	 * 
84
	 * @access public
85
	 * @param string $key (default: 'default')
86
	 * @return this
87
	 */
88 1
	public function key($key = 'default')
89
	{
90 1
		$this->config = "services.liveperson.$key";
91 1
		$this->__construct();
92
93 1
		return $this;
94
	}
95
	
96 1
	public function nonInteractive()
97
	{
98 1
		$this->interactive = false;
99 1
		return $this;
100
	}
101
	
102 1
	public function active()
103
	{
104 1
		$this->ended = false;
105 1
		return $this;
106
	}
107
108 1
	public function limit($limit)
109
	{
110 1
		$this->history_limit = $limit;
111
112 1
		return $this;
113
	}
114
115 1
	public function retry($limit)
116
	{
117 1
		$this->retry_limit = $limit;
118
119 1
		return $this;
120
	}
121
122 1
	public function account($accountid)
123
	{
124 1
		$this->account = $accountid;
125
126 1
		return $this;
127
	}
128
129
	/**
130
	 * domain function sets the LivePerson domain for the secified service. Like `key` it is set for the execution script, but not session. It must be run each time.
131
	 * 
132
	 * @access public
133
	 * @param mixed $service
134
	 * @return this
135
	 */
136 1
	public function domain($service)
137
	{
138 1
		$response = $this->requestV1("https://api.liveperson.net/api/account/{$this->account}/service/{$service}/baseURI.json?version={$this->version}", 'GET');
139
		
140 1
		$this->domain = $response->baseURI;
141
142 1
		return $this;
143
	}
144
145
	/**
146
	 * visitor function gets or sets visitor attribute information - this only works for CHAT, not messaging.
147
	 * 
148
	 * @access public
149
	 * @param string $visitorID
150
	 * @param string $sessionID
151
	 * @param mixed $setData (default: false)
152
	 * @return mixed
153
	 * @codeCoverageIgnore
154
	 */
155
	public function visitor($visitorID, $sessionID, $setData = false)
156
	{
157
		$this->domain('smt');
158
159
		if ($setData) {
160
			$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/events?v=1&sid={$sessionID}";
161
162
			return $this->requestV1($url, 'POST', $setData);
163
		} else {
164
			$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/state?v=1&sid={$sessionID}";
165
166
			return $this->requestV1($url, 'GET');
167
		}
168
	}
169
	
170
	/**
171
     * @codeCoverageIgnore
172
     */
173
	// TODO: Enable server chat api here. I actually may create new class
174
	public function chat()
175
	{
176
		$this->domain('conversationVep');
177
		
178
		$url = "https://{$this->domain}/api/account/{$this->account}/chat/request?v=1&NC=true";
179
		
180
		$args = [
181
			
182
		];
183
		$payload = new Payload($args);
184
		
185
		$response = $this->requestV1($url, 'POST', $payload);
0 ignored issues
show
Bug introduced by
$payload of type LivePersonInc\LiveEngageLaravel\Models\Payload is incompatible with the type array expected by parameter $payload of LivePersonInc\LiveEngage...ageLaravel::requestV1(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

185
		$response = $this->requestV1($url, 'POST', /** @scrutinizer ignore-type */ $payload);
Loading history...
186
		
187
		return $response;
188
	}
189
190 1
	final public function retrieveHistory(Carbon $start, Carbon $end, $url = false)
191
	{
192 1
		$this->domain('engHistDomain');
193
194 1
		$url = $url ?: "https://{$this->domain}/interaction_history/api/account/{$this->account}/interactions/search?limit={$this->history_limit}&offset=0";
195
196 1
		$start_str = $start->toW3cString();
197 1
		$end_str = $end->toW3cString();
198
199 1
		$data = new Payload([
200 1
			'interactive' => $this->interactive,
201 1
			'ended' => $this->ended,
202
			'start' => [
203 1
				'from' => strtotime($start_str) . '000',
204 1
				'to' => strtotime($end_str) . '000',
205
			],
206 1
			'skillIds' => $this->skills
207
		]);
208
209 1
		return $this->requestV1($url, 'POST', $data);
0 ignored issues
show
Bug introduced by
$data of type LivePersonInc\LiveEngageLaravel\Models\Payload is incompatible with the type array expected by parameter $payload of LivePersonInc\LiveEngage...ageLaravel::requestV1(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
		return $this->requestV1($url, 'POST', /** @scrutinizer ignore-type */ $data);
Loading history...
210
	}
211
212 1
	final public function retrieveMsgHistory(Carbon $start, Carbon $end, $url = false)
213
	{
214 1
		$this->domain('msgHist');
215
216 1
		$url = $url ?: "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/search?limit={$this->history_limit}&offset=0&sort=start:desc";
217
218 1
		$start_str = $start->toW3cString();
219 1
		$end_str = $end->toW3cString();
220
221 1
		$data = new Payload([
222 1
			'status' => $this->ended ? ['CLOSE'] : ['OPEN', 'CLOSE'],
223
			'start' => [
224 1
				'from' => strtotime($start_str) . '000',
225 1
				'to' => strtotime($end_str) . '000',
226
			],
227 1
			'skillIds' => $this->skills
228
		]);
229
		
230 1
		return $this->requestV1($url, 'POST', $data);
0 ignored issues
show
Bug introduced by
$data of type LivePersonInc\LiveEngageLaravel\Models\Payload is incompatible with the type array expected by parameter $payload of LivePersonInc\LiveEngage...ageLaravel::requestV1(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

230
		return $this->requestV1($url, 'POST', /** @scrutinizer ignore-type */ $data);
Loading history...
231
	}
232
	
233
	/**
234
	 * skills function gets collection of skills associated with the account.
235
	 * 
236
	 * @access public
237
	 * @return Collections\Skills
238
	 */
239 1
	public function skills()
240
	{
241 1
		$this->domain('accountConfigReadOnly');
242
		
243 1
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/skills?v=4.0";
244
		
245 1
		return new Skills($this->requestV2($url, 'GET'));
0 ignored issues
show
Bug introduced by
$this->requestV2($url, 'GET') of type void is incompatible with the type array expected by parameter $models of LivePersonInc\LiveEngage...s\Skills::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

245
		return new Skills(/** @scrutinizer ignore-type */ $this->requestV2($url, 'GET'));
Loading history...
Bug introduced by
Are you sure the usage of $this->requestV2($url, 'GET') targeting LivePersonInc\LiveEngage...ageLaravel::requestV2() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
246
	}
247
	
248
	/**
249
	 * getSkill function gets skill object based on ID.
250
	 * 
251
	 * @access public
252
	 * @param int $skillId
253
	 * @return Models\Skill
254
	 */
255 1
	public function getSkill($skillId)
256
	{
257 1
		$this->domain('accountConfigReadOnly');
258
		
259 1
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/skills/{$skillId}?v=4.0";
260
		
261 1
		return new Skill((array) $this->requestV2($url, 'GET'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->requestV2($url, 'GET') targeting LivePersonInc\LiveEngage...ageLaravel::requestV2() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
262
	}
263
	
264
	/**
265
	 * getAgent function gets agent object based on ID.
266
	 * 
267
	 * @access public
268
	 * @param int $userId
269
	 * @return Models\Agent
270
	 */
271 1
	public function getAgent($userId)
272
	{
273 1
		$this->domain('accountConfigReadOnly');
274
		
275 1
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/users/{$userId}?v=4.0";
276
		
277 1
		return new Agent((array) $this->requestV2($url, 'GET'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->requestV2($url, 'GET') targeting LivePersonInc\LiveEngage...ageLaravel::requestV2() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
278
	}
279
	
280
	/**
281
	 * updateAgent function.
282
	 * 
283
	 * @access public
284
	 * @param mixed $userId
285
	 * @param mixed $properties
286
	 * @return void
287
	 * @codeCoverageIgnore
288
	 */
289
	public function updateAgent($userId, $properties)
290
	{
291
		$agent = $this->getAgent($userId);
0 ignored issues
show
Unused Code introduced by
The assignment to $agent is dead and can be removed.
Loading history...
292
		
293
		$this->domain('accountConfigReadWrite');
294
		
295
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/users/{$userId}?v=4.0";
296
		$headers = [
297
			'X-HTTP-Method-Override' => 'PUT',
298
			'if-Match' => '*'
299
		];
300
		
301
		return new Agent((array) $this->requestV2($url, 'PUT', $properties, $headers));
0 ignored issues
show
Bug Best Practice introduced by
The expression return new LivePersonInc...$properties, $headers)) returns the type LivePersonInc\LiveEngageLaravel\Models\Agent which is incompatible with the documented return type void.
Loading history...
Bug introduced by
Are you sure the usage of $this->requestV2($url, '... $properties, $headers) targeting LivePersonInc\LiveEngage...ageLaravel::requestV2() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
302
	}
303
	
304
	/**
305
	 * agents function gets collection of agents from account.
306
	 * 
307
	 * @access public
308
	 * @return Collections\AgentParticipants
309
	 */
310 1
	public function agents()
311
	{
312 1
		$this->domain('accountConfigReadOnly');
313
		
314 1
		$select = implode(',', [
315 1
			'id',
316
			'pid',
317
			'deleted',
318
			'loginName',
319
			'skills',
320
			'nickname',
321
			'dateCreated',
322
			'userTypeId',
323
			'isApiUser',
324
			'profileIds',
325
			'permissionGroups',
326
			'allowedAppKeys',
327
			'changePwdNextLogin',
328
			'maxChats',
329
			'skillIds',
330
			'lpaCreatedUser',
331
			'email',
332
			'lobs',
333
			'profiles',
334
			'fullName',
335
			'employeeId',
336
			'managedAgentGroups',
337
			'dateUpdated',
338
			'isEnabled',
339
			'lastPwdChangeDate'
340
		]);
341
		
342 1
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/users?v=4.0&select=$select";
343
		
344 1
		return new AgentParticipants($this->requestV2($url, 'GET'));
0 ignored issues
show
Bug introduced by
$this->requestV2($url, 'GET') of type void is incompatible with the type array expected by parameter $models of LivePersonInc\LiveEngage...icipants::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

344
		return new AgentParticipants(/** @scrutinizer ignore-type */ $this->requestV2($url, 'GET'));
Loading history...
Bug introduced by
Are you sure the usage of $this->requestV2($url, 'GET') targeting LivePersonInc\LiveEngage...ageLaravel::requestV2() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
345
	}
346
	
347
	/**
348
	 * getAgentStatus function gets status of agents based on provided Skill IDs.
349
	 * 
350
	 * @access public
351
	 * @param int/array $skills
0 ignored issues
show
Documentation Bug introduced by
The doc comment int/array at position 0 could not be parsed: Unknown type name 'int/array' at position 0 in int/array.
Loading history...
352
	 * @return Collections\AgentParticipants
353
	 */
354 1
	public function getAgentStatus($skills)
355
	{
356 1
		$skills = is_array($skills) ? $skills : [$skills];
357
	
358 1
		$this->domain('msgHist');
359
		
360 1
		$url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/agent-view/status";
361
		
362 1
		$data = ['skillIds' => $skills];
363
		
364 1
		$response = $this->requestV1($url, 'POST', $data);
365 1
		$collection = new AgentParticipants($response->agentStatusRecords);
366 1
		$collection->metaData = new MetaData((array) $response->_metadata);
367
		
368 1
		return $collection;
369
		
370
	}
371
	
372
	/**
373
	 * conversationHistory function.
374
	 * 
375
	 * @access public
376
	 * @param Carbon $start (default: null)
377
	 * @param Carbon $end (default: null)
378
	 * @param int/array $skills (default: [])
0 ignored issues
show
Documentation Bug introduced by
The doc comment int/array at position 0 could not be parsed: Unknown type name 'int/array' at position 0 in int/array.
Loading history...
379
	 * @return Collections\ConversationHistory
380
	 */
381 1
	public function conversationHistory(Carbon $start = null, Carbon $end = null, $skills = [])
382
	{
383 1
		$this->retry_counter = 0;
384 1
		$this->skills = $skills;
385
386 1
		$start = $start ?: (new Carbon())->today();
387 1
		$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59);
388
389 1
		$results_object = $this->retrieveMsgHistory($start, $end);
390
		
391 1
		$results_object->_metadata->start = $start;
392 1
		$results_object->_metadata->end = $end;
393
	
394 1
		$meta = new MetaData((array) $results_object->_metadata);
395
		
396 1
		$collection = new ConversationHistory($results_object->conversationHistoryRecords);
397 1
		$collection->metaData = $meta;
398
		
399 1
		return $collection;
400
			
401
	}
402
	
403
	/**
404
	 * getConversation function.
405
	 * 
406
	 * @access public
407
	 * @param mixed $conversationId
408
	 * @return Models\Conversation
409
	 */
410 1
	public function getConversation($conversationId)
411
	{
412 1
		$this->domain('msgHist');
413
		
414 1
		$url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/conversation/search";
415
		
416 1
		$data = new Payload([
417 1
			'conversationId' => $conversationId
418
		]);
419
		
420 1
		$result = $this->requestV1($url, 'POST', $data);
0 ignored issues
show
Bug introduced by
$data of type LivePersonInc\LiveEngageLaravel\Models\Payload is incompatible with the type array expected by parameter $payload of LivePersonInc\LiveEngage...ageLaravel::requestV1(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

420
		$result = $this->requestV1($url, 'POST', /** @scrutinizer ignore-type */ $data);
Loading history...
421 1
		if (!count($result->conversationHistoryRecords)) {
422
			return null;
423
		}
424
		
425 1
		return new Conversation((array) $result->conversationHistoryRecords[0]);
426
	}
427
428
	/**
429
	 * engagementHistory function.
430
	 * 
431
	 * @access public
432
	 * @param Carbon $start (default: null)
433
	 * @param Carbon $end (default: null)
434
	 * @param int/array $skills (default: [])
0 ignored issues
show
Documentation Bug introduced by
The doc comment int/array at position 0 could not be parsed: Unknown type name 'int/array' at position 0 in int/array.
Loading history...
435
	 * @return Collections\EngagementHistory
436
	 */
437 1
	public function engagementHistory(Carbon $start = null, Carbon $end = null, $skills = [])
438
	{
439 1
		$this->retry_counter = 0;
440 1
		$this->skills = $skills;
441
442 1
		$start = $start ?: (new Carbon())->today();
443 1
		$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59);
444
445 1
		$results_object = $this->retrieveHistory($start, $end);
446
		
447 1
		$results_object->_metadata->start = $start;
448 1
		$results_object->_metadata->end = $end;
449
	
450 1
		$meta = new MetaData((array) $results_object->_metadata);
451
		
452 1
		$collection = new EngagementHistory($results_object->interactionHistoryRecords);
453 1
		$collection->metaData = $meta;
454
		
455 1
		return $collection;
456
			
457
	}
458
	
459
	/**
460
	 * status function gets status of the account.
461
	 * 
462
	 * @access public
463
	 * @return Models\AccountStatus
464
	 */
465 1
	public function status()
466
	{
467 1
		$url = "https://status.liveperson.com/json?site={$this->account}";
468
		
469 1
		$response = $this->requestV1($url, 'GET');
470
		
471 1
		return new AccountStatus((array) $response);
472
	}
473
	
474
	/**
475
	 * login function.
476
	 * 
477
	 * @access public
478
	 * @return this
479
	 */
480
	public function login()
481
	{
482
		$this->domain('agentVep');
483
		
484
		$consumer_key = config("{$this->config}.key");
485
		$consumer_secret = config("{$this->config}.secret");
486
		$token = config("{$this->config}.token");
487
		$secret = config("{$this->config}.token_secret");
488
		$username = config("{$this->config}.user_name");
489
		
490
		$auth = [
491
			'username'		  => $username,
492
			'appKey'			=> $consumer_key,
493
			'secret'			=> $consumer_secret,
494
			'accessToken'		=> $token,
495
			'accessTokenSecret' => $secret,
496
		];
497
		
498
		$url = "https://{$this->domain}/api/account/{$this->account}/login?v=1.3";
499
		
500
		$response = $this->requestV1($url, 'POST', $auth);
501
		
502
		$this->bearer = $response->bearer;
503
		
504
		return $this;
505
	}
506
	
507
	/**
508
	 * requestV2 function.
509
	 * 
510
	 * @access private
511
	 * @param mixed $url
512
	 * @param mixed $method
513
	 * @param mixed $payload (default: [])
514
	 * @param mixed $headers (default: [])
515
	 * @return void
516
	 */
517
	private function requestV2($url, $method, $payload = [], $headers = [])
518
	{
519
		$this->login();
520
		
521
		$client = new Client();
522
		$args = [
523
			'headers' => array_merge([
524
				'content-type' => 'application/json',
525
				'Authorization' => 'Bearer ' . $this->bearer
526
			], $headers),
527
			'body' => json_encode($payload)
528
		];
529
		
530
		try {
531
			$res = $client->request($method, $url, $args);
532
		} catch (\Exception $e) {
533
			throw $e;
534
		} 
535
		
536
		return json_decode($res->getBody());
537
	}
538
	
539
	/**
540
	 * requestClient function.
541
	 * 
542
	 * @access private
543
	 * @return \GuzzleHttp\Client
544
	 */
545 2
	private function requestClient()
546
	{
547 2
		$consumer_key = config("{$this->config}.key");
548 2
		$consumer_secret = config("{$this->config}.secret");
549 2
		$token = config("{$this->config}.token");
550 2
		$secret = config("{$this->config}.token_secret");
551
552 2
		$stack = HandlerStack::create();
553 2
		$auth = new Oauth1([
554 2
			'consumer_key'	=> $consumer_key,
555 2
			'consumer_secret' => $consumer_secret,
556 2
			'token'		   => $token,
557 2
			'token_secret'	=> $secret,
558 2
			'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC,
559
		]);
560 2
		$stack->push($auth);
561
562 2
		$client = new Client([
563 2
			'handler' => $stack,
564
		]);
565
		
566 2
		return $client;
567
	}
568
	
569
	/**
570
	 * requestV1 - request bootstrap for older oauth supported APIs.
571
	 * 
572
	 * @access private
573
	 * @param string $url
574
	 * @param string $method
575
	 * @param array $payload (default: [])
576
	 * @return mixed
577
	 */
578 1
	private function requestV1($url, $method, $payload = [])
579
	{
580 1
		$client = $this->requestClient();
581
582
		$args = [
583 1
			'auth' => 'oauth',
584
			'headers' => [
585
				'content-type' => 'application/json',
586
			],
587 1
			'body' => json_encode($payload)
588
		];
589
590
		try {
591 1
			$res = $client->request($method, $url, $args);
592 1
			$response = json_decode($res->getBody());
593
		} catch (\Exception $e) {
594
			if ($this->retry_counter < $this->retry_limit || $this->retry_limit == -1) {
595
				usleep(1500);
596
				$this->retry_counter++;
597
				$response = $this->requestV1($url, $payload);
0 ignored issues
show
Bug introduced by
$payload of type array is incompatible with the type string expected by parameter $method of LivePersonInc\LiveEngage...ageLaravel::requestV1(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

597
				$response = $this->requestV1($url, /** @scrutinizer ignore-type */ $payload);
Loading history...
598
			} else {
599
				throw $e; //new LiveEngageException("Retry limit has been exceeded ($this->retry_limit)", 100);
600
			}
601
		}
602
603 1
		return $response;
604
	}
605
}
606