Skip to content

[Notifier] [Mobyt] Change ctor signature and validate message types #39587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Notifier/Bridge/Mobyt/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* The bridge is not marked as `@experimental` anymore
* Validate message types

5.2.0
-----
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mobyt/MobytOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Notifier\Bridge\Mobyt;

use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
use Symfony\Component\Notifier\Notification\Notification;

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

public function __construct(array $options = [])
{
if (isset($options['message_type'])) {
self::validateMessageType($options['message_type']);
}

$this->options = $options;
}

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

public function messageType(string $type)
{
self::validateMessageType($type);

$this->options['message_type'] = $type;
}

public static function validateMessageType($type): string
{
if (!\in_array($type, $supported = [self::MESSAGE_TYPE_QUALITY_HIGH, self::MESSAGE_TYPE_QUALITY_MEDIUM, self::MESSAGE_TYPE_QUALITY_LOW], true)) {
throw new InvalidArgumentException(sprintf('The message type "%s" is not supported; supported message types are: "%s"', $type, implode('", "', $supported)));
}

return $type;
}
}
14 changes: 10 additions & 4 deletions src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ final class MobytTransport extends AbstractTransport
private $from;
private $typeQuality;

public function __construct(string $accountSid, string $authToken, string $from, string $typeQuality, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
public function __construct(string $accountSid, string $authToken, string $from, string $typeQuality = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to make that parameter nullable anyway?

Suggested change
public function __construct(string $accountSid, string $authToken, string $from, string $typeQuality = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
public function __construct(string $accountSid, string $authToken, string $from, string $typeQuality = MobytOptions::MESSAGE_TYPE_QUALITY_LOW, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest that staying BC from the ctor signature is easier if the default value can be handled in the ctor itself, isn't it? 🤔

Copy link
Contributor Author

@OskarStark OskarStark Jan 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah and we need to know the default value in the transport factory too:

public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
if ('mobyt' !== $scheme) {
throw new UnsupportedSchemeException($dsn, 'mobyt', $this->getSupportedSchemes());
}
$accountSid = $this->getUser($dsn);
$authToken = $this->getPassword($dsn);
$from = $dsn->getRequiredOption('from');
$typeQuality = $dsn->getOption('type_quality', MobytOptions::MESSAGE_TYPE_QUALITY_LOW);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();
return (new MobytTransport($accountSid, $authToken, $from, $typeQuality, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

By using null, only the transport sets and knows about the default value

{
$this->accountSid = $accountSid;
$this->authToken = $authToken;
$this->from = $from;

$typeQuality = $typeQuality ?? MobytOptions::MESSAGE_TYPE_QUALITY_LOW;
MobytOptions::validateMessageType($typeQuality);

$this->typeQuality = $typeQuality;

parent::__construct($client, $dispatcher);
Expand Down Expand Up @@ -77,14 +81,16 @@ protected function doSend(MessageInterface $message): SentMessage
'user_key: '.$this->accountSid,
'Access_token: '.$this->authToken,
],
'body' => json_encode(array_filter($options)),
'json' => array_filter($options),
]);

if (401 === $response->getStatusCode() || 404 === $response->getStatusCode()) {
$statusCode = $response->getStatusCode();

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

if (201 !== $response->getStatusCode()) {
if (201 !== $statusCode) {
$error = $response->toArray(false);

throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['result']), $response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function create(Dsn $dsn): TransportInterface
$accountSid = $this->getUser($dsn);
$authToken = $this->getPassword($dsn);
$from = $dsn->getRequiredOption('from');
$typeQuality = $dsn->getOption('type_quality', MobytOptions::MESSAGE_TYPE_QUALITY_LOW);
$typeQuality = $dsn->getOption('type_quality');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Notifier/Bridge/Mobyt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where:
- `USER_KEY` is your Mobyt user key
- `ACCESS_TOKEN` is your Mobyt access token
- `FROM` is the sender
- `TYPE_QUALITY` is the quality : `N` for high, `L` for medium, `LL` for low (default: `L`)
- `TYPE_QUALITY` is the quality of your message: `N` for high, `L` for medium, `LL` for low (default: `L`)

Resources
---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytOptions;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Notification\Notification;

final class MobytOptionsTest extends TestCase
Expand Down Expand Up @@ -64,11 +65,43 @@ public function testToArray()
$this->assertEmpty($mobytOptions->toArray());
}

public function testMessageType()
/**
* @dataProvider validMessageTypes
*/
public function testMessageType(string $type)
{
$mobytOptions = new MobytOptions();
$mobytOptions->messageType($type);

$this->assertSame(['message_type' => $type], $mobytOptions->toArray());
}

public function validMessageTypes(): iterable
{
yield [MobytOptions::MESSAGE_TYPE_QUALITY_HIGH];
yield [MobytOptions::MESSAGE_TYPE_QUALITY_MEDIUM];
yield [MobytOptions::MESSAGE_TYPE_QUALITY_LOW];
}

public function testCallingMessageTypeMethodWithUnknownTypeThrowsInvalidArgumentException()
{
$mobytOptions = new MobytOptions();
$mobytOptions->messageType('foo');

$this->assertSame(['message_type' => 'foo'], $mobytOptions->toArray());
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The message type "foo-bar" is not supported; supported message types are: "N", "L", "LL"');

$mobytOptions->messageType('foo-bar');
}

public function testSettingMessageTypeViaConstructorWithUnknownTypeThrowsInvalidArgumentException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'The message type "foo-bar" is not supported; supported message types are: "N", "L", "LL"'
);

new MobytOptions([
'message_type' => 'foo-bar',
]);
}
}