1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeadCommerce\Shopware\SDK; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use LeadCommerce\Shopware\SDK\Query\AddressQuery; |
7
|
|
|
use LeadCommerce\Shopware\SDK\Query\ArticleQuery; |
8
|
|
|
use LeadCommerce\Shopware\SDK\Query\CacheQuery; |
9
|
|
|
use LeadCommerce\Shopware\SDK\Query\CategoriesQuery; |
10
|
|
|
use LeadCommerce\Shopware\SDK\Query\CountriesQuery; |
11
|
|
|
use LeadCommerce\Shopware\SDK\Query\CustomerGroupsQuery; |
12
|
|
|
use LeadCommerce\Shopware\SDK\Query\CustomerQuery; |
13
|
|
|
use LeadCommerce\Shopware\SDK\Query\GenerateArticleImagesQuery; |
14
|
|
|
use LeadCommerce\Shopware\SDK\Query\ManufacturersQuery; |
15
|
|
|
use LeadCommerce\Shopware\SDK\Query\MediaQuery; |
16
|
|
|
use LeadCommerce\Shopware\SDK\Query\OrdersQuery; |
17
|
|
|
use LeadCommerce\Shopware\SDK\Query\PropertyGroupsQuery; |
18
|
|
|
use LeadCommerce\Shopware\SDK\Query\ShopsQuery; |
19
|
|
|
use LeadCommerce\Shopware\SDK\Query\TranslationsQuery; |
20
|
|
|
use LeadCommerce\Shopware\SDK\Query\VariantsQuery; |
21
|
|
|
use LeadCommerce\Shopware\SDK\Query\VersionQuery; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class ShopwareClient |
25
|
|
|
* |
26
|
|
|
* @author Alexander Mahrt <[email protected]> |
27
|
|
|
* @copyright 2016 LeadCommerce <[email protected]> |
28
|
|
|
* |
29
|
|
|
* @method AddressQuery getAddressQuery() |
30
|
|
|
* @method ArticleQuery getArticleQuery() |
31
|
|
|
* @method CacheQuery getCacheQuery() |
32
|
|
|
* @method CategoriesQuery getCategoriesQuery() |
33
|
|
|
* @method CountriesQuery getCountriesQuery() |
34
|
|
|
* @method CustomerGroupsQuery getCustomerGroupsQuery() |
35
|
|
|
* @method CustomerQuery getCustomerQuery() |
36
|
|
|
* @method GenerateArticleImagesQuery getGenerateArticleImageQuery() |
37
|
|
|
* @method MediaQuery getMediaQuery() |
38
|
|
|
* @method ManufacturersQuery getManufacturersQuery() |
39
|
|
|
* @method OrdersQuery getOrdersQuery() |
40
|
|
|
* @method PropertyGroupsQuery getPropertyGroupsQuery() |
41
|
|
|
* @method ShopsQuery getShopsQuery() |
42
|
|
|
* @method TranslationsQuery getTranslationsQuery() |
43
|
|
|
* @method VariantsQuery getVariantsQuery() |
44
|
|
|
* @method VersionQuery getVersionQuery() |
45
|
|
|
*/ |
46
|
|
|
class ShopwareClient |
47
|
|
|
{ |
48
|
|
|
const VERSION = '0.0.1'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string|null |
52
|
|
|
*/ |
53
|
|
|
protected $baseUrl; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string|null |
57
|
|
|
*/ |
58
|
|
|
protected $username; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string|null |
62
|
|
|
*/ |
63
|
|
|
protected $apiKey; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var Client |
67
|
|
|
*/ |
68
|
|
|
protected $client; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* ShopwareClient constructor. |
72
|
|
|
* |
73
|
|
|
* @param $baseUrl |
74
|
|
|
* @param null $username |
75
|
|
|
* @param null $apiKey |
76
|
|
|
*/ |
77
|
10 |
|
public function __construct($baseUrl, $username = null, $apiKey = null) |
78
|
|
|
{ |
79
|
10 |
|
$this->baseUrl = $baseUrl; |
80
|
10 |
|
$this->username = $username; |
81
|
10 |
|
$this->apiKey = $apiKey; |
82
|
10 |
|
$this->client = new Client([ |
83
|
10 |
|
'base_uri' => $this->baseUrl, |
84
|
10 |
|
]); |
85
|
10 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Does a request. |
89
|
|
|
* |
90
|
|
|
* @param $uri |
91
|
|
|
* @param string $method |
92
|
|
|
* @param null $body |
93
|
|
|
* @param array $headers |
94
|
|
|
* |
95
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
96
|
|
|
*/ |
97
|
8 |
|
public function request($uri, $method = 'GET', $body = null, $headers = []) |
98
|
|
|
{ |
99
|
8 |
|
if (empty($headers['Accept'])) { |
100
|
8 |
|
$headers['Accept'] = 'application/json'; |
101
|
8 |
|
} |
102
|
|
|
|
103
|
8 |
|
if (empty($headers['Content-Type'])) { |
104
|
8 |
|
$headers['Content-Type'] = 'application/json'; |
105
|
8 |
|
} |
106
|
|
|
|
107
|
8 |
|
return $this->client->request($method, $uri, [ |
108
|
8 |
|
'form_params' => $body, |
109
|
8 |
|
'headers' => $headers, |
110
|
|
|
'auth' => [ |
111
|
8 |
|
$this->username, |
112
|
8 |
|
$this->apiKey, |
113
|
8 |
|
'digest', |
114
|
8 |
|
], |
115
|
8 |
|
]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Magically get the query classes. |
120
|
|
|
* |
121
|
|
|
* @param $name |
122
|
|
|
* @param array $arguments |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
2 |
|
public function __call($name, $arguments = []) |
127
|
|
|
{ |
128
|
2 |
|
if (!preg_match('/^get([a-z]+Query)$/i', $name, $matches)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
$className = __NAMESPACE__ . '\\Query\\' . $matches[1]; |
133
|
|
|
|
134
|
1 |
|
if (!class_exists($className)) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return new $className($this); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return Client |
143
|
|
|
*/ |
144
|
1 |
|
public function getClient() |
145
|
|
|
{ |
146
|
1 |
|
return $this->client; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param Client $client |
151
|
|
|
* |
152
|
|
|
* @return ShopwareClient |
153
|
|
|
*/ |
154
|
10 |
|
public function setClient($client) |
155
|
|
|
{ |
156
|
10 |
|
$this->client = $client; |
157
|
|
|
|
158
|
10 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|