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