-
Notifications
You must be signed in to change notification settings - Fork 301
/
MessageSentReport.php
103 lines (86 loc) · 2.34 KB
/
MessageSentReport.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
<?php declare(strict_types=1);
/**
* @author Igor Timoshenkov [[email protected]]
* @started: 03.09.2018 9:21
*/
namespace Minishlink\WebPush;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Standardized response from sending a message
*/
class MessageSentReport implements \JsonSerializable
{
public function __construct(
protected RequestInterface $request,
protected ?ResponseInterface $response = null,
protected bool $success = true,
protected string $reason = 'OK'
) {
}
public function isSuccess(): bool
{
return $this->success;
}
public function setSuccess(bool $success): MessageSentReport
{
$this->success = $success;
return $this;
}
public function getRequest(): RequestInterface
{
return $this->request;
}
public function setRequest(RequestInterface $request): MessageSentReport
{
$this->request = $request;
return $this;
}
public function getResponse(): ?ResponseInterface
{
return $this->response;
}
public function setResponse(ResponseInterface $response): MessageSentReport
{
$this->response = $response;
return $this;
}
public function getEndpoint(): string
{
return $this->request->getUri()->__toString();
}
public function isSubscriptionExpired(): bool
{
if (!$this->response) {
return false;
}
return \in_array($this->response->getStatusCode(), [404, 410], true);
}
public function getReason(): string
{
return $this->reason;
}
public function setReason(string $reason): MessageSentReport
{
$this->reason = $reason;
return $this;
}
public function getRequestPayload(): string
{
return $this->request->getBody()->getContents();
}
public function getResponseContent(): ?string
{
return $this->response?->getBody()->getContents();
}
public function jsonSerialize(): array
{
return [
'success' => $this->isSuccess(),
'expired' => $this->isSubscriptionExpired(),
'reason' => $this->reason,
'endpoint' => $this->getEndpoint(),
'payload' => $this->request->getBody()->getContents(),
];
}
}