Passed
Push — master ( a2c862...16044f )
by Robert
05:35
created

LiveEngageRequest::requestClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
14
/**
15
 * LiveEngageRequest class.
16
 */
17
class LiveEngageRequest
18
{
19
	/**
20
	 * config
21
	 * 
22
	 * @var mixed
23
	 * @access private
24
	 */
25
	private $config;
26
	
27
	/**
28
	 * __construct function.
29
	 * 
30
	 * @access public
31
	 * @param mixed $config
32
	 * @return void
33
	 */
34
	public function __construct($config)
35
	{
36
		$this->config = $config; // @codeCoverageIgnore
37
	}
38
	
39
	/**
40
	 * login
41
	 * 
42
	 * @access public
43
	 * @return this
44
	 */
45 1
	public function login()
46
	{
47 1
		$le = LiveEngage::domain('agentVep');
0 ignored issues
show
Bug introduced by
The method domain() does not exist on LivePersonInc\LiveEngage...cades\LiveEngageLaravel. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

47
		/** @scrutinizer ignore-call */ 
48
  $le = LiveEngage::domain('agentVep');
Loading history...
48 1
		$domain = $le->domain;
49 1
		$account = $le->account;
50
		
51 1
		$consumer_key = config("{$this->config}.key");
52 1
		$consumer_secret = config("{$this->config}.secret");
53 1
		$token = config("{$this->config}.token");
54 1
		$secret = config("{$this->config}.token_secret");
55 1
		$username = config("{$this->config}.user_name");
56
		
57
		$auth = [
58 1
			'username'		  => $username,
59 1
			'appKey'			=> $consumer_key,
60 1
			'secret'			=> $consumer_secret,
61 1
			'accessToken'		=> $token,
62 1
			'accessTokenSecret' => $secret,
63
		];
64
		
65 1
		$url = "https://{$domain}/api/account/{$account}/login?v=1.3";
66
		
67 1
		$response = $this->V1($url, 'POST', $auth);
68
		
69 1
		$this->bearer = $response->bearer;
0 ignored issues
show
Bug Best Practice introduced by
The property bearer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
		
71 1
		return $this;
72
	}
73
	
74
	/**
75
	 * V1
76
	 * 
77
	 * @access public
78
	 * @param string $url
79
	 * @param string $method
80
	 * @param array $payload (default: [])
81
	 * @return mixed
82
	 */
83 1
	public function V1($url, $method, $payload = [])
84
	{
85 1
		$client = $this->requestClient();
86
87
		$args = [
88 1
			'auth' => 'oauth',
89
			'headers' => [
90
				'content-type' => 'application/json',
91
			],
92 1
			'body' => json_encode($payload)
93
		];
94
95
		try {
96 1
			$res = $client->request($method, $url, $args);
97 1
			$response = json_decode($res->getBody());
98
		} catch (\Exception $e) {
99
			// @codeCoverageIgnoreStart
100
			if ($this->retry_counter < $this->retry_limit || $this->retry_limit == -1) {
0 ignored issues
show
Bug Best Practice introduced by
The property retry_counter does not exist on LivePersonInc\LiveEngageLaravel\LiveEngageRequest. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property retry_limit does not exist on LivePersonInc\LiveEngageLaravel\LiveEngageRequest. Did you maybe forget to declare it?
Loading history...
101
				usleep(1500);
102
				$this->retry_counter++;
103
				$response = $this->requestV1($url, $payload);
0 ignored issues
show
Bug introduced by
The method requestV1() does not exist on LivePersonInc\LiveEngageLaravel\LiveEngageRequest. ( Ignorable by Annotation )

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

103
				/** @scrutinizer ignore-call */ 
104
    $response = $this->requestV1($url, $payload);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
			} else {
105
				throw $e;
106
			}
107
			// @codeCoverageIgnoreEnd
108
		}
109
110 1
		return $response;
111
	}
112
	
113
	/**
114
	 * V2
115
	 * 
116
	 * @access public
117
	 * @param mixed $url
118
	 * @param mixed $method
119
	 * @param mixed $payload (default: [])
120
	 * @param mixed $headers (default: [])
121
	 * @return void
122
	 */
123 1
	public function V2($url, $method, $payload = [], $headers = [])
124
	{
125 1
		$this->login();
126
		
127 1
		$client = new Client();
128
		$args = [
129 1
			'headers' => array_merge([
130 1
				'content-type' => 'application/json',
131 1
				'Authorization' => 'Bearer ' . $this->bearer
132 1
			], $headers),
133 1
			'body' => json_encode($payload)
134
		];
135
		
136
		// @codeCoverageIgnoreStart
137
		try {
138
			$res = $client->request($method, $url, $args);
139
		} catch (\Exception $e) {
140
			throw $e;
141
		}
142
		// @codeCoverageIgnoreEnd
143
		
144 1
		return json_decode($res->getBody());
145
	}
146
	
147
	/**
148
	 * requestClient
149
	 * 
150
	 * @access private
151
	 * @return \GuzzleHttp\Client
152
	 */
153 1
	private function requestClient()
154
	{
155 1
		$consumer_key = config("{$this->config}.key");
156 1
		$consumer_secret = config("{$this->config}.secret");
157 1
		$token = config("{$this->config}.token");
158 1
		$secret = config("{$this->config}.token_secret");
159
160 1
		$stack = HandlerStack::create();
161 1
		$auth = new Oauth1([
162 1
			'consumer_key'	=> $consumer_key,
163 1
			'consumer_secret' => $consumer_secret,
164 1
			'token'		   => $token,
165 1
			'token_secret'	=> $secret,
166 1
			'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC,
167
		]);
168 1
		$stack->push($auth);
169
170 1
		$client = new Client([
171 1
			'handler' => $stack,
172
		]);
173
		
174 1
		return $client;
175
	}
176
}