Passed
Push — master ( f2603d...49d68e )
by Robert
05:07
created

LiveEngageLaravel::domain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

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 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\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) return false;
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
		if (!$results_object) return false;
223
		
224 1
		$results_object->_metadata->start = $start;
225 1
		$results_object->_metadata->end = $end;
226
	
227 1
		$meta = new MetaData((array) $results_object->_metadata);
228
		
229 1
		$collection = new EngagementHistory($results_object->interactionHistoryRecords);
230 1
		$collection->metaData = $meta;
231
		
232 1
		return $collection;
233
			
234
	}
235
	
236
	public function login()
237
	{
238
		$this->domain('agentVep');
239
		
240
		$consumer_key = config("{$this->config}.key");
241
		$consumer_secret = config("{$this->config}.secret");
242
		$token = config("{$this->config}.token");
243
		$secret = config("{$this->config}.token_secret");
244
		$username = config("{$this->config}.user_name");
245
		
246
		$auth = [
247
			'username'		  => $username,
248
			'appKey'			=> $consumer_key,
249
			'secret'			=> $consumer_secret,
250
			'accessToken'		=> $token,
251
			'accessTokenSecret' => $secret,
252
		];
253
		
254
		$url = "https://{$this->domain}/api/account/{$this->account}/login?v=1.3";
255
		
256
		$response = $this->requestV1($url, 'POST', $auth);
257
		
258
		$this->bearer = $response->bearer;
259
		
260
		return $this;
261
	}
262
	
263
	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...
264
	{
265
		$this->login();
266
		
267
		$client = new Client();
268
		$args = [
269
			'headers' => [
270
				'content-type' => 'application/json',
271
			],
272
			'body' => json_encode($payload)
273
		];
274
		
275
		try {
276
			$res = $client->request($method, $url, $args);
277
		} catch (\Exception $e) {
278
			throw $e;
279
		} 
280
		
281
		return json_decode($res->getBody());
282
	}
283
	
284 2
	private function requestClient()
285
	{
286 2
		$consumer_key = config("{$this->config}.key");
287 2
		$consumer_secret = config("{$this->config}.secret");
288 2
		$token = config("{$this->config}.token");
289 2
		$secret = config("{$this->config}.token_secret");
290
291 2
		$stack = HandlerStack::create();
292 2
		$auth = new Oauth1([
293 2
			'consumer_key'	=> $consumer_key,
294 2
			'consumer_secret' => $consumer_secret,
295 2
			'token'		   => $token,
296 2
			'token_secret'	=> $secret,
297 2
			'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC,
298
		]);
299 2
		$stack->push($auth);
300
301 2
		$client = new Client([
302 2
			'handler' => $stack,
303
		]);
304
		
305 2
		return $client;
306
	}
307
	
308 1
	private function requestV1($url, $method, $payload = [])
309
	{
310 1
		$client = $this->requestClient();
311
312
		$args = [
313 1
			'auth' => 'oauth',
314
			'headers' => [
315
				'content-type' => 'application/json',
316
			],
317 1
			'body' => json_encode($payload)
318
		];
319
320
		try {
321 1
			$res = $client->request($method, $url, $args);
322 1
			$response = json_decode($res->getBody());
323
		} catch (\Exception $e) {
324
			if ($this->retry_counter < $this->retry_limit || $this->retry_limit == -1) {
325
				usleep(1500);
326
				$this->retry_counter++;
327
				$response = $this->requestV1($url, $payload);
328
			} else {
329
				throw $e; //new LiveEngageException("Retry limit has been exceeded ($this->retry_limit)", 100);
330
			}
331
		}
332
333 1
		return $response;
334
	}
335
}
336