forked from vienthuong/shopware-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserConfigService.php
65 lines (52 loc) · 2.43 KB
/
UserConfigService.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
<?php declare(strict_types=1);
namespace Vin\ShopwareSdk\Service;
use GuzzleHttp\Exception\BadResponseException;
use Vin\ShopwareSdk\Exception\ShopwareResponseException;
use Vin\ShopwareSdk\Service\Struct\KeyValuePair;
use Vin\ShopwareSdk\Service\Struct\KeyValuePairs;
class UserConfigService extends ApiService
{
private const USER_CONFIG_ENDPOINT = '/api/_info/config-me';
public function getConfigMe(array $keys, array $additionalHeaders = []): KeyValuePairs
{
try {
$response = $this->httpClient->get($this->getFullUrl(self::USER_CONFIG_ENDPOINT), [
'headers' => $this->getBasicHeaders($additionalHeaders),
'query' => [
'keys' => $keys,
]
]);
$contents = self::handleResponse($response->getBody()->getContents(), $response->getHeaders());
$data = new KeyValuePairs();
if (empty($contents['data'])) {
return $data;
}
foreach ($contents['data'] as $key => $value) {
$data->add(KeyValuePair::create($key, $value));
}
return $data;
} catch (BadResponseException $exception) {
$message = $exception->getResponse()->getBody()->getContents();
throw new ShopwareResponseException($message, $exception->getResponse()->getStatusCode(), $exception);
}
}
public function saveConfigMe(KeyValuePairs $configs, array $additionalParams = [], array $additionalHeaders = []): ApiResponse
{
$parsed = [];
/** @var KeyValuePair $item */
foreach ($configs as $item) {
$parsed[$item->getKey()] = $item->getValue();
}
try {
$response = $this->httpClient->post($this->getFullUrl(self::USER_CONFIG_ENDPOINT), [
'headers' => $this->getBasicHeaders($additionalHeaders),
'body' => json_encode(array_merge($parsed, $additionalParams))
]);
$contents = self::handleResponse($response->getBody()->getContents(), $response->getHeaders());
return new ApiResponse($contents, $response->getHeaders(), $response->getStatusCode());
} catch (BadResponseException $exception) {
$message = $exception->getResponse()->getBody()->getContents();
throw new ShopwareResponseException($message, $exception->getResponse()->getStatusCode(), $exception);
}
}
}