File tree

5 files changed

+209
-6
lines changed

5 files changed

+209
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `MessageMediaOptions` class
8+
49
6.2
510
---
611

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\MessageMedia;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://.com/gnito-org>
18+
*/
19+
final class MessageMediaOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getCallbackUrl(): ?string
29+
{
30+
return $this->options['callback_url'] ?? null;
31+
}
32+
33+
public function getDeliveryReport(): ?bool
34+
{
35+
return $this->options['delivery_report'] ?? null;
36+
}
37+
38+
public function getFormat(): ?string
39+
{
40+
return $this->options['format'] ?? null;
41+
}
42+
43+
public function getFrom(): ?string
44+
{
45+
return $this->options['from'] ?? null;
46+
}
47+
48+
public function getMedia(): ?array
49+
{
50+
return $this->options['media'] ?? null;
51+
}
52+
53+
public function getMessageExpiryTimestamp(): ?int
54+
{
55+
return $this->options['message_expiry_timestamp'] ?? null;
56+
}
57+
58+
public function getMetadata(): ?array
59+
{
60+
return $this->options['metadata'] ?? null;
61+
}
62+
63+
public function getRecipientId(): ?string
64+
{
65+
return $this->options['recipient_id'] ?? null;
66+
}
67+
68+
public function getScheduled(): ?string
69+
{
70+
return $this->options['scheduled'] ?? null;
71+
}
72+
73+
public function getSubject(): ?string
74+
{
75+
return $this->options['subject'] ?? null;
76+
}
77+
78+
public function setCallbackUrl(string $callbackUrl): self
79+
{
80+
$this->options['callback_url'] = $callbackUrl;
81+
82+
return $this;
83+
}
84+
85+
public function setDeliveryReport(bool $deliveryReport): self
86+
{
87+
$this->options['delivery_report'] = $deliveryReport;
88+
89+
return $this;
90+
}
91+
92+
public function setFormat(string $format): self
93+
{
94+
$this->options['format'] = $format;
95+
96+
return $this;
97+
}
98+
99+
public function setFrom(string $from): self
100+
{
101+
$this->options['from'] = $from;
102+
103+
return $this;
104+
}
105+
106+
public function setMedia(array $media): self
107+
{
108+
$this->options['media'] = $media;
109+
110+
return $this;
111+
}
112+
113+
public function setMessageExpiryTimestamp(int $messageExpiryTimestamp): self
114+
{
115+
$this->options['message_expiry_timestamp'] = $messageExpiryTimestamp;
116+
117+
return $this;
118+
}
119+
120+
public function setMetadata(array $metadata): self
121+
{
122+
$this->options['metadata'] = $metadata;
123+
124+
return $this;
125+
}
126+
127+
public function setRecipientId(string $id): self
128+
{
129+
$this->options['recipient_id'] = $id;
130+
131+
return $this;
132+
}
133+
134+
public function setScheduled(string $scheduled): self
135+
{
136+
$this->options['scheduled'] = $scheduled;
137+
138+
return $this;
139+
}
140+
141+
public function setSubject(string $subject): self
142+
{
143+
$this->options['subject'] = $subject;
144+
145+
return $this;
146+
}
147+
148+
public function toArray(): array
149+
{
150+
$options = $this->options;
151+
if (isset($options['recipient_id'])) {
152+
unset($options['recipient_id']);
153+
}
154+
155+
return $options;
156+
}
157+
}
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __toString(): string
5353

5454
public function supports(MessageInterface $message): bool
5555
{
56-
return $message instanceof SmsMessage;
56+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof MessageMediaOptions);
5757
}
5858

5959
protected function doSend(MessageInterface $message): SentMessage
@@ -64,6 +64,14 @@ protected function doSend(MessageInterface $message): SentMessage
6464

6565
$from = $message->getFrom() ?: $this->from;
6666

67+
$opts = $message->getOptions();
68+
$options = $opts ? $opts->toArray() : [];
69+
$options['source_number'] = $options['from'] ?? $from;
70+
$options['destination_number'] = $message->getPhone();
71+
$options['content'] = $message->getSubject();
72+
73+
unset($options['from']);
74+
6775
$endpoint = sprintf('https://%s/v1/messages', $this->getEndpoint());
6876
$response = $this->client->request(
6977
'POST',
@@ -72,11 +80,7 @@ protected function doSend(MessageInterface $message): SentMessage
7280
'auth_basic' => $this->apiKey.':'.$this->apiSecret,
7381
'json' => [
7482
'messages' => [
75-
[
76-
'destination_number' => $message->getPhone(),
77-
'source_number' => $from,
78-
'content' => $message->getSubject(),
79-
],
83+
array_filter($options),
8084
],
8185
],
8286
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\MessageMedia\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaOptions;
16+
17+
class MessageMediaOptionsTest extends TestCase
18+
{
19+
public function testMessageMediaOptions()
20+
{
21+
$messageMediaOptions = (new MessageMediaOptions())->setFrom('test_from')->setMedia(['test_media'])->setCallbackUrl('test_callback_url')->setFormat('test_format')->setRecipientId('test_recipient')->setDeliveryReport(true)->setMessageExpiryTimestamp(999)->setMetadata(['test_metadata'])->setScheduled('test_scheduled')->setSubject('test_subject');
22+
23+
self::assertSame([
24+
'from' => 'test_from',
25+
'media' => ['test_media'],
26+
'callback_url' => 'test_callback_url',
27+
'format' => 'test_format',
28+
'delivery_report' => true,
29+
'message_expiry_timestamp' => 999,
30+
'metadata' => ['test_metadata'],
31+
'scheduled' => 'test_scheduled',
32+
'subject' => 'test_subject',
33+
], $messageMediaOptions->toArray());
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Bridge\MessageMedia\Tests;
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaOptions;
1516
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransport;
1617
use Symfony\Component\Notifier\Exception\TransportException;
1718
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
@@ -38,6 +39,7 @@ public static function toStringProvider(): iterable
3839
public static function supportedMessagesProvider(): iterable
3940
{
4041
yield [new SmsMessage('0491570156', 'Hello!')];
42+
yield [new SmsMessage('0491570156', 'Hello!', 'from', new MessageMediaOptions(['from' => 'foo']))];
4143
}
4244

4345
public static function unsupportedMessagesProvider(): iterable

0 commit comments

Comments
 (0)