Total Complexity | 56 |
Total Lines | 393 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like LiveEngageLaravel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LiveEngageLaravel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class LiveEngageLaravel |
||
22 | { |
||
23 | private $account = false; |
||
24 | private $results = []; |
||
25 | private $skills = []; |
||
26 | private $next = false; |
||
27 | private $prev = false; |
||
28 | private $start; |
||
29 | private $end; |
||
30 | private $config = 'services.liveperson.default'; |
||
31 | private $version = '1.0'; |
||
32 | private $history_limit = 50; |
||
33 | private $history = false; |
||
34 | private $context = 'interactionHistoryRecords'; |
||
35 | private $interactive = true; |
||
36 | private $ended = true; |
||
37 | private $bearer = false; |
||
38 | |||
39 | private $domain = false; |
||
40 | |||
41 | private $retry_limit = 5; |
||
42 | private $retry_counter = 0; |
||
43 | |||
44 | public function __get($attribute) |
||
47 | } |
||
48 | |||
49 | public function __set($attribute, $value) |
||
50 | { |
||
51 | return $this->$attribute = $value; |
||
52 | } |
||
53 | |||
54 | public function __construct() |
||
59 | } |
||
60 | |||
61 | public function key($key = 'default') |
||
62 | { |
||
63 | $this->config = "services.liveperson.$key"; |
||
64 | |||
65 | return $this; |
||
66 | } |
||
67 | |||
68 | public function nonInteractive() |
||
69 | { |
||
70 | $this->interactive = false; |
||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | public function active() |
||
75 | { |
||
76 | $this->ended = false; |
||
77 | return $this; |
||
78 | } |
||
79 | |||
80 | public function limit($limit) |
||
81 | { |
||
82 | $this->history_limit = $limit; |
||
83 | |||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | public function skills($skills) |
||
88 | { |
||
89 | $this->skills = $skills; |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | public function retry($limit) |
||
95 | { |
||
96 | $this->retry_limit = $limit; |
||
97 | |||
98 | return $this; |
||
99 | } |
||
100 | |||
101 | public function get() |
||
102 | { |
||
103 | return $this->results; |
||
104 | } |
||
105 | |||
106 | public function account($accountid) |
||
107 | { |
||
108 | $this->account = $accountid; |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | public function domain($service) |
||
114 | { |
||
115 | $response = $this->requestV1("https://api.liveperson.net/api/account/{$this->account}/service/{$service}/baseURI.json?version={$this->version}", 'GET'); |
||
116 | if (is_a($response, 'Exception')) { |
||
|
|||
117 | throw new \Exception('Unable to get LivePerson account domain', 101); |
||
118 | } else { |
||
119 | $this->domain = $response->baseURI; |
||
120 | |||
121 | return $this; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | public function visitor($visitorID, $sessionID, $setData = false) |
||
126 | { |
||
127 | $this->domain('smt'); |
||
128 | |||
129 | if ($setData) { |
||
130 | $url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/events?v=1&sid={$sessionID}"; |
||
131 | |||
132 | return $this->requestV1($url, 'POST', $setData); |
||
133 | } else { |
||
134 | $url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/state?v=1&sid={$sessionID}"; |
||
135 | |||
136 | return $this->requestV1($url, 'GET'); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | final public function retrieveHistory(Carbon $start, Carbon $end, $url = false) |
||
141 | { |
||
142 | $this->domain('engHistDomain'); |
||
143 | |||
144 | $url = $url ?: "https://{$this->domain}/interaction_history/api/account/{$this->account}/interactions/search?limit={$this->history_limit}&offset=0"; |
||
145 | |||
146 | $start_str = $start->toW3cString(); |
||
147 | $end_str = $end->toW3cString(); |
||
148 | |||
149 | $data = [ |
||
150 | 'interactive' => $this->interactive, |
||
151 | 'ended' => $this->ended, |
||
152 | 'start' => [ |
||
153 | 'from' => strtotime($start_str).'000', |
||
154 | 'to' => strtotime($end_str).'000', |
||
155 | ], |
||
156 | ]; |
||
157 | if (count($this->skills)) { |
||
158 | $data['skillIds'] = $this->skills; |
||
159 | } |
||
160 | |||
161 | $data = new Payload($data); |
||
162 | |||
163 | return $this->requestV1($url, 'POST', $data); |
||
164 | } |
||
165 | |||
166 | final public function retrieveMsgHistory(Carbon $start, Carbon $end, $url = false) |
||
167 | { |
||
168 | $this->domain('msgHist'); |
||
169 | |||
170 | $url = $url ?: "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/search?limit={$this->history_limit}&offset=0"; |
||
171 | |||
172 | $start_str = $start->toW3cString(); |
||
173 | $end_str = $end->toW3cString(); |
||
174 | |||
175 | $data = [ |
||
176 | 'interactive' => $this->interactive, |
||
177 | 'ended' => $this->ended, |
||
178 | 'start' => [ |
||
179 | 'from' => strtotime($start_str).'000', |
||
180 | 'to' => strtotime($end_str).'000', |
||
181 | ], |
||
182 | ]; |
||
183 | if (count($this->skills)) { |
||
184 | $data['skillIds'] = $this->skills; |
||
185 | } |
||
186 | |||
187 | $data = new Payload($data); |
||
188 | |||
189 | return $this->requestV1($url, 'POST', $data); |
||
190 | } |
||
191 | |||
192 | public function getAgentStatus(array $skills) |
||
193 | { |
||
194 | |||
195 | $this->domain('msgHist'); |
||
196 | |||
197 | $url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/agent-view/status"; |
||
198 | |||
199 | $data['skillIds'] = $skills; |
||
200 | |||
201 | $response = $this->requestV1($url, 'POST', $data); |
||
202 | $agents = []; |
||
203 | |||
204 | foreach ($response->agentStatusRecords as $agent) { |
||
205 | $agents[] = new Agent((array) $agent); |
||
206 | } |
||
207 | |||
208 | $collection = new Humans($agents); |
||
209 | $collection->metaData = $response->_metadata; |
||
210 | |||
211 | return $collection; |
||
212 | |||
213 | } |
||
214 | |||
215 | public function messagingHistory(Carbon $start = null, Carbon $end = null) |
||
216 | { |
||
217 | $this->retry_counter = 0; |
||
218 | |||
219 | $start = $start ?: (new Carbon())->today(); |
||
220 | $end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59); |
||
221 | |||
222 | $this->start = $start; |
||
223 | $this->end = $end; |
||
224 | |||
225 | if (is_object($results_object)) { |
||
226 | |||
227 | $results_object = $this->retrieveMsgHistory($start, $end); |
||
228 | $results = $results_object->conversationHistoryRecords; |
||
229 | if (property_exists($results_object->_metadata, 'next')) { |
||
230 | $this->next = $results_object->_metadata->next->href; |
||
231 | } |
||
232 | if (property_exists($results_object->_metadata, 'prev')) { |
||
233 | $this->prev = $results_object->_metadata->prev->href; |
||
234 | } |
||
235 | |||
236 | $history = []; |
||
237 | foreach ($results as $item) { |
||
238 | if (property_exists($item, 'info')) { |
||
239 | $item->info = new Info((array) $item->info); |
||
240 | } |
||
241 | |||
242 | if (property_exists($item, 'visitorInfo')) { |
||
243 | $item->visitorInfo = new Visitor((array) $item->visitorInfo); |
||
244 | } |
||
245 | |||
246 | if (property_exists($item, 'campaign')) { |
||
247 | $item->campaign = new Campaign((array) $item->campaign); |
||
248 | } |
||
249 | |||
250 | $history[] = new Conversation((array) $item); |
||
251 | } |
||
252 | |||
253 | $collection = new ConversationHistory($history, $this); |
||
254 | $collection->metaData = $results_object->_metadata; |
||
255 | |||
256 | return $collection; |
||
257 | |||
258 | } else { |
||
259 | return false; |
||
260 | } |
||
261 | } |
||
262 | |||
263 | public function history(Carbon $start = null, Carbon $end = null) |
||
264 | { |
||
265 | $this->retry_counter = 0; |
||
266 | |||
267 | $start = $start ?: (new Carbon())->today(); |
||
268 | $end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59); |
||
269 | |||
270 | $this->start = $start; |
||
271 | $this->end = $end; |
||
272 | |||
273 | $results_object = $this->retrieveHistory($start, $end); |
||
274 | |||
275 | if (is_object($results_object)) { |
||
276 | |||
277 | $results = $results_object->interactionHistoryRecords; |
||
278 | if (property_exists($results_object->_metadata, 'next')) { |
||
279 | $this->next = $results_object->_metadata->next->href; |
||
280 | } |
||
281 | if (property_exists($results_object->_metadata, 'prev')) { |
||
282 | $this->prev = $results_object->_metadata->prev->href; |
||
283 | } |
||
284 | |||
285 | $history = []; |
||
286 | foreach ($results as $item) { |
||
287 | if (property_exists($item, 'info')) { |
||
288 | $item->info = new Info((array) $item->info); |
||
289 | } |
||
290 | |||
291 | if (property_exists($item, 'visitorInfo')) { |
||
292 | $item->visitorInfo = new Visitor((array) $item->visitorInfo); |
||
293 | } |
||
294 | |||
295 | if (property_exists($item, 'campaign')) { |
||
296 | $item->campaign = new Campaign((array) $item->campaign); |
||
297 | } |
||
298 | |||
299 | $history[] = new Engagement((array) $item); |
||
300 | } |
||
301 | |||
302 | $collection = new EngagementHistory($history, $this); |
||
303 | $collection->metaData = $results_object->_metadata; |
||
304 | |||
305 | return $collection; |
||
306 | |||
307 | } else { |
||
308 | return false; |
||
309 | } |
||
310 | } |
||
311 | |||
312 | public function login() |
||
337 | } |
||
338 | |||
339 | private function requestV2($url, $method, $payload = false) |
||
340 | { |
||
361 | } |
||
362 | |||
363 | private function requestV1($url, $method, $payload = false) |
||
364 | { |
||
365 | $consumer_key = config("{$this->config}.key"); |
||
366 | $consumer_secret = config("{$this->config}.secret"); |
||
367 | $token = config("{$this->config}.token"); |
||
414 | } |
||
415 | } |
||
416 |