Passed
Push — master ( 074c96...5e6d2b )
by Robert
04:56
created

LiveEngageLaravel::getAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Root class file for all api wrappers.
4
 *
5
 */
6
 
7
namespace LivePersonInc\LiveEngageLaravel;
8
9
use Carbon\Carbon;
10
use GuzzleHttp\Client;
11
use GuzzleHttp\HandlerStack;
12
use GuzzleHttp\Subscriber\Oauth\Oauth1;
13
use LivePersonInc\LiveEngageLaravel\Models\Info;
14
use LivePersonInc\LiveEngageLaravel\Models\MetaData;
15
use LivePersonInc\LiveEngageLaravel\Models\AccountStatus;
16
use LivePersonInc\LiveEngageLaravel\Models\MessagingInfo;
17
use LivePersonInc\LiveEngageLaravel\Models\Payload;
18
use LivePersonInc\LiveEngageLaravel\Models\Visitor;
19
use LivePersonInc\LiveEngageLaravel\Models\Agent;
20
use LivePersonInc\LiveEngageLaravel\Models\Skill;
21
use LivePersonInc\LiveEngageLaravel\Models\Campaign;
22
use LivePersonInc\LiveEngageLaravel\Models\Engagement;
23
use LivePersonInc\LiveEngageLaravel\Models\Conversation;
24
use LivePersonInc\LiveEngageLaravel\Models\Message;
25
use LivePersonInc\LiveEngageLaravel\Collections\EngagementHistory;
26
use LivePersonInc\LiveEngageLaravel\Collections\Skills;
27
use LivePersonInc\LiveEngageLaravel\Collections\AgentParticipants;
28
use LivePersonInc\LiveEngageLaravel\Exceptions\LiveEngageException;
29
use LivePersonInc\LiveEngageLaravel\Collections\ConversationHistory;
30
31
/**
32
 * LiveEngageLaravel class holds all of the root package functions.
33
 *
34
 * All "setting" functions will return `$this` so method can be chained. Most methods will return a class object or Laravel collection.
35
 */
36
class LiveEngageLaravel
37
{
38
	/**
39
	 * account - LiveEngage account number, usually set by configuration
40
	 * 
41
	 * (default value: false)
42
	 * 
43
	 * @var long
0 ignored issues
show
Bug introduced by
The type LivePersonInc\LiveEngageLaravel\long was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
	 * @access private
45
	 */
46
	private $account = false;
47
	/**
48
	 * skills - holds the skills for history retrieval
49
	 * 
50
	 * (default value: [])
51
	 * 
52
	 * @var array
53
	 * @access private
54
	 */
55
	private $skills = [];
56
	/**
57
	 * config - holds the configuration key where keyset is stored in config/services.php
58
	 * 
59
	 * (default value: 'services.liveperson.default')
60
	 * 
61
	 * @var string
62
	 * @access private
63
	 */
64
	private $config = 'services.liveperson.default';
65
	/**
66
	 * version - api version
67
	 * 
68
	 * (default value: '1.0')
69
	 * 
70
	 * @var string
71
	 * @access private
72
	 */
73
	private $version = '1.0';
74
	/**
75
	 * history_limit - stores the history page limit
76
	 * 
77
	 * (default value: 50)
78
	 * 
79
	 * @var int
80
	 * @access private
81
	 */
82
	private $history_limit = 50;
83
	private $interactive = true;
84
	private $ended = true;
85
	/**
86
	 * bearer - bearer token for V2 authentication
87
	 * 
88
	 * (default value: false)
89
	 * 
90
	 * @var string
91
	 * @access private
92
	 */
93
	private $bearer = false;
94
	/**
95
	 * revision
96
	 * 
97
	 * (default value: 0)
98
	 * 
99
	 * @var int
100
	 * @access private
101
	 */
102
	private $revision = 0;
103
104
	/**
105
	 * domain - api domain storage
106
	 * 
107
	 * (default value: false)
108
	 * 
109
	 * @var bool
110
	 * @access private
111
	 */
112
	private $domain = false;
113
114
	/**
115
	 * retry_limit - number of times the request will attempt before it throws the exception.
116
	 * 
117
	 * (default value: 5)
118
	 * 
119
	 * @var int
120
	 * @access private
121
	 */
122
	private $retry_limit = 5;
123
	/**
124
	 * retry_counter - stores current count of retries.
125
	 * 
126
	 * (default value: 0)
127
	 * 
128
	 * @var int
129
	 * @access private
130
	 */
131
	private $retry_counter = 0;
132
133
	/**
134
	 * __get magic function to retrieve private properties of the class.
135
	 * 
136
	 * @access public
137
	 * @param mixed $attribute
138
	 * @return mixed
139
	 */
140 5
	public function __get($attribute)
141
	{
142 5
		return $this->$attribute;
143
	}
144
145
	/**
146
	 * __construct function.
147
	 * 
148
	 * @access public
149
	 * @return void
150
	 */
151 8
	public function __construct()
152
	{
153 8
		$this->account = config("{$this->config}.account");
154
		//$this->domain = config("{$this->config}.domain");
155 8
		$this->version = config("{$this->config}.version") ?: $this->version;
156 8
	}
157
158
	/**
159
	 * key function sets the keyset the class should use. Setting this once will be stored for script execution, but not for the session.
160
	 * 
161
	 * @access public
162
	 * @param string $key (default: 'default')
163
	 * @return this
164
	 */
165 1
	public function key($key = 'default')
166
	{
167 1
		$this->config = "services.liveperson.$key";
168 1
		$this->__construct();
169
170 1
		return $this;
171
	}
172
	
173
	/**
174
	 * nonInteractive function.
175
	 * 
176
	 * @access public
177
	 * @return void
178
	 */
179 1
	public function nonInteractive()
180
	{
181 1
		$this->interactive = false;
182 1
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LivePersonInc\LiveEngageLaravel\LiveEngageLaravel which is incompatible with the documented return type void.
Loading history...
183
	}
184
	
185
	/**
186
	 * active function.
187
	 * 
188
	 * @access public
189
	 * @return void
190
	 */
191 1
	public function active()
192
	{
193 1
		$this->ended = false;
194 1
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LivePersonInc\LiveEngageLaravel\LiveEngageLaravel which is incompatible with the documented return type void.
Loading history...
195
	}
196
197
	/**
198
	 * limit function.
199
	 * 
200
	 * @access public
201
	 * @param mixed $limit
202
	 * @return void
203
	 */
204 1
	public function limit($limit)
205
	{
206 1
		$this->history_limit = $limit;
207
208 1
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LivePersonInc\LiveEngageLaravel\LiveEngageLaravel which is incompatible with the documented return type void.
Loading history...
209
	}
210
211
	/**
212
	 * retry function.
213
	 * 
214
	 * @access public
215
	 * @param mixed $limit
216
	 * @return void
217
	 */
218 1
	public function retry($limit)
219
	{
220 1
		$this->retry_limit = $limit;
221
222 1
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LivePersonInc\LiveEngageLaravel\LiveEngageLaravel which is incompatible with the documented return type void.
Loading history...
223
	}
224
225
	/**
226
	 * account function.
227
	 * 
228
	 * @access public
229
	 * @param mixed $accountid
230
	 * @return void
231
	 */
232 1
	public function account($accountid)
233
	{
234 1
		$this->account = $accountid;
235
236 1
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LivePersonInc\LiveEngageLaravel\LiveEngageLaravel which is incompatible with the documented return type void.
Loading history...
237
	}
238
239
	/**
240
	 * 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.
241
	 * 
242
	 * @access public
243
	 * @param mixed $service
244
	 * @return this
245
	 */
246 1
	public function domain($service)
247
	{
248 1
		$response = $this->requestV1("https://api.liveperson.net/api/account/{$this->account}/service/{$service}/baseURI.json?version={$this->version}", 'GET');
249
		
250 1
		$this->domain = $response->baseURI;
251
252 1
		return $this;
253
	}
254
255
	/**
256
	 * visitor function gets or sets visitor attribute information - this only works for CHAT, not messaging.
257
	 * 
258
	 * @access public
259
	 * @param string $visitorID
260
	 * @param string $sessionID
261
	 * @param mixed $setData (default: false)
262
	 * @return mixed
263
	 * @codeCoverageIgnore
264
	 */
265
	public function visitor($visitorID, $sessionID, $setData = false)
266
	{
267
		$this->domain('smt');
268
269
		if ($setData) {
270
			$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/events?v=1&sid={$sessionID}";
271
272
			return $this->requestV1($url, 'POST', $setData);
273
		} else {
274
			$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/state?v=1&sid={$sessionID}";
275
276
			return $this->requestV1($url, 'GET');
277
		}
278
	}
279
	
280
	/**
281
	 * chat function
282
	 *
283
     * @codeCoverageIgnore
284
     *
285
     * @todo connect this to the server chat api - this function currently does nothing.
286
     */
287
	public function chat()
288
	{
289
		$this->domain('conversationVep');
290
		
291
		$url = "https://{$this->domain}/api/account/{$this->account}/chat/request?v=1&NC=true";
292
		
293
		$args = [
294
			
295
		];
296
		$payload = new Payload($args);
297
		
298
		$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

298
		$response = $this->requestV1($url, 'POST', /** @scrutinizer ignore-type */ $payload);
Loading history...
299
		
300
		return $response;
301
	}
302
303
	/**
304
	 * retrieveHistory function.
305
	 * 
306
	 * @access public
307
	 * @final
308
	 * @param Carbon $start
309
	 * @param Carbon $end
310
	 * @param string $url (default: false)
311
	 * @return mixed
312
	 */
313 1
	final public function retrieveHistory(Carbon $start, Carbon $end, $url = false)
314
	{
315 1
		$this->domain('engHistDomain');
316
317 1
		$url = $url ?: "https://{$this->domain}/interaction_history/api/account/{$this->account}/interactions/search?limit={$this->history_limit}&offset=0";
318
319 1
		$start_str = $start->toW3cString();
320 1
		$end_str = $end->toW3cString();
321
322 1
		$data = new Payload([
323 1
			'interactive' => $this->interactive,
324 1
			'ended' => $this->ended,
325
			'start' => [
326 1
				'from' => strtotime($start_str) . '000',
327 1
				'to' => strtotime($end_str) . '000',
328
			],
329 1
			'skillIds' => $this->skills
330
		]);
331
332 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

332
		return $this->requestV1($url, 'POST', /** @scrutinizer ignore-type */ $data);
Loading history...
333
	}
334
335
	/**
336
	 * retrieveMsgHistory function.
337
	 * 
338
	 * @access public
339
	 * @final
340
	 * @param Carbon $start
341
	 * @param Carbon $end
342
	 * @param string $url (default: false)
343
	 * @return mixed
344
	 */
345 1
	final public function retrieveMsgHistory(Carbon $start, Carbon $end, $url = false)
346
	{
347 1
		$this->domain('msgHist');
348
349 1
		$url = $url ?: "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/search?limit={$this->history_limit}&offset=0&sort=start:desc";
350
351 1
		$start_str = $start->toW3cString();
352 1
		$end_str = $end->toW3cString();
353
354 1
		$data = new Payload([
355 1
			'status' => $this->ended ? ['CLOSE'] : ['OPEN', 'CLOSE'],
356
			'start' => [
357 1
				'from' => strtotime($start_str) . '000',
358 1
				'to' => strtotime($end_str) . '000',
359
			],
360 1
			'skillIds' => $this->skills
361
		]);
362
		
363 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

363
		return $this->requestV1($url, 'POST', /** @scrutinizer ignore-type */ $data);
Loading history...
364
	}
365
	
366
	/**
367
	 * skills function gets collection of skills associated with the account.
368
	 * 
369
	 * @access public
370
	 * @return Collections\Skills
371
	 */
372 1
	public function skills()
373
	{
374 1
		$this->domain('accountConfigReadOnly');
375
		
376 1
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/skills?v=4.0";
377
		
378 1
		return new Skills($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...
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

378
		return new Skills(/** @scrutinizer ignore-type */ $this->requestV2($url, 'GET'));
Loading history...
379
	}
380
	
381
	/**
382
	 * getSkill function gets skill object based on ID.
383
	 * 
384
	 * @access public
385
	 * @param int $skillId
386
	 * @return Models\Skill
387
	 */
388 1
	public function getSkill($skillId)
389
	{
390 1
		$this->domain('accountConfigReadOnly');
391
		
392 1
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/skills/{$skillId}?v=4.0";
393
		
394 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...
395
	}
396
	
397
	/**
398
	 * getAgent function gets agent object based on ID.
399
	 * 
400
	 * @access public
401
	 * @param int $userId
402
	 * @return Models\Agent
403
	 */
404 1
	public function getAgent($userId)
405
	{
406 1
		$this->domain('accountConfigReadOnly');
407
		
408 1
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/users/{$userId}?v=4.0";
409
		
410 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...
411
	}
412
	
413
	/**
414
	 * updateAgent function.
415
	 * 
416
	 * @access public
417
	 * @param mixed $userId
418
	 * @param mixed $properties
419
	 * @return void
420
	 * @codeCoverageIgnore
421
	 */
422
	public function updateAgent($userId, $properties)
423
	{
424
		$agent = $this->getAgent($userId);
0 ignored issues
show
Unused Code introduced by
The assignment to $agent is dead and can be removed.
Loading history...
425
		
426
		$this->domain('accountConfigReadWrite');
427
		
428
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/users/{$userId}?v=4.0";
429
		$headers = [
430
			'X-HTTP-Method-Override' => 'PUT',
431
			'if-Match' => '*'
432
		];
433
		
434
		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...
435
	}
436
	
437
	/**
438
	 * agents function gets collection of agents from account.
439
	 * 
440
	 * @access public
441
	 * @return Collections\AgentParticipants
442
	 */
443 1
	public function agents()
444
	{
445 1
		$this->domain('accountConfigReadOnly');
446
		
447 1
		$select = implode(',', [
448 1
			'id',
449
			'pid',
450
			'deleted',
451
			'loginName',
452
			'skills',
453
			'nickname',
454
			'dateCreated',
455
			'userTypeId',
456
			'isApiUser',
457
			'profileIds',
458
			'permissionGroups',
459
			'allowedAppKeys',
460
			'changePwdNextLogin',
461
			'maxChats',
462
			'skillIds',
463
			'lpaCreatedUser',
464
			'email',
465
			'lobs',
466
			'profiles',
467
			'fullName',
468
			'employeeId',
469
			'managedAgentGroups',
470
			'dateUpdated',
471
			'isEnabled',
472
			'lastPwdChangeDate'
473
		]);
474
		
475 1
		$url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/users?v=4.0&select=$select";
476
		
477 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

477
		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...
478
	}
479
	
480
	/**
481
	 * getAgentStatus function gets status of agents based on provided Skill IDs.
482
	 * 
483
	 * @access public
484
	 * @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...
485
	 * @return Collections\AgentParticipants
486
	 */
487 1
	public function getAgentStatus($skills)
488
	{
489 1
		$skills = is_array($skills) ? $skills : [$skills];
490
	
491 1
		$this->domain('msgHist');
492
		
493 1
		$url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/agent-view/status";
494
		
495 1
		$data = ['skillIds' => $skills];
496
		
497 1
		$response = $this->requestV1($url, 'POST', $data);
498 1
		$collection = new AgentParticipants($response->agentStatusRecords);
499 1
		$collection->metaData = new MetaData((array) $response->_metadata);
500
		
501 1
		return $collection;
502
		
503
	}
504
	
505
	/**
506
	 * conversationHistory function.
507
	 * 
508
	 * @access public
509
	 * @param Carbon $start (default: null)
510
	 * @param Carbon $end (default: null)
511
	 * @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...
512
	 * @return Collections\ConversationHistory
513
	 */
514 1
	public function conversationHistory(Carbon $start = null, Carbon $end = null, $skills = [])
515
	{
516 1
		$this->retry_counter = 0;
517 1
		$this->skills = $skills;
518
519 1
		$start = $start ?: (new Carbon())->today();
520 1
		$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59);
521
522 1
		$results_object = $this->retrieveMsgHistory($start, $end);
523
		
524 1
		$results_object->_metadata->start = $start;
525 1
		$results_object->_metadata->end = $end;
526
	
527 1
		$meta = new MetaData((array) $results_object->_metadata);
528
		
529 1
		$collection = new ConversationHistory($results_object->conversationHistoryRecords);
530 1
		$collection->metaData = $meta;
531
		
532 1
		return $collection;
533
			
534
	}
535
	
536
	/**
537
	 * getConversation function.
538
	 * 
539
	 * @access public
540
	 * @param mixed $conversationId
541
	 * @return Models\Conversation
542
	 */
543 1
	public function getConversation($conversationId)
544
	{
545 1
		$this->domain('msgHist');
546
		
547 1
		$url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/conversation/search";
548
		
549 1
		$data = new Payload([
550 1
			'conversationId' => $conversationId
551
		]);
552
		
553 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

553
		$result = $this->requestV1($url, 'POST', /** @scrutinizer ignore-type */ $data);
Loading history...
554 1
		if (!count($result->conversationHistoryRecords)) {
555
			return null; // @codeCoverageIgnore
556
		}
557
		
558 1
		return new Conversation((array) $result->conversationHistoryRecords[0]);
559
	}
560
561
	/**
562
	 * engagementHistory function.
563
	 * 
564
	 * @access public
565
	 * @param Carbon $start (default: null)
566
	 * @param Carbon $end (default: null)
567
	 * @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...
568
	 * @return Collections\EngagementHistory
569
	 */
570 1
	public function engagementHistory(Carbon $start = null, Carbon $end = null, $skills = [])
571
	{
572 1
		$this->retry_counter = 0;
573 1
		$this->skills = $skills;
574
575 1
		$start = $start ?: (new Carbon())->today();
576 1
		$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59);
577
578 1
		$results_object = $this->retrieveHistory($start, $end);
579
		
580 1
		$results_object->_metadata->start = $start;
581 1
		$results_object->_metadata->end = $end;
582
	
583 1
		$meta = new MetaData((array) $results_object->_metadata);
584
		
585 1
		$collection = new EngagementHistory($results_object->interactionHistoryRecords);
586 1
		$collection->metaData = $meta;
587
		
588 1
		return $collection;
589
			
590
	}
591
	
592
	/**
593
	 * status function gets status of the account.
594
	 * 
595
	 * @access public
596
	 * @return Models\AccountStatus
597
	 */
598 1
	public function status()
599
	{
600 1
		$url = "https://status.liveperson.com/json?site={$this->account}";
601
		
602 1
		$response = $this->requestV1($url, 'GET');
603
		
604 1
		return new AccountStatus((array) $response);
605
	}
606
	
607
	/**
608
	 * login function.
609
	 * 
610
	 * @access public
611
	 * @return this
612
	 */
613 1
	public function login()
614
	{
615 1
		$this->domain('agentVep');
616
		
617 1
		$consumer_key = config("{$this->config}.key");
618 1
		$consumer_secret = config("{$this->config}.secret");
619 1
		$token = config("{$this->config}.token");
620 1
		$secret = config("{$this->config}.token_secret");
621 1
		$username = config("{$this->config}.user_name");
622
		
623
		$auth = [
624 1
			'username'		  => $username,
625 1
			'appKey'			=> $consumer_key,
626 1
			'secret'			=> $consumer_secret,
627 1
			'accessToken'		=> $token,
628 1
			'accessTokenSecret' => $secret,
629
		];
630
		
631 1
		$url = "https://{$this->domain}/api/account/{$this->account}/login?v=1.3";
632
		
633 1
		$response = $this->requestV1($url, 'POST', $auth);
634
		
635 1
		$this->bearer = $response->bearer;
636
		
637 1
		return $this;
638
	}
639
	
640
	/**
641
	 * requestV2 function.
642
	 * 
643
	 * @access private
644
	 * @param mixed $url
645
	 * @param mixed $method
646
	 * @param mixed $payload (default: [])
647
	 * @param mixed $headers (default: [])
648
	 * @return void
649
	 */
650 1
	private function requestV2($url, $method, $payload = [], $headers = [])
651
	{
652 1
		$this->login();
653
		
654 1
		$client = new Client();
655
		$args = [
656 1
			'headers' => array_merge([
657 1
				'content-type' => 'application/json',
658 1
				'Authorization' => 'Bearer ' . $this->bearer
659 1
			], $headers),
660 1
			'body' => json_encode($payload)
661
		];
662
		
663
		// @codeCoverageIgnoreStart
664
		try {
665
			$res = $client->request($method, $url, $args);
666
		} catch (\Exception $e) {
667
			throw $e;
668
		}
669
		// @codeCoverageIgnoreEnd
670
		
671 1
		return json_decode($res->getBody());
672
	}
673
	
674
	/**
675
	 * requestClient function.
676
	 * 
677
	 * @access private
678
	 * @return \GuzzleHttp\Client
679
	 */
680 2
	private function requestClient()
681
	{
682 2
		$consumer_key = config("{$this->config}.key");
683 2
		$consumer_secret = config("{$this->config}.secret");
684 2
		$token = config("{$this->config}.token");
685 2
		$secret = config("{$this->config}.token_secret");
686
687 2
		$stack = HandlerStack::create();
688 2
		$auth = new Oauth1([
689 2
			'consumer_key'	=> $consumer_key,
690 2
			'consumer_secret' => $consumer_secret,
691 2
			'token'		   => $token,
692 2
			'token_secret'	=> $secret,
693 2
			'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC,
694
		]);
695 2
		$stack->push($auth);
696
697 2
		$client = new Client([
698 2
			'handler' => $stack,
699
		]);
700
		
701 2
		return $client;
702
	}
703
	
704
	/**
705
	 * requestV1 - request bootstrap for older oauth supported APIs.
706
	 * 
707
	 * @access private
708
	 * @param string $url
709
	 * @param string $method
710
	 * @param array $payload (default: [])
711
	 * @return mixed
712
	 */
713 1
	private function requestV1($url, $method, $payload = [])
714
	{
715 1
		$client = $this->requestClient();
716
717
		$args = [
718 1
			'auth' => 'oauth',
719
			'headers' => [
720
				'content-type' => 'application/json',
721
			],
722 1
			'body' => json_encode($payload)
723
		];
724
725
		try {
726 1
			$res = $client->request($method, $url, $args);
727 1
			$response = json_decode($res->getBody());
728
		} catch (\Exception $e) {
729
			// @codeCoverageIgnoreStart
730
			if ($this->retry_counter < $this->retry_limit || $this->retry_limit == -1) {
731
				usleep(1500);
732
				$this->retry_counter++;
733
				$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

733
				$response = $this->requestV1($url, /** @scrutinizer ignore-type */ $payload);
Loading history...
734
			} else {
735
				throw $e;
736
			}
737
			// @codeCoverageIgnoreEnd
738
		}
739
740 1
		return $response;
741
	}
742
}