Test Failed
Branch development (1f4e65)
by Robert
11:57
created

LiveEngageRequest::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 6
1
<?php
2
/**
3
 * LiveEngageRequest class.
4
 */
5
	
6
namespace LivePersonInc\LiveEngageLaravel;
7
8
use LivePersonInc\LiveEngageLaravel\Facades\LiveEngageLaravel as LiveEngage;
9
use GuzzleHttp\Client;
10
use GuzzleHttp\HandlerStack;
11
use GuzzleHttp\Subscriber\Oauth\Oauth1;
12
use LivePersonInc\LiveEngageLaravel\Models\Payload;
13
use LivePersonInc\LiveEngageLaravel\Exceptions\LiveEngageException;
14
use LivePersonInc\LiveEngageLaravel\Exceptions\LoginFailure;
15
16
/**
17
 * LiveEngageRequest class.
18
 */
19
class LiveEngageRequest
20
{
21
	/**
22
	 * config
23
	 * 
24
	 * @var mixed
25
	 * @access private
26
	 */
27
	private $config;
28
	
29
	/**
30
	 * retry_limit
31
	 * 
32
	 * (default value: 3)
33
	 * 
34
	 * @var int
35
	 * @access private
36
	 */
37
	private $retry_limit = 3;
0 ignored issues
show
introduced by
The private property $retry_limit is not used, and could be removed.
Loading history...
38
	/**
39
	 * retry_counter
40
	 * 
41
	 * (default value: 0)
42
	 * 
43
	 * @var int
44
	 * @access private
45
	 */
46
	private $retry_counter = 0;
0 ignored issues
show
introduced by
The private property $retry_counter is not used, and could be removed.
Loading history...
47
	/**
48
	 * bearer
49
	 * 
50
	 * @var mixed
51
	 * @access private
52
	 */
53
	public $bearer;
54
	
55
	/**
56
	 * __construct function.
57
	 * 
58
	 * @access public
59
	 * @param mixed $config
60
	 * @return void
61
	 */
62
	public function __construct($config)
63
	{
64
		$this->config = $config; // @codeCoverageIgnore
65
	}
66
	
67
	/**
68
	 * login function.
69
	 * 
70
	 * @access public
71
	 * @param string $user (default: null)
72
	 * @param string $pass (default: null)
73
	 * @return object
74
	 */
75
	public function login($user = false, $pass = false)
76
	{
77
		/** @scrutinizer ignore-call */
78
		$le = LiveEngage::domain('agentVep');
79
		$domain = $le->domain;
80
		$account = $le->account;
81
		
82
		$consumer_key = config("{$this->config}.key");
83
		$consumer_secret = config("{$this->config}.secret");
84
		$token = config("{$this->config}.token");
85
		$secret = config("{$this->config}.token_secret");
86
		$username = config("{$this->config}.user_name");
87
		
88
		if ($user && $pass) {
89
			$auth = [
90
				'username'			=> $user,
91
				'password'			=> $pass
92
			];
93
		} else {
94
			$auth = [
95
				'username'			=> $username,
96
				'appKey'			=> $consumer_key,
97
				'secret'			=> $consumer_secret,
98
				'accessToken'		=> $token,
99
				'accessTokenSecret' => $secret,
100
			];
101
		}
102
		
103
		$url = "https://{$domain}/api/account/{$account}/login?v=1.3";
104
		
105
		try {
106
			$response = $this->get('V1', $url, 'POST', $auth, [], true);
107
		} catch (\GuzzleHttp\Exception\ServerException $e) {
108
			throw $e; //new LoginFailure();
109
		}
110
		
111
		$this->bearer = $response->body->bearer;
112
		
113
		return $this;
114
	}
115
	
116
	/**
117
	 * V1
118
	 * 
119
	 * @access public
120
	 * @param string $url
121
	 * @param string $method
122
	 * @param array $payload (default: [])
123
	 * @param bool $noauth (default: false)
124
	 * @return mixed
125
	 */
126
	public function V1($url, $method, $payload = null, $headers = null, $noauth = false)
127
	{
128
		$client = $this->requestClient($noauth);
129
		
130
		$args = [
131
			'auth' => 'oauth',
132
			'headers' => array_merge([
133
				'content-type' => 'application/json',
134
				'accept' => 'application/json'
135
			], $headers ?: []),
136
			'body' => json_encode($payload ?: [])
137
		];
138
		
139
		if ($noauth) unset($args['auth']);
140
		
141
		// @codeCoverageIgnoreStart
142
		try {
143
			$res = $client->request($method, $url, $args);
144
		} catch (\GuzzleHttp\Exception\ServerException $e) {
145
			throw $e;
146
		} catch (\GuzzleHttp\Exception\ClientException $e) {
147
			$code = $e->getResponse()->getStatusCode();
148
			if ($code == 401) {
149
				throw new LoginFailure();
150
			} else {
151
				throw $e;
152
			}
153
		} catch (\Exception $e) {
154
			throw $e;
155
		}
156
		// @codeCoverageIgnoreEnd
157
158
		return $res;
159
	}
160
	
161
	/**
162
	 * V2
163
	 * 
164
	 * @access public
165
	 * @param mixed $url
166
	 * @param mixed $method
167
	 * @param mixed $payload (default: [])
168
	 * @param mixed $headers (default: [])
169
	 * @return mixed
170
	 */
171
	public function V2($url, $method, $payload = null, $headers = null, $noauth = false)
172
	{
173
		if (!$this->bearer) $this->login();
174
		
175
		$client = new Client();
176
		$args = [
177
			'headers' => array_merge([
178
				'content-type' => 'application/json',
179
				'accept' => 'application/json',
180
				'Authorization' => 'Bearer ' . $this->bearer
181
			], $headers ?: []),
182
			'body' => json_encode($payload ?: [])
183
		];
184
		
185
		// @codeCoverageIgnoreStart
186
		try {
187
			$res = $client->request($method, $url, $args);
188
		} catch (\Exception $e) {
189
			throw $e;
190
		}
191
		// @codeCoverageIgnoreEnd
192
		
193
		return $res;
194
	}
195
	
196
	public function get($version, $url, $method, $payload = null, $headers = null, $noauth = false)
197
	{
198
		$response = $this->$version($url, $method, $payload, $headers, $noauth);
199
		
200
		$content = new \StdClass();
201
		$content->body = json_decode($response->getBody());
202
		$content->headers = $response->getHeaders();
203
		
204
		return $content;
205
	}
206
	
207
	/**
208
	 * requestClient
209
	 * 
210
	 * @access private
211
	 * @return \GuzzleHttp\Client
212
	 * @param bool $noauth (default: false)
213
	 */
214
	private function requestClient($noauth = false)
215
	{
216
		if ($noauth) {
217
			return new Client();
218
		}
219
		
220
		$consumer_key = config("{$this->config}.key");
221
		$consumer_secret = config("{$this->config}.secret");
222
		$token = config("{$this->config}.token");
223
		$secret = config("{$this->config}.token_secret");
224
225
		$stack = HandlerStack::create();
226
		$auth = new Oauth1([
227
			'consumer_key'	=> $consumer_key,
228
			'consumer_secret' => $consumer_secret,
229
			'token'		   => $token,
230
			'token_secret'	=> $secret,
231
			'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC,
232
		]);
233
		$stack->push($auth);
234
235
		$client = new Client([
236
			'handler' => $stack,
237
		]);
238
		
239
		return $client;
240
	}
241
}