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