-
Notifications
You must be signed in to change notification settings - Fork 44
/
NotificationService.php
62 lines (49 loc) · 2.74 KB
/
NotificationService.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
<?php declare(strict_types=1);
namespace Vin\ShopwareSdk\Service;
use GuzzleHttp\Exception\BadResponseException;
use Vin\ShopwareSdk\Exception\ShopwareResponseException;
use Vin\ShopwareSdk\Service\Struct\Notification;
use Vin\ShopwareSdk\Service\Struct\NotificationCollection;
class NotificationService extends ApiService
{
private const NOTIFICATION_ENDPOINT = '/api/notification';
private const NOTIFICATION_MESSAGE_ENDPOINT = '/api/notification/message';
public function sendNotification(Notification $notification, array $additionalParams = [], array $additionalHeaders = []): ApiResponse
{
try {
$response = $this->httpClient->post($this->getFullUrl(self::NOTIFICATION_ENDPOINT), [
'headers' => $this->getBasicHeaders($additionalHeaders),
'body' => json_encode(array_merge($notification->parse(), $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);
}
}
public function fetchNotification(?string $latestTimestamp = null, ?int $limit = 5, array $additionalParams = [], array $additionalHeaders = []): NotificationCollection
{
try {
$response = $this->httpClient->get($this->getFullUrl(self::NOTIFICATION_MESSAGE_ENDPOINT), [
'headers' => $this->getBasicHeaders($additionalHeaders),
'body' => json_encode(array_merge([
'latestTimestamp' => $latestTimestamp,
'limit' => $limit,
], $additionalParams))
]);
$contents = self::handleResponse($response->getBody()->getContents(), $response->getHeaders());
$collection = new NotificationCollection([], $contents['timestamp']);
if (empty($contents['notifications'])) {
return $collection;
}
foreach ($contents['notifications'] as $notification) {
$collection->add(Notification::create($notification['status'], $notification['message'], $notification['adminOnly'], $notification['requiredPrivileges']));
}
return $collection;
} catch (BadResponseException $exception) {
$message = $exception->getResponse()->getBody()->getContents();
throw new ShopwareResponseException($message, $exception->getResponse()->getStatusCode(), $exception);
}
}
}