|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Request; |
|
6
|
|
|
|
|
7
|
|
|
use Kerox\Messenger\Helper\UtilityTrait; |
|
8
|
|
|
use Kerox\Messenger\Model\Message; |
|
9
|
|
|
use Kerox\Messenger\SendInterface; |
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
|
11
|
|
|
use function GuzzleHttp\Psr7\stream_for; |
|
12
|
|
|
|
|
13
|
|
|
class SendRequest extends AbstractRequest implements BodyRequestInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use UtilityTrait; |
|
16
|
|
|
|
|
17
|
|
|
public const REQUEST_TYPE_MESSAGE = 'message'; |
|
18
|
|
|
public const REQUEST_TYPE_ACTION = 'action'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var null|array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $recipient; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var null|string|\Kerox\Messenger\Model\Message |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $message; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var null|string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $senderAction; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var null|string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $notificationType; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var null|string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $tag; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var null|string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $personaId; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $messagingType; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Request constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $path |
|
59
|
|
|
* @param string|\Kerox\Messenger\Model\Message $content |
|
60
|
|
|
* @param string|null $recipient |
|
61
|
|
|
* @param array $options |
|
62
|
|
|
* @param string $requestType |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function __construct( |
|
65
|
|
|
string $path, |
|
66
|
|
|
$content, |
|
67
|
|
|
?string $recipient = null, |
|
68
|
|
|
array $options = [], |
|
69
|
|
|
string $requestType = self::REQUEST_TYPE_MESSAGE |
|
70
|
|
|
) { |
|
71
|
9 |
|
parent::__construct($path); |
|
72
|
|
|
|
|
73
|
9 |
|
if ($content instanceof Message || $requestType === self::REQUEST_TYPE_MESSAGE) { |
|
74
|
7 |
|
$this->message = $content; |
|
75
|
|
|
} else { |
|
76
|
2 |
|
$this->senderAction = $content; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
9 |
|
$this->recipient = \is_string($recipient) ? ['id' => $recipient] : $recipient; |
|
80
|
9 |
|
$this->messagingType = $options[SendInterface::OPTION_MESSAGING_TYPE] ?? null; |
|
81
|
9 |
|
$this->notificationType = $options[SendInterface::OPTION_NOTIFICATION_TYPE] ?? null; |
|
82
|
9 |
|
$this->tag = $options[SendInterface::OPTION_TAG] ?? null; |
|
83
|
9 |
|
$this->personaId = $options[SendInterface::OPTION_PERSONA_ID] ?? null; |
|
84
|
9 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $method |
|
88
|
|
|
* |
|
89
|
|
|
* @return RequestInterface |
|
90
|
|
|
*/ |
|
91
|
9 |
|
public function build(string $method = 'post'): RequestInterface |
|
92
|
|
|
{ |
|
93
|
9 |
|
return $this->origin |
|
94
|
9 |
|
->withMethod($method) |
|
95
|
9 |
|
->withBody(stream_for($this->buildBody())); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
9 |
|
public function buildBody(): string |
|
102
|
|
|
{ |
|
103
|
|
|
$body = [ |
|
104
|
9 |
|
'messaging_type' => $this->messagingType, |
|
105
|
9 |
|
'recipient' => $this->recipient, |
|
106
|
9 |
|
'message' => $this->message, |
|
107
|
9 |
|
'sender_action' => $this->senderAction, |
|
108
|
9 |
|
'notification_type' => $this->notificationType, |
|
109
|
9 |
|
'tag' => $this->tag, |
|
110
|
9 |
|
'persona_id' => $this->personaId, |
|
111
|
|
|
]; |
|
112
|
|
|
|
|
113
|
9 |
|
return json_encode($this->arrayFilter($body)); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|