Skip to content

Commit 89d0f8e

Browse files
committed
Added config for WP headers
1 parent 2bf615d commit 89d0f8e

File tree

3 files changed

+50
-58
lines changed

3 files changed

+50
-58
lines changed

src/Traits/WooCommerceTrait.php

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@ trait WooCommerceTrait
1515
*/
1616
public function all($endpoint = '', $options = [])
1717
{
18-
try {
19-
self::__construct();
18+
self::__construct();
2019

21-
return $this->client->get($endpoint, $options);
22-
} catch (\Exception $ex) {
23-
throw new \Exception($ex->getMessage(), 1);
24-
}
20+
return $this->client->get($endpoint, $options);
2521
}
2622

2723
/**
@@ -35,13 +31,9 @@ public function all($endpoint = '', $options = [])
3531
*/
3632
public function find($endpoint = '', $options = [])
3733
{
38-
try {
39-
self::__construct();
34+
self::__construct();
4035

41-
return $this->client->get($endpoint, $options);
42-
} catch (\Exception $ex) {
43-
throw new \Exception($ex->getMessage(), 1);
44-
}
36+
return $this->client->get($endpoint, $options);
4537
}
4638

4739
/**
@@ -55,13 +47,9 @@ public function find($endpoint = '', $options = [])
5547
*/
5648
public function create($endpoint, $data)
5749
{
58-
try {
59-
self::__construct();
50+
self::__construct();
6051

61-
return $this->client->post($endpoint, $data);
62-
} catch (\Exception $ex) {
63-
throw new \Exception($ex->getMessage(), 1);
64-
}
52+
return $this->client->post($endpoint, $data);
6553
}
6654

6755
/**
@@ -75,13 +63,9 @@ public function create($endpoint, $data)
7563
*/
7664
public function update($endpoint, $data)
7765
{
78-
try {
79-
self::__construct();
66+
self::__construct();
8067

81-
return $this->client->put($endpoint, $data);
82-
} catch (\Exception $ex) {
83-
throw new \Exception($ex->getMessage(), 1);
84-
}
68+
return $this->client->put($endpoint, $data);
8569
}
8670

8771
/**
@@ -95,13 +79,9 @@ public function update($endpoint, $data)
9579
*/
9680
public function delete($endpoint, $options = [])
9781
{
98-
try {
99-
self::__construct();
82+
self::__construct();
10083

101-
return $this->client->delete($endpoint, $options);
102-
} catch (\Exception $ex) {
103-
throw new \Exception($ex->getMessage(), 1);
104-
}
84+
return $this->client->delete($endpoint, $options);
10585
}
10686

10787
/**
@@ -111,11 +91,7 @@ public function delete($endpoint, $options = [])
11191
*/
11292
public function getRequest()
11393
{
114-
try {
115-
return $this->client->http->getRequest();
116-
} catch (\Exception $ex) {
117-
throw new \Exception($ex->getMessage(), 1);
118-
}
94+
return $this->client->http->getRequest();
11995
}
12096

12197
/**
@@ -125,11 +101,7 @@ public function getRequest()
125101
*/
126102
public function getResponse()
127103
{
128-
try {
129-
return $this->client->http->getResponse();
130-
} catch (\Exception $ex) {
131-
throw new \Exception($ex->getMessage(), 1);
132-
}
104+
return $this->client->http->getResponse();
133105
}
134106

135107
/**
@@ -139,7 +111,7 @@ public function getResponse()
139111
*/
140112
public function countResults()
141113
{
142-
return (int) $this->getResponse()->getHeaders()['X-WP-Total'];
114+
return (int) $this->getResponse()->getHeaders()[$this->headers['header_total']];
143115
}
144116

145117
/**
@@ -149,7 +121,7 @@ public function countResults()
149121
*/
150122
public function countPages()
151123
{
152-
return (int) $this->getResponse()->getHeaders()['X-WP-TotalPages'];
124+
return (int) $this->getResponse()->getHeaders()[$this->headers['header_total_pages']];
153125
}
154126

155127
/**

src/WooCommerceApi.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,34 @@ class WooCommerceApi
1414
*/
1515
protected $client;
1616

17+
/**
18+
*@var array
19+
*/
20+
protected $headers = [];
21+
1722
/**
1823
* Build Woocommerce connection.
1924
*
2025
* @return void
2126
*/
2227
public function __construct()
2328
{
24-
try {
25-
$this->client = new Client(
26-
config('woocommerce.store_url'),
27-
config('woocommerce.consumer_key'),
28-
config('woocommerce.consumer_secret'),
29-
[
30-
'version' => 'wc/'.config('woocommerce.api_version'),
31-
'wp_api' => config('woocommerce.wp_api_integration'),
32-
'verify_ssl' => config('woocommerce.verify_ssl'),
33-
'query_string_auth' => config('woocommerce.query_string_auth'),
34-
'timeout' => config('woocommerce.timeout'),
35-
]
36-
);
37-
} catch (\Exception $ex) {
38-
throw new \Exception($ex->getMessage(), 1);
39-
}
29+
$this->headers = [
30+
'header_total' => config('woocommerce.header_total') ?? 'X-WP-Total',
31+
'header_total_pages' => config('woocommerce.header_total_pages') ?? 'X-WP-TotalPages',
32+
];
33+
34+
$this->client = new Client(
35+
config('woocommerce.store_url'),
36+
config('woocommerce.consumer_key'),
37+
config('woocommerce.consumer_secret'),
38+
[
39+
'version' => 'wc/'.config('woocommerce.api_version'),
40+
'wp_api' => config('woocommerce.wp_api_integration'),
41+
'verify_ssl' => config('woocommerce.verify_ssl'),
42+
'query_string_auth' => config('woocommerce.query_string_auth'),
43+
'timeout' => config('woocommerce.timeout'),
44+
]
45+
);
4046
}
4147
}

src/config/woocommerce.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@
5454
*================================================================================.
5555
*/
5656
'timeout' => env('WOOCOMMERCE_WP_TIMEOUT', 15),
57+
58+
/**
59+
*================================================================================
60+
* Total results header
61+
*================================================================================.
62+
*/
63+
'header_total' => env('WOOCOMMERCE_WP_HEADER_TOTAL', 'X-WP-Total'),
64+
65+
/**
66+
*================================================================================
67+
* Total pages header
68+
*================================================================================.
69+
*/
70+
'header_total_pages' => env('WOOCOMMERCE_WP_HEADER_TOTAL_PAGES', 'X-WP-TotalPages'),
5771
];

0 commit comments

Comments
 (0)