Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit e9ec2af

Browse files
committed
Some clean/tidy up
1 parent 86393c8 commit e9ec2af

File tree

1 file changed

+204
-167
lines changed

1 file changed

+204
-167
lines changed

src/Mochaka/Shopify/Shopify.php

Lines changed: 204 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,191 +1,228 @@
11
<?php
22

3-
namespace Mochaka\Shopify;
3+
namespace Mochaka\Shopify;
44

5-
use Config;
6-
use Guzzle\Http\Client;
7-
use GuzzleHttp\Exception\ClientErrorResponseException;
5+
use Guzzle\Http\Client;
6+
use Guzzle\Http\Exception\ClientErrorResponseException;
7+
use Guzzle\Http\Exception\BadResponseException;
88

99

10-
class Shopify {
11-
12-
/**
13-
* Guzzle Client
14-
*
15-
* @var object
16-
*/
17-
protected $client;
18-
19-
/**
20-
* __construct to init the guzzle client
21-
* @param string $domain
22-
* @param string $key
23-
* @param string $password
24-
*/
25-
public function __construct($domain, $key, $password)
10+
class Shopify
2611
{
27-
$url = "https://".$key.":".$password."@".$domain."/admin/";
28-
$this->client = new Client(
29-
$url,
30-
['defaults' => [
31-
'headers' => ['Content-Type' => 'application/json'],
32-
]]
33-
);
34-
}
3512

36-
/**
37-
* send off the request to Shopify, encoding the data as JSON
38-
* @param string $method
39-
* @param string $page
40-
* @param array $data
41-
* @return array
42-
*/
43-
private function makeRequest($method, $page, $data = array())
44-
{
45-
46-
$r = $this->client->createRequest($method, $page, null, $data);
47-
48-
if($data && $method != 'GET') $r->setBody(json_encode($data), 'application/json');
49-
50-
if($method == 'GET' && !empty($data))
13+
/**
14+
* Guzzle Client
15+
*
16+
* @var object
17+
*/
18+
protected $client;
19+
20+
/**
21+
* __construct to init the guzzle client
22+
*
23+
* @param string $domain
24+
* @param string $key
25+
* @param string $password
26+
*/
27+
public function __construct($domain, $key, $password)
5128
{
52-
$query = $r->getQuery();
53-
foreach($data as $key=>$val) $query->set($key, $val);
29+
$url = "https://" . $key . ":" . $password . "@" . $domain . "/admin/";
30+
$this->client = new Client(
31+
$url,
32+
[
33+
'defaults' => [
34+
'headers' => ['Content-Type' => 'application/json'],
35+
]
36+
]
37+
);
5438
}
5539

56-
try {
57-
return $r->send()->json();
58-
} catch (Guzzle\Http\Exception\BadResponseException $e) {
59-
return ['error'=>$e->getMessage(), 'url'=>$e->getRequest()->getUrl(), 'request'=>$e->getRequest(), 'status'=>$e->getResponse()->getStatusCode(), 'response'=>$e->getResponse()];
60-
} catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
61-
return ['error'=>$e->getMessage(), 'url'=>$e->getRequest()->getUrl(), 'request'=>$e->getRequest(), 'status'=>$e->getResponse()->getStatusCode(), 'response'=>$e->getResponse()];
40+
/**
41+
* send off the request to Shopify, encoding the data as JSON
42+
*
43+
* @param string $method
44+
* @param string $page
45+
* @param array $data
46+
* @return array
47+
*/
48+
private function makeRequest($method, $page, $data = array())
49+
{
50+
51+
$r = $this->client->createRequest($method, $page, null, $data);
52+
53+
if ($data && $method != 'GET') {
54+
$r->setBody(json_encode($data), 'application/json');
55+
}
56+
57+
if ($method == 'GET' && !empty($data)) {
58+
$query = $r->getQuery();
59+
foreach ($data as $key => $val) {
60+
$query->set($key, $val);
61+
}
62+
}
63+
64+
try {
65+
return $r->send()
66+
->json();
67+
} catch (ClientErrorResponseException $e) {
68+
return ['error' => $e->getMessage(),
69+
'url' => $e->getRequest()
70+
->getUrl(),
71+
'request' => $e->getRequest(),
72+
'status' => $e->getResponse()
73+
->getStatusCode(),
74+
'response' => $e->getResponse()
75+
];
76+
77+
} catch (BadResponseException $e) {
78+
return [
79+
'error' => $e->getMessage(),
80+
'url' => $e->getRequest()
81+
->getUrl(),
82+
'request' => $e->getRequest(),
83+
'status' => $e->getResponse()
84+
->getStatusCode(),
85+
'response' => $e->getResponse()
86+
];
87+
}
88+
6289
}
6390

64-
}
91+
/**
92+
* returns a count of products
93+
*
94+
* @return array
95+
*/
96+
public function getProductsCount()
97+
{
98+
return $this->makeRequest('GET', 'products/count.json');
99+
}
65100

66-
/**
67-
* returns a count of products
68-
* @return array
69-
*/
70-
public function getProductsCount()
71-
{
72-
return $this->makeRequest('GET', 'products/count.json');
73-
}
74-
75-
/**
76-
* returns a list of products, depending on the input data
77-
* @param array $data
78-
* @return array
79-
*/
80-
public function getProducts($data)
81-
{
82-
return $this->makeRequest('GET', 'products.json', $data);
83-
}
101+
/**
102+
* returns a list of products, depending on the input data
103+
*
104+
* @param array $data
105+
* @return array
106+
*/
107+
public function getProducts($data)
108+
{
109+
return $this->makeRequest('GET', 'products.json', $data);
110+
}
84111

85-
/**
86-
* returns product information by id
87-
* @param int $productId
88-
* @return array
89-
*/
90-
public function getProductById($productId)
91-
{
92-
return $this->makeRequest('GET', 'products/'.$productId.'.json')['product'];
93-
}
112+
/**
113+
* returns product information by id
114+
*
115+
* @param int $productId
116+
* @return array
117+
*/
118+
public function getProductById($productId)
119+
{
120+
return $this->makeRequest('GET', 'products/' . $productId . '.json')['product'];
121+
}
94122

95-
/**
96-
* creates a product on shopify
97-
* @param array $data
98-
* @return array
99-
*/
100-
public function createProduct($data)
101-
{
102-
$d['product'] = (!isset($data['product'])) ? $data : $data['product'];
103-
return $this->makeRequest('POST', 'products.json', $d);
104-
}
123+
/**
124+
* creates a product on shopify
125+
*
126+
* @param array $data
127+
* @return array
128+
*/
129+
public function createProduct($data)
130+
{
131+
$d['product'] = (!isset($data['product'])) ? $data : $data['product'];
132+
return $this->makeRequest('POST', 'products.json', $d);
133+
}
105134

106-
/**
107-
* updates a product by id
108-
* @param int $productId
109-
* @param array $data
110-
* @return array
111-
*/
112-
public function updateProduct($productId, $data)
113-
{
114-
$d['product'] = (!isset($data['product'])) ? $data : $data['product'];
115-
return $this->makeRequest('PUT', 'products/'.$productId.'.json', $d);
116-
}
135+
/**
136+
* updates a product by id
137+
*
138+
* @param int $productId
139+
* @param array $data
140+
* @return array
141+
*/
142+
public function updateProduct($productId, $data)
143+
{
144+
$d['product'] = (!isset($data['product'])) ? $data : $data['product'];
145+
return $this->makeRequest('PUT', 'products/' . $productId . '.json', $d);
146+
}
117147

118-
/**
119-
* Delete's a product from shopify
120-
* @param int $productId
121-
* @return array
122-
*/
123-
public function deleteProduct($productId)
124-
{
125-
return $this->makeRequest('DELETE', 'products/'.$productId.'.json');
126-
}
148+
/**
149+
* Delete's a product from shopify
150+
*
151+
* @param int $productId
152+
* @return array
153+
*/
154+
public function deleteProduct($productId)
155+
{
156+
return $this->makeRequest('DELETE', 'products/' . $productId . '.json');
157+
}
127158

128-
/**
129-
* updates a specific variant by id
130-
* @param int $variantId
131-
* @param array $data
132-
* @return array
133-
*/
134-
public function updateVariant($variantId, $data)
135-
{
136-
$data['id'] = $variantId;
137-
$d['variant'] = (!isset($data['variant'])) ? $data : $data['variant'];
138-
return $this->makeRequest('PUT', 'variants/'.$variantId.'.json', $d);
139-
}
159+
/**
160+
* updates a specific variant by id
161+
*
162+
* @param int $variantId
163+
* @param array $data
164+
* @return array
165+
*/
166+
public function updateVariant($variantId, $data)
167+
{
168+
$data['id'] = $variantId;
169+
$d['variant'] = (!isset($data['variant'])) ? $data : $data['variant'];
170+
return $this->makeRequest('PUT', 'variants/' . $variantId . '.json', $d);
171+
}
140172

141-
/**
142-
* Delete's a variant from shopify
143-
* @param int $productId
144-
* @param int $variantId
145-
* @return array
146-
*/
147-
public function deleteVariant($productId, $variantId)
148-
{
149-
return $this->makeRequest('DELETE', 'products/'.$productId.'/variants/'.$variantId.'.json');
150-
}
173+
/**
174+
* Delete's a variant from shopify
175+
*
176+
* @param int $productId
177+
* @param int $variantId
178+
* @return array
179+
*/
180+
public function deleteVariant($productId, $variantId)
181+
{
182+
return $this->makeRequest('DELETE', 'products/' . $productId . '/variants/' . $variantId . '.json');
183+
}
151184

152-
/**
153-
* get a list of webhooks
154-
* @return array
155-
*/
156-
public function getWebhooks()
157-
{
158-
return $this->makeRequest('GET', 'webhooks.json');
159-
}
185+
/**
186+
* get a list of webhooks
187+
*
188+
* @return array
189+
*/
190+
public function getWebhooks()
191+
{
192+
return $this->makeRequest('GET', 'webhooks.json');
193+
}
160194

161-
/**
162-
* create a webhook
163-
* @param array $data
164-
* @return array
165-
*/
166-
public function createWebhook($data)
167-
{
168-
$d['webhook'] = (!isset($data['webhook'])) ? $data : $data['webhook'];
169-
return $this->makeRequest('POST', 'webhooks.json', $d);
170-
}
195+
/**
196+
* create a webhook
197+
*
198+
* @param array $data
199+
* @return array
200+
*/
201+
public function createWebhook($data)
202+
{
203+
$d['webhook'] = (!isset($data['webhook'])) ? $data : $data['webhook'];
204+
return $this->makeRequest('POST', 'webhooks.json', $d);
205+
}
171206

172-
/**
173-
* get a list of all customers in shopify
174-
* @return array
175-
*/
176-
public function getAllCustomers()
177-
{
178-
return $this->makeRequest('GET', 'customers.json');
179-
}
207+
/**
208+
* get a list of all customers in shopify
209+
*
210+
* @return array
211+
*/
212+
public function getAllCustomers()
213+
{
214+
return $this->makeRequest('GET', 'customers.json');
215+
}
180216

181-
/**
182-
* creates an order on shopify
183-
* @param $data
184-
* @return array
185-
*/
186-
public function createOrder($data)
187-
{
188-
$d['order'] = (!isset($data['order'])) ? $data : $data['order'];
189-
return $this->makeRequest('POST', 'orders.json', $d);
217+
/**
218+
* creates an order on shopify
219+
*
220+
* @param $data
221+
* @return array
222+
*/
223+
public function createOrder($data)
224+
{
225+
$d['order'] = (!isset($data['order'])) ? $data : $data['order'];
226+
return $this->makeRequest('POST', 'orders.json', $d);
227+
}
190228
}
191-
}

0 commit comments

Comments
 (0)