8000 [Notifier] Bring consistency to bridges by nicolas-grekas · Pull Request #50308 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Notifier] Bring consistency to bridges #50308

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
May 13, 2023
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
[Notifier] Bring consistency to bridges
  • Loading branch information
nicolas-grekas committed May 12, 2023
commit 657e9be3c306b8fc7592c00ea6b57c7c0cb8ea1b
104 changes: 33 additions & 71 deletions src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,108 +25,75 @@ public function __construct(array $options = [])
$this->options = $options;
}

public function getAlerting(): ?int
{
return $this->options['alerting'] ?? null;
}

public function getCampaignName(): ?string
{
return $this->options['campaign_name'] ?? null;
}

public function getCliMsgId(): ?string
{
return $this->options['cli_msg_id'] ?? null;
}

public function getDate(): ?string
{
return $this->options['date'] ?? null;
}

public function getFrom(): ?string
{
return $this->options['from'] ?? null;
}

public function getRecipientId(): ?string
{
return $this->options['recipient_id'] ?? null;
}

public function getSimulate(): ?int
{
return $this->options['simulate'] ?? null;
return null;
}

public function getUniqueIdentifier(): ?string
{
return $this->options['unique_identifier'] ?? null;
}

public function getVerbose(): ?int
{
return $this->options['verbose'] ?? null;
}

public function setAlerting(int $alerting): self
/**
* @return $this
*/
public function alerting(int $alerting): static
{
$this->options['alerting'] = $alerting;

return $this;
}

public function setCampaignName(string $campaignName): self
/**
* @return $this
*/
public function campaignName(string $campaignName): static
{
$this->options['campaign_name'] = $campaignName;
$this->options['campaignName'] = $campaignName;

return $this;
}

public function setCliMsgId(string $cliMsgId): self
/**
* @return $this
*/
public function cliMsgId(string $cliMsgId): static
{
$this->options['cli_msg_id'] = $cliMsgId;
$this->options['cliMsgId'] = $cliMsgId;

return $this;
}

public function setDate(string $date): self
/**
* @return $this
*/
public function date(string $date): static
{
$this->options['date'] = $date;

return $this;
}

public function setFrom(string $from): self
{
$this->options['from'] = $from;

return $this;
}

public function setRecipientId(string $id): self
{
$this->options['recipient_id'] = $id;

return $this;
}

public function setSimulate(int $simulate): self
/**
* @return $this
*/
public function simulate(int $simulate): static
{
$this->options['simulate'] = $simulate;

return $this;
}

public function setUniqueIdentifier(string $uniqueIdentifier): self
/**
* @return $this
*/
public function uniqueIdentifier(string $uniqueIdentifier): static
{
$this->options['unique_identifier'] = $uniqueIdentifier;
$this->options['uniqueIdentifier'] = $uniqueIdentifier;

return $this;
}

public function setVerbose(int $verbose): self
/**
* @return $this
*/
public function verbose(int $verbose): static
{
$this->options['verbose'] = $verbose;

Expand All @@ -135,11 +102,6 @@ public function setVerbose(int $verbose): self

public function toArray(): array
{
$options = $this->options;
if (isset($options['recipient_id'])) {
unset($options['recipient_id']);
}

return $options;
return $this->options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public function __construct(string $login, #[\SensitiveParameter] string $apiKey

public function __toString(): string
{
if (null !== $this->from) {
return sprintf('allmysms://%s?from=%s', $this->getEndpoint(), $this->from);
}

return sprintf('allmysms://%s', $this->getEndpoint());
return sprintf('allmysms://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : '');
}

public function supports(MessageInterface $message): bool
Expand All @@ -61,32 +57,14 @@ protected function doSend(MessageInterface $message): SentMessage
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
}

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

$opts = $message->getOptions();
$options = $opts ? $opts->toArray() : [];
$options['from'] = $options['from'] ?? $from;
$options = $message->getOptions()?->toArray() ?? [];
$options['from'] = $message->getFrom() ?: $this->from;
$options['to'] = $message->getPhone();
$options['text'] = $message->getSubject();

if (isset($options['campaign_name'])) {
$options['campaignName'] = $options['campaign_name'];
unset($options['campaign_name']);
}

if (isset($options['cli_msg_id'])) {
$options['cliMsgId'] = $options['cli_msg_id'];
unset($options['cli_msg_id']);
}

if (isset($options['unique_identifier'])) {
$options['uniqueIdentifier'] = $options['unique_identifier'];
unset($options['unique_identifier']);
}

$endpoint = sprintf('https://%s/sms/send/', $this->getEndpoint());
$response = $this->client->request('POST', $endpoint, [
'auth_basic' => $this->login.':'.$this->apiKey,
'auth_basic' => [$this->login, $this->apiKey],
'json' => array_filter($options),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ class AllMySmsOptionsTest extends TestCase
{
public function testAllMySmsOptions()
{
$allMySmsOptions = (new AllMySmsOptions())->setFrom('test_from')->setAlerting(1)->setDate('test_date')->setCampaignName('test_campaign_name')->setRecipientId('test_recipient')->setCliMsgId('test_cli_msg_id')->setSimulate(1)->setUniqueIdentifier('test_unique_identifier')->setVerbose(1);
$allMySmsOptions = (new AllMySmsOptions())
->alerting(1)
->date('test_date')
->campaignName('test_campaign_name')
->cliMsgId('test_cli_msg_id')
->simulate(1)
->uniqueIdentifier('test_unique_identifier')
->verbose(1);

self::assertSame([
'from' => 'test_from',
'alerting' => 1,
'date' => 'test_date',
'campaign_name' => 'test_campaign_name',
'cli_msg_id' => 'test_cli_msg_id',
'campaignName' => 'test_campaign_name',
'cliMsgId' => 'test_cli_msg_id',
'simulate' => 1,
'unique_identifier' => 'test_unique_identifier',
'uniqueIdentifier' => 'test_unique_identifier',
'verbose' => 1,
], $allMySmsOptions->toArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,23 @@ protected function doSend(MessageInterface $message): SentMessage

if ($message instanceof ChatMessage && $message->getOptions() instanceof AmazonSnsOptions) {
$options = $message->getOptions()->toArray();
} else {
$options = [];
}
$options['Message'] = $message->getSubject();
$options[($message instanceof ChatMessage) ? 'TopicArn' : 'PhoneNumber'] = $message->getRecipientId();

if ($message instanceof SmsMessage) {
$options['PhoneNumber'] = $message->getPhone();
} else {
$options['TopicArn'] = $message->getRecipientId();
}

try {
$response = $this->snsClient->publish($options);
$message = new SentMessage($message, (string) $this);
$message->setMessageId($response->getMessageId());
} catch (\Exception $exception) {
$info = isset($response) ? $response->info() : [];
$info = $response?->info() ?? [];
throw new TransportException('Unable to send the message.', $info['response'] ?? null, $info['status'] ?? 0, $exception);
}

Expand Down
Loading
0