Issues (38)

src/LiveEngageRequest.php (2 issues)

Severity
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
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
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
	private $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 1
	public function login($user = false, $pass = false)
76
	{
77
		/** @scrutinizer ignore-call */
78 1
		$le = LiveEngage::domain('agentVep');
79 1
		$domain = $le->domain;
80 1
		$account = $le->account;
81
		
82 1
		$consumer_key = config("{$this->config}.key");
83 1
		$consumer_secret = config("{$this->config}.secret");
84 1
		$token = config("{$this->config}.token");
85 1
		$secret = config("{$this->config}.token_secret");
86 1
		$username = config("{$this->config}.user_name");
87
		
88 1
		if ($user && $pass) {
89
			$auth = [
90
				'username'			=> $user,
91
				'password'			=> $pass
92
			];
93
		} else {
94
			$auth = [
95 1
				'username'			=> $username,
96 1
				'appKey'			=> $consumer_key,
97 1
				'secret'			=> $consumer_secret,
98 1
				'accessToken'		=> $token,
99 1
				'accessTokenSecret' => $secret,
100
			];
101
		}
102
		
103 1
		$url = "https://{$domain}/api/account/{$account}/login?v=1.3";
104
		
105
		try {
106 1
			$response = $this->V1($url, 'POST', $auth, true);
107
		} catch (\GuzzleHttp\Exception\ServerException $e) {
108
			throw $e; //new LoginFailure();
109
		}
110
		
111 1
		$this->bearer = $response->bearer;
112
		
113 1
		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 1
	public function V1($url, $method, $payload = null, $noauth = false)
127
	{
128 1
		$client = $this->requestClient($noauth);
129
130
		$args = [
131 1
			'auth' => 'oauth',
132
			'headers' => [
133
				'content-type' => 'application/json',
134
				'accept' => 'application/json'
135
			],
136 1
			'body' => json_encode($payload ?: [])
137
		];
138
		
139 1
		if ($noauth) unset($args['auth']);
140
		
141
		// @codeCoverageIgnoreStart
142
		try {
143
			$res = $client->request($method, $url, $args);
144
			$response = json_decode($res->getBody());
145
		} catch (\GuzzleHttp\Exception\ServerException $e) {
146
			throw $e;
147
		} catch (\GuzzleHttp\Exception\ClientException $e) {
148
			$code = $e->getResponse()->getStatusCode();
149
			if ($code == 401) {
150
				throw new LoginFailure();
151
			} else {
152
				throw $e;
153
			}
154
		} catch (\Exception $e) {
155
			throw $e;
156
		}
157
		// @codeCoverageIgnoreEnd
158
159 1
		return $response;
160
	}
161
	
162
	/**
163
	 * V2
164
	 * 
165
	 * @access public
166
	 * @param mixed $url
167
	 * @param mixed $method
168
	 * @param mixed $payload (default: [])
169
	 * @param mixed $headers (default: [])
170
	 * @return mixed
171
	 */
172 1
	public function V2($url, $method, $payload = null, $headers = null)
173
	{
174 1
		if (!$this->bearer) $this->login();
175
		
176 1
		$client = new Client();
177
		$args = [
178 1
			'headers' => array_merge([
179 1
				'content-type' => 'application/json',
180 1
				'accept' => 'application/json',
181 1
				'Authorization' => 'Bearer ' . $this->bearer
182 1
			], $headers ?: []),
183 1
			'body' => json_encode($payload ?: [])
184
		];
185
		
186
		// @codeCoverageIgnoreStart
187
		try {
188
			$res = $client->request($method, $url, $args);
189
		} catch (\Exception $e) {
190
			throw $e;
191
		}
192
		// @codeCoverageIgnoreEnd
193
		
194 1
		return json_decode($res->getBody());
195
	}
196
	
197
	/**
198
	 * requestClient
199
	 * 
200
	 * @access private
201
	 * @return \GuzzleHttp\Client
202
	 * @param bool $noauth (default: false)
203
	 */
204 1
	private function requestClient($noauth = false)
205
	{
206 1
		if ($noauth) {
207
			return new Client();
208
		}
209
		
210 1
		$consumer_key = config("{$this->config}.key");
211 1
		$consumer_secret = config("{$this->config}.secret");
212 1
		$token = config("{$this->config}.token");
213 1
		$secret = config("{$this->config}.token_secret");
214
215 1
		$stack = HandlerStack::create();
216 1
		$auth = new Oauth1([
217 1
			'consumer_key'	=> $consumer_key,
218 1
			'consumer_secret' => $consumer_secret,
219 1
			'token'		   => $token,
220 1
			'token_secret'	=> $secret,
221 1
			'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC,
222
		]);
223 1
		$stack->push($auth);
224
225 1
		$client = new Client([
226 1
			'handler' => $stack,
227
		]);
228
		
229 1
		return $client;
230
	}
231
}