Skip to content

Commit 9bf9d09

Browse files
committed
CodemashPushNotification.
1 parent e917ad3 commit 9bf9d09

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

src/CodemashPushNotification.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace Codemash;
4+
5+
use Codemash\Exceptions\RequestValidationException;
6+
use Codemash\Params\CodemashPushNotificationParams;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
9+
class CodemashPushNotification
10+
{
11+
private CodemashClient $client;
12+
private string $uriPrefix = 'v2/notifications/push/';
13+
14+
public function __construct(CodemashClient $client)
15+
{
16+
$this->client = $client;
17+
}
18+
19+
/**
20+
* @throws GuzzleException
21+
* @throws RequestValidationException
22+
*/
23+
public function getTemplate(array $params): array
24+
{
25+
$params = CodemashPushNotificationParams::prepGetByIdParams($params);
26+
27+
$response = $this->client->request('GET', $this->uriPrefix . 'templates/' . $params['id'], [
28+
'headers' => [
29+
'Accept' => 'application/json',
30+
'Content-Type' => 'application/json',
31+
],
32+
]);
33+
34+
return $response['result'];
35+
}
36+
37+
/**
38+
* @throws GuzzleException
39+
*/
40+
public function getTemplates(): array
41+
{
42+
$response = $this->client->request('GET', $this->uriPrefix . 'templates', [
43+
'headers' => [
44+
'Accept' => 'application/json',
45+
'Content-Type' => 'application/json',
46+
],
47+
]);
48+
49+
return $response['result'];
50+
}
51+
52+
/**
53+
* @throws GuzzleException
54+
* @throws RequestValidationException
55+
*/
56+
public function sendNotification(array $params): string
57+
{
58+
$params = CodemashPushNotificationParams::prepSendNotificationParams($params);
59+
60+
$response = $this->client->request('POST', $this->uriPrefix, [
61+
'headers' => [
62+
'Accept' => 'application/json',
63+
'Content-Type' => 'application/json',
64+
],
65+
'body' => toJson($params),
66+
]);
67+
68+
return $response['result'];
69+
}
70+
71+
/**
72+
* @throws GuzzleException
73+
* @throws RequestValidationException
74+
*/
75+
public function getNotification(array $params): array
76+
{
77+
$params = CodemashPushNotificationParams::prepGetByIdParams($params);
78+
79+
$response = $this->client->request('GET', $this->uriPrefix . $params['id'], [
80+
'headers' => [
81+
'Accept' => 'application/json',
82+
'Content-Type' => 'application/json',
83+
],
84+
]);
85+
86+
return $response['result'];
87+
}
88+
89+
/**
90+
* @throws GuzzleException
91+
*/
92+
public function getNotifications(array $params = []): array
93+
{
94+
$params = CodemashPushNotificationParams::prepGetNotificationsParams($params);
95+
96+
$response = $this->client->request('GET', $this->uriPrefix, [
97+
'headers' => [
98+
'Accept' => 'application/json',
99+
'Content-Type' => 'application/json',
100+
],
101+
'body' => toJson($params),
102+
]);
103+
104+
return $response['result'];
105+
}
106+
107+
/**
108+
* @throws GuzzleException
109+
*/
110+
public function getNotificationCount(array $params): array
111+
{
112+
$params = CodemashPushNotificationParams::prepGetNotificationsParams($params);
113+
114+
$response = $this->client->request('GET', $this->uriPrefix . 'count', [
115+
'headers' => [
116+
'Accept' => 'application/json',
117+
'Content-Type' => 'application/json',
118+
],
119+
'body' => toJson($params),
120+
]);
121+
122+
return $response['result'];
123+
}
124+
125+
/**
126+
* @throws GuzzleException
127+
* @throws RequestValidationException
128+
*/
129+
public function readNotification(array $params): int
130+
{
131+
$params = CodemashPushNotificationParams::prepGetByIdParams($params);
132+
133+
return $this->client->request('PATCH', $this->uriPrefix . $params['id'] . '/read', [
134+
'headers' => [
135+
'Accept' => 'application/json',
136+
'Content-Type' => 'application/json',
137+
],
138+
]);
139+
}
140+
141+
/**
142+
* @throws GuzzleException
143+
* @throws RequestValidationException
144+
*/
145+
public function deleteNotification(array $params)
146+
{
147+
$params = CodemashPushNotificationParams::prepGetByIdParams($params);
148+
149+
return $this->client->request('DELETE', $this->uriPrefix . $params['id'], [
150+
'headers' => [
151+
'Accept' => 'application/json',
152+
'Content-Type' => 'application/json',
153+
],
154+
]);
155+
}
156+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Codemash\Params;
4+
5+
use Codemash\Exceptions\RequestValidationException;
6+
7+
class CodemashPushNotificationParams
8+
{
9+
/**
10+
* @throws RequestValidationException
11+
*/
12+
public static function prepGetByIdParams(array $params): array
13+
{
14+
$required = ['id'];
15+
16+
validateRequiredRequestParams($required, $params);
17+
18+
return [
19+
'id' => $params['id'],
20+
];
21+
}
22+
23+
/**
24+
* @throws RequestValidationException
25+
*/
26+
public static function prepSendNotificationParams(array $params): array
27+
{
28+
$required = ['templateId'];
29+
30+
validateRequiredRequestParams($required, $params);
31+
32+
return [
33+
'templateId' => $params['templateId'],
34+
'roles' => $params['roles'] ?? null,
35+
'users' => $params['users'] ?? null,
36+
'devices' => $params['devices'] ?? null,
37+
'isNonPushable' => $params['isNonPushable'] ?? false,
38+
'tokens' => $params['tokens'] ?? null,
39+
'postpone' => $params['postpone'] ?? null,
40+
'respectTimeZone' => $params['respectTimeZone'] ?? false,
41+
];
42+
}
43+
44+
public static function prepGetNotificationsParams(array $params): array
45+
{
46+
return [
47+
'userId' => $params['userId'] ?? null,
48+
'deviceId' => $params['deviceId'] ?? null,
49+
];
50+
}
51+
}

0 commit comments

Comments
 (0)