Skip to content

Commit cc33680

Browse files
committed
[Notifier] [Mobyt] [BC BREAK] Change ctor signature and validate message types
1 parent 72a82c3 commit cc33680

File tree

5 files changed

+68
-8
lines changed

5 files changed

+68
-8
lines changed

src/Symfony/Component/Notifier/Bridge/Mobyt/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ CHANGELOG
55
---
66

77
* The bridge is not marked as `@experimental` anymore
8+
* [BC BREAK] Change signature of `MobytTransport::__construct()` method from:
9+
`public function __construct(string $accountSid, string $authToken, string $from, string $typeQuality, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)`
10+
to:
11+
`public function __construct(string $accountSid, string $authToken, string $from, string $typeQuality = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)`
12+
* Validate message types
813

914
5.2.0
1015
-----

src/Symfony/Component/Notifier/Bridge/Mobyt/MobytOptions.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Mobyt;
1313

14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1415
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
1516
use Symfony\Component\Notifier\Notification\Notification;
1617

@@ -27,6 +28,10 @@ final class MobytOptions implements MessageOptionsInterface
2728

2829
public function __construct(array $options = [])
2930
{
31+
if (isset($options['message_type'])) {
32+
self::validateMessageType($options['message_type']);
33+
}
34+
3035
$this->options = $options;
3136
}
3237

@@ -66,6 +71,17 @@ public function getRecipientId(): ?string
6671

6772
public function messageType(string $type)
6873
{
74+
self::validateMessageType($type);
75+
6976
$this->options['message_type'] = $type;
7077
}
78+
79+
public static function validateMessageType($type): string
80+
{
81+
if (!\in_array($type, $supported = [self::MESSAGE_TYPE_QUALITY_HIGH, self::MESSAGE_TYPE_QUALITY_MEDIUM, self::MESSAGE_TYPE_QUALITY_LOW], true)) {
82+
throw new InvalidArgumentException(sprintf('The message type "%s" is not supported; supported message types are: "%s"', $type, implode('", "', $supported)));
83+
}
84+
85+
return $type;
86+
}
7187
}

src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransport.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ final class MobytTransport extends AbstractTransport
3333
private $from;
3434
private $typeQuality;
3535

36-
public function __construct(string $accountSid, string $authToken, string $from, string $typeQuality, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
public function __construct(string $accountSid, string $authToken, string $from, string $typeQuality = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
3737
{
3838
$this->accountSid = $accountSid;
3939
$this->authToken = $authToken;
4040
$this->from = $from;
41+
42+
$messageType = $typeQuality ?? MobytOptions::MESSAGE_TYPE_QUALITY_LOW;
43+
MobytOptions::validateMessageType($messageType);
44+
4145
$this->typeQuality = $typeQuality;
4246

4347
parent::__construct($client, $dispatcher);
@@ -77,14 +81,16 @@ protected function doSend(MessageInterface $message): SentMessage
7781
'user_key: '.$this->accountSid,
7882
'Access_token: '.$this->authToken,
7983
],
80-
'body' => json_encode(array_filter($options)),
84+
'json' => array_filter($options),
8185
]);
8286

83-
if (401 === $response->getStatusCode() || 404 === $response->getStatusCode()) {
87+
$statusCode = $response->getStatusCode();
88+
89+
if (401 === $statusCode || 404 === $statusCode) {
8490
throw new TransportException(sprintf('Unable to send the SMS: "%s". Check your credentials.', $message->getSubject()), $response);
8591
}
8692

87-
if (201 !== $response->getStatusCode()) {
93+
if (201 !== $statusCode) {
8894
$error = $response->toArray(false);
8995

9096
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['result']), $response);

src/Symfony/Component/Notifier/Bridge/Mobyt/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ where:
1414
- `USER_KEY` is your Mobyt user key
1515
- `ACCESS_TOKEN` is your Mobyt access token
1616
- `FROM` is the sender
17-
- `TYPE_QUALITY` is the quality : `N` for high, `L` for medium, `LL` for low (default: `L`)
17+
- `TYPE_QUALITY` is the quality of your message: `N` for high, `L` for medium, `LL` for low (default: `L`)
1818

1919
Resources
2020
---------

src/Symfony/Component/Notifier/Bridge/Mobyt/Tests/MobytOptionsTest.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Notifier\Bridge\Mobyt\MobytOptions;
7+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
78
use Symfony\Component\Notifier\Notification\Notification;
89

910
final class MobytOptionsTest extends TestCase
@@ -64,11 +65,43 @@ public function testToArray()
6465
$this->assertEmpty($mobytOptions->toArray());
6566
}
6667

67-
public function testMessageType()
68+
/**
69+
* @dataProvider validMessageTypes
70+
*/
71+
public function testMessageType(string $type)
72+
{
73+
$mobytOptions = new MobytOptions();
74+
$mobytOptions->messageType($type);
75+
76+
$this->assertSame(['message_type' => $type], $mobytOptions->toArray());
77+
}
78+
79+
public function validMessageTypes(): iterable
80+
{
81+
yield [MobytOptions::MESSAGE_TYPE_QUALITY_HIGH];
82+
yield [MobytOptions::MESSAGE_TYPE_QUALITY_MEDIUM];
83+
yield [MobytOptions::MESSAGE_TYPE_QUALITY_LOW];
84+
}
85+
86+
public function testCallingMessageTypeMethodWithUnknownTypeThrowsInvalidArgumentException()
6887
{
6988
$mobytOptions = new MobytOptions();
70-
$mobytOptions->messageType('foo');
7189

72-
$this->assertSame(['message_type' => 'foo'], $mobytOptions->toArray());
90+
$this->expectException(InvalidArgumentException::class);
91+
$this->expectExceptionMessage('The message type "foo-bar" is not supported; supported message types are: "N", "L", "LL"');
92+
93+
$mobytOptions->messageType('foo-bar');
94+
}
95+
96+
public function testSettingMessageTypeViaConstructorWithUnknownTypeThrowsInvalidArgumentException()
97+
{
98+
$this->expectException(InvalidArgumentException::class);
99+
$this->expectExceptionMessage(
100+
'The message type "foo-bar" is not supported; supported message types are: "N", "L", "LL"'
101+
);
102+
103+
new MobytOptions([
104+
'message_type' => 'foo-bar',
105+
]);
73106
}
74107
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy