1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ShopifyClient; |
4
|
|
|
|
5
|
|
|
use ShopifyClient\Resource\AbandonedCheckouts; |
6
|
|
|
use ShopifyClient\Resource\Blog; |
7
|
|
|
use ShopifyClient\Resource\Country; |
8
|
|
|
use ShopifyClient\Resource\Customer; |
9
|
|
|
use ShopifyClient\Resource\FulfillmentService; |
10
|
|
|
use ShopifyClient\Resource\Order; |
11
|
|
|
use ShopifyClient\Resource\PriceRule; |
12
|
|
|
use ShopifyClient\Resource\Product; |
13
|
|
|
use ShopifyClient\Resource\Resource; |
14
|
|
|
use ShopifyClient\Resource\ResourceCollection; |
15
|
|
|
use ShopifyClient\Resource\Shop; |
16
|
|
|
use ShopifyClient\Resource\Webhook; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @property AbandonedCheckouts $abandonedCheckouts |
20
|
|
|
* @property Blog $blogs |
21
|
|
|
* @property Country $countries |
22
|
|
|
* @property Customer $customers |
23
|
|
|
* @property FulfillmentService $fulfillmentServices |
24
|
|
|
* @property PriceRule $priceRules |
25
|
|
|
* @property Product $products |
26
|
|
|
* @property Order $orders |
27
|
|
|
* @property Shop $shop |
28
|
|
|
* @property Webhook $webhooks |
29
|
|
|
*/ |
30
|
|
|
class Client |
31
|
|
|
{ |
32
|
|
|
const API_URL = 'https://%s/admin/'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Request |
36
|
|
|
*/ |
37
|
|
|
protected $request; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Config |
41
|
|
|
*/ |
42
|
|
|
private $config; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ResourceCollection |
46
|
|
|
*/ |
47
|
|
|
private $resources; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Client constructor. |
51
|
|
|
* @param Config $config |
52
|
|
|
*/ |
53
|
5 |
|
public function __construct(Config $config) |
54
|
|
|
{ |
55
|
5 |
|
$this->config = $config; |
56
|
|
|
|
57
|
5 |
|
$this->initializeHttpClient(); |
58
|
4 |
|
} |
59
|
|
|
|
60
|
5 |
|
private function initializeHttpClient() |
61
|
|
|
{ |
62
|
|
|
$config = [ |
63
|
5 |
|
'base_uri' => $this->getBaseUrl(), |
64
|
|
|
'headers' => [ |
65
|
5 |
|
'Content-Type' => 'application/json', |
66
|
5 |
|
'Accept-Encoding' => 'application/json', |
67
|
5 |
|
'User-Agent' => $this->getBaseUrl(), |
68
|
5 |
|
'Authorization' => 'Basic ' . $this->getCredentials(), |
69
|
|
|
] |
70
|
|
|
]; |
71
|
|
|
|
72
|
5 |
|
if (!empty($this->config->getAccessToken())) { |
73
|
1 |
|
$config['headers'] = array_merge( |
74
|
1 |
|
$config['headers'], |
75
|
1 |
|
['X-Shopify-Access-Token' => $this->config->getAccessToken()] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
$this->resources = new ResourceCollection( |
80
|
5 |
|
new Request(new \GuzzleHttp\Client($config)), |
81
|
5 |
|
$this->config->getResources() |
82
|
|
|
); |
83
|
4 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
5 |
|
private function getBaseUrl(): string |
89
|
|
|
{ |
90
|
5 |
|
if (!empty($this->config->getAccessToken())) { |
91
|
1 |
|
return $this->config->getDomain(); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
return sprintf(self::API_URL, $this->config->getDomain()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
5 |
|
private function getCredentials(): string |
101
|
|
|
{ |
102
|
5 |
|
return base64_encode(sprintf( |
103
|
5 |
|
'%s:%s', |
104
|
5 |
|
$this->config->getKey(), |
105
|
5 |
|
$this->config->getSecret() |
106
|
|
|
)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $name |
111
|
|
|
* @return Resource |
112
|
|
|
*/ |
113
|
2 |
|
public function getResource(string $name): Resource |
114
|
|
|
{ |
115
|
2 |
|
return $this->resources->getResource($name); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $name |
120
|
|
|
* @return Resource |
121
|
|
|
*/ |
122
|
133 |
|
public function __get(string $name): Resource |
123
|
|
|
{ |
124
|
133 |
|
return $this->resources->getResource($name); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|