forked from krizzdev/shopware-api-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShopwareApiClient.php
196 lines (165 loc) · 6.35 KB
/
ShopwareApiClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace GeNyaa\ShopwareApiSdk;
use Carbon\Carbon;
use GeNyaa\ShopwareApiSdk\Dto\Arrayable;
use GeNyaa\ShopwareApiSdk\Endpoints\CategoryEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\CountryEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\CurrencyEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\CustomerAddressEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\CustomerEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\CustomerGroupEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\LanguageEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\ManufacturerEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\PaymentMethodEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\PropertyEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\PropertyOptionEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\SalesChannelEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\SalutationEndpoint;
use GeNyaa\ShopwareApiSdk\Endpoints\TaxEndpoint;
use GeNyaa\ShopwareApiSdk\Exceptions\ShopwareApiAuthenticationException;
use GeNyaa\ShopwareApiSdk\Exceptions\ShopwareApiException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use GeNyaa\ShopwareApiSdk\Dto\Header;
use GeNyaa\ShopwareApiSdk\Dto\Parameters;
use GeNyaa\ShopwareApiSdk\Endpoints\ProductEndpoint;
class ShopwareApiClient
{
public Http $http;
protected string $domain;
protected ?string $bearer = null;
protected string $clientId;
protected string $clientSecret;
protected ?Carbon $expiresTime = null;
public CategoryEndpoint $category;
public ProductEndpoint $product;
public TaxEndpoint $tax;
public LanguageEndpoint $language;
public CurrencyEndpoint $currency;
public PropertyEndpoint $property;
public PropertyOptionEndpoint $propertyOption;
public ManufacturerEndpoint $manufacturer;
public CustomerEndpoint $customer;
public SalutationEndpoint $salutation;
public SalesChannelEndpoint $salesChannel;
public CustomerGroupEndpoint $customerGroup;
public PaymentMethodEndpoint $paymentMethod;
public CustomerAddressEndpoint $customerAddress;
public CountryEndpoint $country;
public function __construct(Http $http)
{
$this->http = $http;
$this->setDomain(config('shopware.url'));
$this->setClientCredentials(config('shopware.client_id'), config('shopware.client_secret'));
$this->initializeEndpoints();
}
public function setDomain(string $domain): self
{
$this->domain = $domain;
return $this;
}
public function setClientCredentials(string $clientId, string $clientSecret): self
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
return $this;
}
public function initializeEndpoints(): void
{
$this->category = new CategoryEndpoint($this);
$this->product = new ProductEndpoint($this);
$this->tax = new TaxEndpoint($this);
$this->language = new LanguageEndpoint($this);
$this->currency = new CurrencyEndpoint($this);
$this->property = new PropertyEndpoint($this);
$this->propertyOption = new PropertyOptionEndpoint($this);
$this->manufacturer = new ManufacturerEndpoint($this);
$this->customer = new CustomerEndpoint($this);
$this->salutation = new SalutationEndpoint($this);
$this->salesChannel = new SalesChannelEndpoint($this);
$this->customerGroup = new CustomerGroupEndpoint($this);
$this->paymentMethod = new PaymentMethodEndpoint($this);
$this->customerAddress = new CustomerAddressEndpoint($this);
$this->country = new CountryEndpoint($this);
}
public function checkBearer(): self
{
if (is_null($this->expiresTime) || $this->expiresTime->unix() <= Carbon::now()->unix()) {
return $this->getBearerToken();
}
return $this;
}
/**
* @throws ShopwareApiAuthenticationException
*/
private function getBearerToken(): self
{
$currentTime = Carbon::now();
$response = $this->http::baseUrl($this->domain)
->post('/api/oauth/token', [
'grant_type' => 'client_credentials',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
])
->onError(function () {
throw new ShopwareApiAuthenticationException('client_id and/or client_secret are not authorized to access this domain.');
});
$data = $response->json();
$this->bearer = $data['access_token'] ?? null;
$seconds = $data['expires_in'] ?? 0;
$this->expiresTime = $currentTime->addSeconds($seconds - 60);
return $this;
}
public function performGetRequest(string $uri, Parameters $parameters = null, Header $header = null): Response
{
$this->checkBearer();
if (is_null($header)) {
$header = new Header();
}
if (is_null($parameters)) {
$parameters = new Parameters();
}
return $this->http::baseUrl($this->domain)
->withToken($this->bearer)
->withHeaders($header->toArray())
->get($uri, $parameters->toArray());
}
public function performPostRequest(string $uri, array $data, Header $header = null): Response
{
$this->checkBearer();
if (is_null($header)) {
$header = new Header();
}
return $this->http::baseUrl($this->domain)
->withToken($this->bearer)
->withHeaders($header->toArray())
->post($uri, $data);
}
public function performPatchRequest(string $uri, array $data, Header $header = null): Response
{
$this->checkBearer();
if (is_null($header)) {
$header = new Header();
}
return $this->http::baseUrl($this->domain)
->withToken($this->bearer)
->withHeaders($header->toArray())
->patch($uri, $data);
}
public function performSyncRequest(array $data, Header $header = null): Response
{
$this->checkBearer();
if (is_null($header)) {
$header = new Header();
}
return $this->http::baseUrl($this->domain)
->withToken($this->bearer)
->withHeaders($header->toArray())
->post('/api/_action/sync', $data);
}
public function copy(): self
{
return clone $this;
}
}