Skip to content

Commit 97d8ddd

Browse files
author
Amro Khaled | Commentatk
committed
:recylce: Handle response data from curl request
1 parent 97a3a2a commit 97d8ddd

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

src/Services/Like4CardAPI.php

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99

1010
namespace CodeBugLab\Like4Card\Services;
1111

12+
use Exception;
1213
use Illuminate\Support\Carbon;
1314
use CodeBugLab\Like4Card\Contracts\Like4CardInterface;
15+
use CodeBugLab\Like4Card\Exceptions\ProductsNotFoundException;
16+
use CodeBugLab\Like4Card\Exceptions\WrongCredentialsException;
1417

1518
class Like4CardAPI implements Like4CardInterface
1619
{
@@ -30,8 +33,31 @@ class Like4CardAPI implements Like4CardInterface
3033
*/
3134
private static function cURL($url, $json = [])
3235
{
33-
dd('there');
36+
$response = self::executeCURL($url, $json);
3437

38+
if ($response->response > 0) {
39+
return $response;
40+
}
41+
42+
switch (optional($response)->message) {
43+
case "Incorrect Login - invalid email or password":
44+
throw new WrongCredentialsException($response->message);
45+
case "No available products":
46+
throw new ProductsNotFoundException($response->message);
47+
default:
48+
throw new Exception(optional($response)->message ?? "Unknown error");
49+
}
50+
}
51+
52+
/**
53+
* Prepare CURL options before execution
54+
*
55+
* @param string $url
56+
* @param array $json
57+
* @return object
58+
*/
59+
private static function executeCURL(string $url, array $json)
60+
{
3561
$ch = curl_init();
3662

3763
$headers = array();
@@ -93,8 +119,10 @@ public static function balance()
93119
*/
94120
public static function categories()
95121
{
96-
return self::cURL(
97-
"categories"
122+
return optional(
123+
self::cURL(
124+
"categories"
125+
)
98126
)->data;
99127
}
100128

@@ -106,12 +134,14 @@ public static function categories()
106134
*/
107135
public static function products(array $ids)
108136
{
109-
$request = self::cURL(
110-
"products",
111-
["ids[]" => implode(",", $ids)]
112-
);
113-
114-
return isset($request->data) ? $request->data : $request->message;
137+
return optional(
138+
self::cURL(
139+
"products",
140+
[
141+
"ids[]" => implode(",", $ids)
142+
]
143+
)
144+
)->data;
115145
}
116146

117147
/**
@@ -122,12 +152,12 @@ public static function products(array $ids)
122152
*/
123153
public static function getProductsByCategoryId(int $category_id)
124154
{
125-
$request = self::cURL(
126-
"products",
127-
["categoryId" => $category_id]
128-
);
129-
130-
return isset($request->data) ? $request->data : $request->message;
155+
return optional(
156+
self::cURL(
157+
"products",
158+
["categoryId" => $category_id]
159+
)
160+
)->data;
131161
}
132162

133163
/**
@@ -138,9 +168,11 @@ public static function getProductsByCategoryId(int $category_id)
138168
*/
139169
public static function orders(array $options = [])
140170
{
141-
return self::cURL(
142-
"orders",
143-
$options
171+
return optional(
172+
self::cURL(
173+
"orders",
174+
$options
175+
)
144176
)->data;
145177
}
146178

@@ -171,7 +203,7 @@ public static function createOrder(int $product_id, int $local_id)
171203
{
172204
$time = now();
173205

174-
return self::cURL(
206+
$order = self::cURL(
175207
"create_order",
176208
[
177209
'referenceId' => $local_id,
@@ -181,6 +213,10 @@ public static function createOrder(int $product_id, int $local_id)
181213
'hash' => self::generateHash($time)
182214
]
183215
);
216+
217+
logger($order);
218+
219+
return $order;
184220
}
185221

186222
/**

tests/Mock/Like4CardAPIMock.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Like4CardAPIMock extends Like4CardAPI
1111
{
1212
private static function cURL($url, $json = [])
1313
{
14-
$response = self::prepareCURL($url, $json);
14+
$response = self::executeCURL($url, $json);
1515

1616
if ($response->response > 0) {
1717
return $response;
@@ -27,7 +27,7 @@ private static function cURL($url, $json = [])
2727
}
2828
}
2929

30-
private static function prepareCURL($url, $json)
30+
private static function executeCURL($url, $json)
3131
{
3232
return json_decode(
3333
file_get_contents(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"response": 0,
3+
"message": "No Data About This Order"
4+
}

0 commit comments

Comments
 (0)