diff --git a/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsOptions.php b/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsOptions.php index 72d71d57879df..17cdeaef29f67 100644 --- a/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsOptions.php @@ -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; @@ -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; } } diff --git a/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsTransport.php b/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsTransport.php index 63c3fc7906d5e..88174a8b3610c 100644 --- a/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/AllMySms/AllMySmsTransport.php @@ -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 @@ -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), ]); diff --git a/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsOptionsTest.php index 4bef488eb6bc5..c0927a17ed47d 100644 --- a/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/AllMySms/Tests/AllMySmsOptionsTest.php @@ -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()); } diff --git a/src/Symfony/Component/Notifier/Bridge/AmazonSns/AmazonSnsTransport.php b/src/Symfony/Component/Notifier/Bridge/AmazonSns/AmazonSnsTransport.php index 2b9b7ee4ce0ad..4ebef70a46c7a 100644 --- a/src/Symfony/Component/Notifier/Bridge/AmazonSns/AmazonSnsTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/AmazonSns/AmazonSnsTransport.php @@ -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); } diff --git a/src/Symfony/Component/Notifier/Bridge/Bandwidth/BandwidthOptions.php b/src/Symfony/Component/Notifier/Bridge/Bandwidth/BandwidthOptions.php index 65083c8b81bad..d88a00cddf007 100644 --- a/src/Symfony/Component/Notifier/Bridge/Bandwidth/BandwidthOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Bandwidth/BandwidthOptions.php @@ -25,121 +25,73 @@ public function __construct(array $options = []) $this->options = $options; } - public function getAccountId(): ?string - { - return $this->options['account_id'] ?? null; - } - - public function getApplicationId(): ?string - { - return $this->options['application_id'] ?? null; - } - - public function getExpiration(): ?string - { - return $this->options['expiration'] ?? null; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - - public function getMedia(): ?array - { - return $this->options['media'] ?? null; - } - - public function getPriority(): ?string - { - return $this->options['priority'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; - } - - public function getTag(): ?string - { - return $this->options['tag'] ?? null; + return null; } - public function getTo(): ?array + /** + * @return $this + */ + public function accountId(string $accountId): static { - return $this->options['to'] ?? null; - } - - public function setAccountId(string $accountId): self - { - $this->options['account_id'] = $accountId; + $this->options['accountId'] = $accountId; return $this; } - public function setApplicationId(string $applicationId): self + /** + * @return $this + */ + public function applicationId(string $applicationId): static { - $this->options['application_id'] = $applicationId; + $this->options['applicationId'] = $applicationId; return $this; } - public function setExpiration(string $expiration): self + /** + * @return $this + */ + public function expiration(string $expiration): static { $this->options['expiration'] = $expiration; return $this; } - public function setFrom(string $from): self - { - $this->options['from'] = $from; - - return $this; - } - - public function setMedia(array $media): self + /** + * @return $this + */ + public function media(array $media): static { $this->options['media'] = $media; return $this; } - public function setPriority(string $priority): self + /** + * @return $this + */ + public function priority(string $priority): static { $this->options['priority'] = $priority; return $this; } - public function setRecipientId(string $id): self - { - $this->options['recipient_id'] = $id; - - return $this; - } - - public function setTag(string $tag): self + /** + * @return $this + */ + public function tag(string $tag): static { $this->options['tag'] = $tag; return $this; } - public function setTo(array $to): self - { - $this->options['to'] = $to; - - return $this; - } - public function toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/Bandwidth/BandwidthTransport.php b/src/Symfony/Component/Notifier/Bridge/Bandwidth/BandwidthTransport.php index d70ef3dffab33..51a85737be277 100644 --- a/src/Symfony/Component/Notifier/Bridge/Bandwidth/BandwidthTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Bandwidth/BandwidthTransport.php @@ -44,7 +44,7 @@ public function __construct( public function __toString(): string { - return sprintf('bandwidth://%s?from=%s&account_id=%s&application_id=%s', $this->getEndpoint(), $this->from, $this->accountId, $this->applicationId).($this->priority ? sprintf('&priority=%s', $this->priority) : null); + return sprintf('bandwidth://%s?from=%s&account_id=%s&application_id=%s%s', $this->getEndpoint(), $this->from, $this->accountId, $this->applicationId, null !== $this->priority ? '&priority='.$this->priority : ''); } public function supports(MessageInterface $message): bool @@ -60,31 +60,26 @@ protected function doSend(MessageInterface $message): SentMessage if (!$message instanceof SmsMessage) { throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['text'] = $message->getSubject(); - $options['from'] = $options['from'] ?? $this->from; - $options['to'] = $options['to'] ?? [$message->getPhone()]; - $options['account_id'] = $options['account_id'] ?? $this->accountId; - $options['applicationId'] = $options['application_id'] ?? $this->applicationId; - unset($options['application_id']); - - if (!isset($options['priority']) && $this->priority) { - $options['priority'] = $this->priority; - } - - if (!preg_match('/^\+[1-9]\d{1,14}$/', $this->from)) { - throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number. The number must be in E.164 format.', $this->from)); + $options['from'] = $message->getFrom() ?: $this->from; + $options['to'] = [$message->getPhone()]; + $options['accountId'] ??= $this->accountId; + $options['applicationId'] ??= $this->applicationId; + $options['priority'] ??= $this->priority; + + if (!preg_match('/^\+[1-9]\d{1,14}$/', $options['from'])) { + throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number. The number must be in E.164 format.', $options['from'])); } - if (!preg_match('/^\+[1-9]\d{1,14}$/', $message->getPhone())) { - throw new InvalidArgumentException(sprintf('The "To" number "%s" is not a valid phone number. The number must be in E.164 format.', $message->getPhone())); + if (!preg_match('/^\+[1-9]\d{1,14}$/', $options['to'][0])) { + throw new InvalidArgumentException(sprintf('The "To" number "%s" is not a valid phone number. The number must be in E.164 format.', $options['to'][0])); } - $endpoint = sprintf('https://%s/api/v2/users/%s/messages', $this->getEndpoint(), $options['account_id']); + $endpoint = sprintf('https://%s/api/v2/users/%s/messages', $this->getEndpoint(), $options['accountId']); unset($options['accountId']); $response = $this->client->request('POST', $endpoint, [ - 'auth_basic' => $this->username.':'.$this->password, + 'auth_basic' => [$this->username, $this->password], 'json' => array_filter($options), ]); diff --git a/src/Symfony/Component/Notifier/Bridge/Bandwidth/Tests/BandwidthOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Bandwidth/Tests/BandwidthOptionsTest.php index 9911f5d64aa15..6d805eecc8326 100644 --- a/src/Symfony/Component/Notifier/Bridge/Bandwidth/Tests/BandwidthOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Bandwidth/Tests/BandwidthOptionsTest.php @@ -18,15 +18,19 @@ class BandwidthOptionsTest extends TestCase { public function testBandwidthOptions() { - $bandwidthOptions = (new BandwidthOptions())->setFrom('test_from')->setRecipientId('test_recipient')->setMedia(['foo'])->setTo(['test_too'])->setTag('test_tag')->setAccountId('test_account_id')->setApplicationId('test_application_id')->setExpiration('test_expiration')->setPriority('test_priority'); + $bandwidthOptions = (new BandwidthOptions()) + ->media(['foo']) + ->tag('test_tag') + ->accountId('test_account_id') + ->applicationId('test_application_id') + ->expiration('test_expiration') + ->priority('test_priority'); self::assertSame([ - 'from' => 'test_from', 'media' => ['foo'], - 'to' => ['test_too'], 'tag' => 'test_tag', - 'account_id' => 'test_account_id', - 'application_id' => 'test_application_id', + 'accountId' => 'test_account_id', + 'applicationId' => 'test_application_id', 'expiration' => 'test_expiration', 'priority' => 'test_priority', ], $bandwidthOptions->toArray()); diff --git a/src/Symfony/Component/Notifier/Bridge/Chatwork/ChatworkOptions.php b/src/Symfony/Component/Notifier/Bridge/Chatwork/ChatworkOptions.php index 7c6c0e164f975..bbdf11f534934 100644 --- a/src/Symfony/Component/Notifier/Bridge/Chatwork/ChatworkOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Chatwork/ChatworkOptions.php @@ -32,9 +32,12 @@ public function toArray(): array public function getRecipientId(): ?string { - return ''; + return null; } + /** + * @return $this + */ public function to(array|string $userIds): static { $this->options['to'] = $userIds; @@ -42,6 +45,9 @@ public function to(array|string $userIds): static return $this; } + /** + * @return $this + */ public function selfUnread(bool $selfUnread): static { $this->options['selfUnread'] = $selfUnread; diff --git a/src/Symfony/Component/Notifier/Bridge/Chatwork/ChatworkTransport.php b/src/Symfony/Component/Notifier/Bridge/Chatwork/ChatworkTransport.php index fae4fa53471e0..ba1c5ffe3d0da 100644 --- a/src/Symfony/Component/Notifier/Bridge/Chatwork/ChatworkTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Chatwork/ChatworkTransport.php @@ -54,8 +54,7 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - $messageOptions = $message->getOptions(); - $options = $messageOptions ? $messageOptions->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $bodyBuilder = new ChatworkMessageBodyBuilder(); if (\array_key_exists('to', $options)) { @@ -74,7 +73,6 @@ protected function doSend(MessageInterface $message): SentMessage 'body' => $messageBody, 'headers' => [ 'X-ChatWorkToken' => $this->apiToken, - 'Content-Type' => 'application/x-www-form-urlencoded', ], ]); diff --git a/src/Symfony/Component/Notifier/Bridge/ClickSend/ClickSendOptions.php b/src/Symfony/Component/Notifier/Bridge/ClickSend/ClickSendOptions.php index e8a7ad26fa94a..ea9b71f919134 100644 --- a/src/Symfony/Component/Notifier/Bridge/ClickSend/ClickSendOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/ClickSend/ClickSendOptions.php @@ -25,96 +25,65 @@ public function __construct(array $options = []) $this->options = $options; } - public function getCountry(): ?string - { - return $this->options['country'] ?? null; - } - - public function getCustomString(): ?string - { - return $this->options['custom_string'] ?? null; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - - public function getFromEmail(): ?string - { - return $this->options['from_email'] ?? null; - } - - public function getListId(): ?string - { - return $this->options['list_id'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; - } - - public function getSchedule(): ?int - { - return $this->options['schedule'] ?? null; - } - - public function getSource(): ?string - { - return $this->options['source'] ?? null; + return null; } - public function setCountry(string $country): self + /** + * @return $this + */ + public function country(string $country): static { $this->options['country'] = $country; return $this; } - public function setCustomString(string $customString): self + /** + * @return $this + */ + public function customString(string $customString): static { $this->options['custom_string'] = $customString; return $this; } - public function setFrom(string $from): self - { - $this->options['from'] = $from; - - return $this; - } - - public function setFromEmail(string $fromEmail): self + /** + * @return $this + */ + public function fromEmail(string $fromEmail): static { $this->options['from_email'] = $fromEmail; return $this; } - public function setListId(string $listId): self + /** + * @return $this + */ + public function listId(string $listId): static { $this->options['list_id'] = $listId; return $this; } - public function setRecipientId(string $id): self - { - $this->options['recipient_id'] = $id; - - return $this; - } - - public function setSchedule(int $schedule): self + /** + * @return $this + */ + public function schedule(int $schedule): static { $this->options['schedule'] = $schedule; return $this; } - public function setSource(string $source): self + /** + * @return $this + */ + public function source(string $source): static { $this->options['source'] = $source; @@ -123,11 +92,6 @@ public function setSource(string $source): self public function toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/ClickSend/ClickSendTransport.php b/src/Symfony/Component/Notifier/Bridge/ClickSend/ClickSendTransport.php index 6ab50491f3e52..680050ecd6b36 100644 --- a/src/Symfony/Component/Notifier/Bridge/ClickSend/ClickSendTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/ClickSend/ClickSendTransport.php @@ -44,21 +44,14 @@ public function __construct( public function __toString(): string { - $queryParameters = []; - if ($this->from) { - $queryParameters['from'] = $this->from; - } - if ($this->source) { - $queryParameters['source'] = $this->source; - } - if ($this->listId) { - $queryParameters['list_id'] = $this->listId; - } - if ($this->fromEmail) { - $queryParameters['from_email'] = $this->fromEmail; - } + $query = array_filter([ + 'from' => $this->from, + 'source' => $this->source, + 'list_id' => $this->listId, + 'from_email' => $this->fromEmail, + ]); - return sprintf('clicksend://%s', $this->getEndpoint()).($queryParameters ? '?'.http_build_query($queryParameters) : null); + return sprintf('clicksend://%s%s', $this->getEndpoint(), $query ? '?'.http_build_query($query, '', '&') : ''); } public function supports(MessageInterface $message): bool @@ -77,28 +70,15 @@ protected function doSend(MessageInterface $message): SentMessage $endpoint = sprintf('https://%s/v3/sms/send', $this->getEndpoint()); - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['body'] = $message->getSubject(); - - if (!isset($options['from']) && $this->from) { - $options['from'] = $this->from; - } + $options['from'] = $message->getFrom() ?: $this->from; + $options['source'] ??= $this->source; + $options['list_id'] ??= $this->listId; + $options['from_email'] ?? $this->fromEmail; if (isset($options['from']) && !preg_match('/^[a-zA-Z0-9\s]{3,11}$/', $options['from']) && !preg_match('/^\+[1-9]\d{1,14}$/', $options['from'])) { - throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $this->from)); - } - - if (!isset($options['source']) && $this->source) { - $options['source'] = $this->source; - } - - if (!isset($options['list_id']) && $this->listId) { - $options['list_id'] = $this->listId; - } - - if (!isset($options['from_email']) && $this->fromEmail) { - $options['from_email'] = urldecode($this->fromEmail); + throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $options['from'])); } if ($options['list_id'] ?? false) { @@ -106,8 +86,8 @@ protected function doSend(MessageInterface $message): SentMessage } $response = $this->client->request('POST', $endpoint, [ - 'auth_basic' => $this->apiUsername.':'.$this->apiKey, - 'json' => array_filter($options), + 'auth_basic' => [$this->apiUsername, $this->apiKey], + 'json' => array_filter($options), ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendOptionsTest.php index b1b97f2376a2a..5020a07f5acde 100644 --- a/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendOptionsTest.php @@ -18,10 +18,15 @@ class ClickSendOptionsTest extends TestCase { public function testClickSendOptions() { - $clickSendOptions = (new ClickSendOptions())->setFrom('test_from')->setCountry('test_country')->setCustomString('test_custom_string')->setFromEmail('test_from_email')->setListId('test_list_id')->setRecipientId('test_recipient_id')->setSchedule(999)->setSource('test_source'); + $clickSendOptions = (new ClickSendOptions()) + ->country('test_country') + ->customString('test_custom_string') + ->fromEmail('test_from_email') + ->listId('test_list_id') + ->schedule(999) + ->source('test_source'); self::assertSame([ - 'from' => 'test_from', 'country' => 'test_country', 'custom_string' => 'test_custom_string', 'from_email' => 'test_from_email', diff --git a/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendTransportFactoryTest.php index 6bcd8e94bced8..670447f3390b0 100644 --- a/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendTransportFactoryTest.php @@ -21,7 +21,7 @@ public function createFactory(): ClickSendTransportFactory return new ClickSendTransportFactory(); } - public function createProvider(): iterable + public static function createProvider(): iterable { yield ['clicksend://host.test', 'clicksend://apiUsername:ApiKey@host.test']; yield ['clicksend://host.test?from=15556667777', 'clicksend://apiUsername:ApiKey@host.test?from=15556667777']; @@ -31,19 +31,19 @@ public function createProvider(): iterable yield ['clicksend://host.test?from=15556667777&source=api&list_id=1&from_email=foo%40bar.com', 'clicksend://apiUsername:ApiKey@host.test?from=15556667777&source=api&list_id=1&from_email=foo%40bar.com']; } - public function incompleteDsnProvider(): iterable + public static function incompleteDsnProvider(): iterable { yield 'missing API username and API key' => ['clicksend://@default']; yield 'missing API username or API key' => ['clicksend://apiUsername@default']; } - public function supportsProvider(): iterable + public static function supportsProvider(): iterable { yield [true, 'clicksend://apiUsername:apiKey@default']; yield [false, 'somethingElse://apiUsername:apiKey@default']; } - public function unsupportedSchemeProvider(): iterable + public static function unsupportedSchemeProvider(): iterable { yield ['somethingElse://apiUsername:apiKey@default']; } diff --git a/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendTransportTest.php b/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendTransportTest.php index e2bee3b5d47ab..cf8eabab0d2ec 100644 --- a/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/ClickSend/Tests/ClickSendTransportTest.php @@ -16,26 +16,26 @@ use Symfony\Component\Notifier\Bridge\ClickSend\ClickSendTransport; use Symfony\Component\Notifier\Exception\InvalidArgumentException; use Symfony\Component\Notifier\Message\ChatMessage; -use Symfony\Component\Notifier\Message\MessageInterface; use Symfony\Component\Notifier\Message\SmsMessage; use Symfony\Component\Notifier\Test\TransportTestCase; +use Symfony\Component\Notifier\Tests\Transport\DummyMessage; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; final class ClickSendTransportTest extends TransportTestCase { - public function createTransport(HttpClientInterface $client = null, string $from = 'test_from', string $source = 'test_source', int $listId = 99, string $fromEmail = 'foo@bar.com'): ClickSendTransport + public static function createTransport(HttpClientInterface $client = null, string $from = 'test_from', string $source = 'test_source', int $listId = 99, string $fromEmail = 'foo@bar.com'): ClickSendTransport { - return new ClickSendTransport('test_username', 'test_key', $from, $source, $listId, $fromEmail, $client ?? $this->createMock(HttpClientInterface::class)); + return new ClickSendTransport('test_username', 'test_key', $from, $source, $listId, $fromEmail, $client ?? new MockHttpClient()); } - public function invalidFromProvider(): iterable + public static function invalidFromProvider(): iterable { yield 'no zero at start if phone number' => ['+0']; yield 'phone number too short' => ['+1']; } - public function supportedMessagesProvider(): iterable + public static function supportedMessagesProvider(): iterable { yield [new SmsMessage('0611223344', 'Hello!')]; yield [new SmsMessage('0611223344', 'Hello!', 'from', new ClickSendOptions(['custom_string' => 'test_custom_string']))]; @@ -73,18 +73,18 @@ public function testNoInvalidArgumentExceptionIsThrownIfFromIsValid(string $from $transport->send($message); } - public function toStringProvider(): iterable + public static function toStringProvider(): iterable { - yield ['clicksend://rest.clicksend.com?from=test_from&source=test_source&list_id=99&from_email=foo%40bar.com', $this->createTransport()]; + yield ['clicksend://rest.clicksend.com?from=test_from&source=test_source&list_id=99&from_email=foo%40bar.com', self::createTransport()]; } - public function unsupportedMessagesProvider(): iterable + public static function unsupportedMessagesProvider(): iterable { yield [new ChatMessage('Hello!')]; - yield [$this->createMock(MessageInterface::class)]; + yield [new DummyMessage()]; } - public function validFromProvider(): iterable + public static function validFromProvider(): iterable { yield ['abc']; yield ['abcd']; diff --git a/src/Symfony/Component/Notifier/Bridge/Clickatell/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/Clickatell/CHANGELOG.md index ff34c9a19a880..5092c668a1f20 100644 --- a/src/Symfony/Component/Notifier/Bridge/Clickatell/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/Clickatell/CHANGELOG.md @@ -1,11 +1,6 @@ CHANGELOG ========= -6.3 ---- - - * Add `ClickatellOptions` class - 6.2 --- diff --git a/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellOptions.php b/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellOptions.php deleted file mode 100644 index d4af576bcd19f..0000000000000 --- a/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellOptions.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Notifier\Bridge\Clickatell; - -use Symfony\Component\Notifier\Message\MessageOptionsInterface; - -/** - * @author gnito-org - */ -final class ClickatellOptions implements MessageOptionsInterface -{ - private array $options; - - public function __construct(array $options = []) - { - $this->options = $options; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - - public function getRecipientId(): ?string - { - return $this->options['recipient_id'] ?? null; - } - - 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 toArray(): array - { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; - } -} diff --git a/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellTransport.php b/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellTransport.php index d52e54c4a3e72..40b555c07185d 100644 --- a/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Clickatell/ClickatellTransport.php @@ -45,12 +45,12 @@ public function __toString(): string return sprintf('clickatell://%s', $this->getEndpoint()); } - return sprintf('clickatell://%s?from=%s', $this->getEndpoint(), $this->from); + return sprintf('clickatell://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : ''); } public function supports(MessageInterface $message): bool { - return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof ClickatellOptions); + return $message instanceof SmsMessage; } protected function doSend(MessageInterface $message): SentMessage @@ -61,11 +61,8 @@ protected function doSend(MessageInterface $message): SentMessage $endpoint = sprintf('https://%s/rest/message', $this->getEndpoint()); - $from = $message->getFrom() ?: $this->from ?: ''; - - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; - $options['from'] = $options['from'] ?? $from; + $options = []; + $options['from'] = $message->getFrom() ?: $this->from; $options['to'] = $message->getPhone(); $options['text'] = $message->getSubject(); diff --git a/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellOptionsTest.php deleted file mode 100644 index 7b84423df7604..0000000000000 --- a/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellOptionsTest.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Notifier\Bridge\Clickatell\Tests; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellOptions; - -class ClickatellOptionsTest extends TestCase -{ - public function testClickatellOptions() - { - $clickatellOptions = (new ClickatellOptions())->setFrom('test_from')->setRecipientId('test_recipient'); - - self::assertSame(['from' => 'test_from'], $clickatellOptions->toArray()); - } -} diff --git a/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellTransportTest.php index b43c17f5f3e7a..bf3de264e73ad 100644 --- a/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Clickatell/Tests/ClickatellTransportTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Notifier\Bridge\Clickatell\Tests; use Symfony\Component\HttpClient\MockHttpClient; -use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellOptions; use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransport; use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Exception\TransportException; @@ -40,7 +39,6 @@ public static function toStringProvider(): iterable public static function supportedMessagesProvider(): iterable { yield [new SmsMessage('+33612345678', 'Hello!')]; - yield [new SmsMessage('+33612345678', 'Hello!', 'from', new ClickatellOptions(['from' => 'foo']))]; } public static function unsupportedMessagesProvider(): iterable diff --git a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneOptions.php b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneOptions.php index 4891c06e4a2ab..be8d2c3f5ffb3 100644 --- a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneOptions.php @@ -25,61 +25,33 @@ public function __construct(array $options = []) $this->options = $options; } - public function getDiffusionName(): ?int - { - return $this->options['diffusion_name'] ?? null; - } - - public function getCategory(): ?string - { - return $this->options['category'] ?? null; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; + return null; } - public function setDiffusionName(int $diffusionName): self + /** + * @return $this + */ + public function diffusionName(int $diffusionName): static { $this->options['diffusion_name'] = $diffusionName; return $this; } - public function setCategory(string $category): self + /** + * @return $this + */ + public function category(string $category): static { $this->options['category'] = $category; 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 toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneTransport.php b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneTransport.php index 3b807cabc7b15..0f09f1e54cde6 100644 --- a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/ContactEveryoneTransport.php @@ -72,14 +72,13 @@ protected function doSend(MessageInterface $message): SentMessage throw new InvalidArgumentException(sprintf('The "%s" transport does not support "from" in "%s".', __CLASS__, SmsMessage::class)); } - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['xcharset'] = 'true'; $options['token'] = $this->token; $options['to'] = $message->getPhone(); $options['msg'] = $message->getSubject(); - $endpoint = sprintf('https://%s/api/light/diffusions/sms', self::HOST); + $endpoint = sprintf('https://%s/api/light/diffusions/sms', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'query' => array_filter($options), ]); diff --git a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneOptionsTest.php index a272c0bbe0709..79f70d6cfe0e8 100644 --- a/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/ContactEveryone/Tests/ContactEveryoneOptionsTest.php @@ -18,12 +18,13 @@ class ContactEveryoneOptionsTest extends TestCase { public function testContactEveryoneOptions() { - $contactEveryoneOptions = (new ContactEveryoneOptions())->setFrom('test_from')->setCategory('test_category')->setDiffusionName('test_diffusion_name')->setRecipientId('test_recipient'); + $contactEveryoneOptions = (new ContactEveryoneOptions()) + ->category('test_category') + ->diffusionName(123); self::assertSame([ - 'from' => 'test_from', 'category' => 'test_category', - 'diffusion_name' => 'test_diffusion_name', + 'diffusion_name' => 123, ], $contactEveryoneOptions->toArray()); } } diff --git a/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransport.php b/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransport.php index 628560f8ef1e5..0d591478f39b0 100644 --- a/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransport.php @@ -50,7 +50,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof ChatMessage; + return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof DiscordOptions); } /** @@ -62,17 +62,14 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - $messageOptions = $message->getOptions(); - $options = $messageOptions ? $messageOptions->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; + $options['content'] = $message->getSubject(); - $content = $message->getSubject(); - - if (mb_strlen($content, 'UTF-8') > self::SUBJECT_LIMIT) { + if (mb_strlen($options['content'], 'UTF-8') > self::SUBJECT_LIMIT) { throw new LengthException(sprintf('The subject length of a Discord message must not exceed %d characters.', self::SUBJECT_LIMIT)); } $endpoint = sprintf('https://%s/api/webhooks/%s/%s', $this->getEndpoint(), $this->webhookId, $this->token); - $options['content'] = $content; $response = $this->client->request('POST', $endpoint, [ 'json' => array_filter($options), ]); diff --git a/src/Symfony/Component/Notifier/Bridge/Engagespot/EngagespotTransport.php b/src/Symfony/Component/Notifier/Bridge/Engagespot/EngagespotTransport.php index 648d8ace989e7..f60ac4231763a 100644 --- a/src/Symfony/Component/Notifier/Bridge/Engagespot/EngagespotTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Engagespot/EngagespotTransport.php @@ -48,7 +48,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof PushMessage; + return $message instanceof PushMessage && (null === $message->getOptions() || $message->getOptions() instanceof EngagespotOptions); } protected function doSend(MessageInterface $message): SentMessage @@ -58,10 +58,8 @@ protected function doSend(MessageInterface $message): SentMessage } $endpoint = sprintf('https://%s', $this->getEndpoint()); - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; - if (!isset($options['to'])) { - $options['to'] = $message->getRecipientId(); - } + $options = $message->getOptions()?->toArray() ?? []; + $options['to'] ??= $message->getRecipientId(); $sendToEveryone = $options['everyone'] ?? false; if (!$sendToEveryone) { diff --git a/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexOptions.php b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexOptions.php index 8832a0117ec9d..6d7cb56946924 100644 --- a/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexOptions.php @@ -25,49 +25,23 @@ public function __construct(array $options = []) $this->options = $options; } - public function getAccountReference(): ?string - { - return $this->options['account_reference'] ?? null; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; + return null; } - public function setAccountReference(string $accountReference): self + /** + * @return $this + */ + public function accountReference(string $accountReference): static { - $this->options['account_reference'] = $accountReference; - - 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; + $this->options['accountreference'] = $accountReference; return $this; } public function toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransport.php b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransport.php index 66d7bbef27a60..e1fd0e3747c7b 100644 --- a/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransport.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Notifier\Bridge\Esendex; use Symfony\Component\HttpClient\Exception\JsonException; -use Symfony\Component\HttpClient\Exception\TransportException as HttpClientTransportException; use Symfony\Component\Notifier\Exception\TransportException; use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; use Symfony\Component\Notifier\Message\MessageInterface; @@ -58,20 +57,16 @@ 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['messages'] = [ 'to' => $message->getPhone(), 'body' => $message->getSubject(), ]; - $options['accountreference'] = $options['account_reference'] ?? $this->accountReference; - unset($options['account_reference']); + $options['accountreference'] ??= $this->accountReference; $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v1.0/messagedispatcher', [ - 'auth_basic' => sprintf('%s:%s', $this->email, $this->password), + 'auth_basic' => [$this->email, $this->password], 'headers' => [ 'Accept' => 'application/json', ], @@ -105,9 +100,6 @@ protected function doSend(MessageInterface $message): SentMessage $message .= sprintf(' Details from Esendex: %s: "%s".', $error['code'], $error['description']); } - } catch (HttpClientTransportException) { - // Catching this exception is useful to keep compatibility, with symfony/http-client < 4.4.10 - // See https://github.com/symfony/symfony/pull/37065 } catch (JsonException) { } diff --git a/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexOptionsTest.php index cea33875e2c63..f6e21223a167d 100644 --- a/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexOptionsTest.php @@ -18,11 +18,11 @@ class EsendexOptionsTest extends TestCase { public function testEsendexOptions() { - $esendexOptions = (new EsendexOptions())->setFrom('test_from')->setAccountReference('test_account_reference')->setRecipientId('test_recipient'); + $esendexOptions = (new EsendexOptions()) + ->accountReference('test_account_reference'); self::assertSame([ - 'from' => 'test_from', - 'account_reference' => 'test_account_reference', + 'accountreference' => 'test_account_reference', ], $esendexOptions->toArray()); } } diff --git a/src/Symfony/Component/Notifier/Bridge/Expo/ExpoOptions.php b/src/Symfony/Component/Notifier/Bridge/Expo/ExpoOptions.php index 9d49439e4c327..fc780b56158ac 100644 --- a/src/Symfony/Component/Notifier/Bridge/Expo/ExpoOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Expo/ExpoOptions.php @@ -38,13 +38,10 @@ public function __construct(string $to, array $options = [], array $data = []) public function toArray(): array { - return array_merge( - $this->options, - [ - 'to' => $this->to, - 'data' => $this->data, - ] - ); + return array_merge($this->options, [ + 'to' => $this->to, + 'data' => $this->data, + ]); } public function getRecipientId(): ?string diff --git a/src/Symfony/Component/Notifier/Bridge/Expo/ExpoTransport.php b/src/Symfony/Component/Notifier/Bridge/Expo/ExpoTransport.php index e97773fd219c1..c8e0ab617dd5a 100644 --- a/src/Symfony/Component/Notifier/Bridge/Expo/ExpoTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Expo/ExpoTransport.php @@ -60,11 +60,10 @@ protected function doSend(MessageInterface $message): SentMessage } $endpoint = sprintf('https://%s', $this->getEndpoint()); - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; - if (!isset($options['to'])) { - $options['to'] = $message->getRecipientId(); - } - if (null === $options['to']) { + $options = $message->getOptions()?->toArray() ?? []; + $options['to'] ??= $message->getRecipientId(); + + if (!$options['to']) { throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" option to be set.', __CLASS__)); } @@ -86,15 +85,15 @@ protected function doSend(MessageInterface $message): SentMessage } $contentType = $response->getHeaders(false)['content-type'][0] ?? ''; - $jsonContents = str_starts_with($contentType, 'application/json') ? $response->toArray(false) : null; + $result = str_starts_with($contentType, 'application/json') ? $response->toArray(false) : null; if (200 !== $statusCode) { - $errorMessage = $jsonContents['error']['message'] ?? $response->getContent(false); + $errorMessage = $result['error']['message'] ?? $response->getContent(false); throw new TransportException('Unable to post the Expo message: '.$errorMessage, $response); } - $result = $response->toArray(false); + $result ??= $response->toArray(false); if ('error' === $result['data']['status']) { throw new TransportException(sprintf('Unable to post the Expo message: "%s" (%s)', $result['data']['message'], $result['data']['details']['error']), $response); diff --git a/src/Symfony/Component/Notifier/Bridge/FakeSms/FakeSmsEmailTransport.php b/src/Symfony/Component/Notifier/Bridge/FakeSms/FakeSmsEmailTransport.php index dba79445f0681..a9efb7acf6343 100644 --- a/src/Symfony/Component/Notifier/Bridge/FakeSms/FakeSmsEmailTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/FakeSms/FakeSmsEmailTransport.php @@ -64,10 +64,8 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $email = (new Email()) - ->from($from) + ->from($message->getFrom() ?: $this->from) ->to($this->to) ->subject(sprintf('New SMS on phone number: %s', $message->getPhone())) ->html($message->getSubject()) diff --git a/src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseTransport.php b/src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseTransport.php index ef6902374f44f..5ba7f6ae6641d 100644 --- a/src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseTransport.php @@ -46,7 +46,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof ChatMessage; + return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof FirebaseOptions); } protected function doSend(MessageInterface $message): SentMessage @@ -56,14 +56,12 @@ protected function doSend(MessageInterface $message): SentMessage } $endpoint = sprintf('https://%s', $this->getEndpoint()); - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; - if (!isset($options['to'])) { - $options['to'] = $message->getRecipientId(); - } - if (null === $options['to']) { + $options = $message->getOptions()?->toArray() ?? []; + $options['to'] = $message->getRecipientId(); + + if (!$options['to']) { throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" option to be set.', __CLASS__)); } - $options['notification'] ??= []; $options['notification']['body'] = $message->getSubject(); $options['data'] ??= []; diff --git a/src/Symfony/Component/Notifier/Bridge/FortySixElks/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/FortySixElks/CHANGELOG.md index bc00d64a4339d..4662c26671e03 100644 --- a/src/Symfony/Component/Notifier/Bridge/FortySixElks/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/FortySixElks/CHANGELOG.md @@ -1,11 +1,6 @@ CHANGELOG ========= -6.3 ---- - - * Add `FortySixElksOptions` class - 6.2 --- diff --git a/src/Symfony/Component/Notifier/Bridge/FortySixElks/FortySixElksOptions.php b/src/Symfony/Component/Notifier/Bridge/FortySixElks/FortySixElksOptions.php deleted file mode 100644 index 79a53600fb717..0000000000000 --- a/src/Symfony/Component/Notifier/Bridge/FortySixElks/FortySixElksOptions.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Notifier\Bridge\FortySixElks; - -use Symfony\Component\Notifier\Message\MessageOptionsInterface; - -/** - * @author gnito-org - */ -final class FortySixElksOptions implements MessageOptionsInterface -{ - private array $options; - - public function __construct(array $options = []) - { - $this->options = $options; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - - public function getRecipientId(): ?string - { - return $this->options['recipient_id'] ?? null; - } - - 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 toArray(): array - { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; - } -} diff --git a/src/Symfony/Component/Notifier/Bridge/FortySixElks/FortySixElksTransport.php b/src/Symfony/Component/Notifier/Bridge/FortySixElks/FortySixElksTransport.php index 0c419e1c191c1..14316704d19bf 100644 --- a/src/Symfony/Component/Notifier/Bridge/FortySixElks/FortySixElksTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/FortySixElks/FortySixElksTransport.php @@ -48,7 +48,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof FortySixElksOptions); + return $message instanceof SmsMessage; } protected function doSend(MessageInterface $message): SentMessage @@ -57,15 +57,12 @@ 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 = []; + $options['from'] = $message->getFrom() ?: $this->from; $options['to'] = $message->getPhone(); $options['message'] = $message->getSubject(); - $endpoint = sprintf('https://%s/a1/sms', self::HOST); + $endpoint = sprintf('https://%s/a1/sms', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'auth_basic' => [$this->apiUsername, $this->apiPassword], 'body' => array_filter($options), diff --git a/src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksOptionsTest.php deleted file mode 100644 index 41c2094dd3e84..0000000000000 --- a/src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksOptionsTest.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Notifier\Bridge\FortySixElks\Tests; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksOptions; - -class FortySixElksOptionsTest extends TestCase -{ - public function testFortySixElksOptions() - { - $fortySixElksOptions = (new FortySixElksOptions())->setFrom('test_from')->setRecipientId('test_recipient'); - - self::assertSame([ - 'from' => 'test_from', - ], $fortySixElksOptions->toArray()); - } -} diff --git a/src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksTransportTest.php b/src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksTransportTest.php index fdf7b08055c7a..cf9fcd9fa3340 100644 --- a/src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksTransportTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Notifier\Bridge\FortySixElks\Tests; use Symfony\Component\HttpClient\MockHttpClient; -use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksOptions; use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksTransport; use Symfony\Component\Notifier\Exception\TransportException; use Symfony\Component\Notifier\Message\ChatMessage; @@ -38,7 +37,6 @@ public static function toStringProvider(): iterable public static function supportedMessagesProvider(): iterable { yield [new SmsMessage('+46701111111', 'Hello!')]; - yield [new SmsMessage('+46701111111', 'Hello!', 'from', new FortySixElksOptions(['from' => 'foo']))]; } public static function unsupportedMessagesProvider(): iterable diff --git a/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiOptions.php b/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiOptions.php index d983b6bf92cd1..4982d06c320bc 100644 --- a/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiOptions.php @@ -25,73 +25,43 @@ public function __construct(array $options = []) $this->options = $options; } - public function getClass(): ?int - { - return $this->options['class'] ?? null; - } - - public function getUserRef(): ?string - { - return $this->options['user_ref'] ?? null; - } - - public function getCallbackUrl(): ?string - { - return $this->options['callback_url'] ?? null; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; + return null; } - public function setClass(int $class): self + /** + * @return $this + */ + public function class(int $class): static { $this->options['class'] = $class; return $this; } - public function setUserRef(string $userRef): self + /** + * @return $this + */ + public function userRef(string $userRef): static { - $this->options['user_ref'] = $userRef; + $this->options['userref'] = $userRef; return $this; } - public function setCallbackUrl(string $callbackUrl): self + /** + * @return $this + */ + public function callbackUrl(string $callbackUrl): static { $this->options['callback_url'] = $callbackUrl; 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 toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php b/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php index f714df587efb1..e2c495a8a1cf4 100644 --- a/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php @@ -55,19 +55,11 @@ 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['sender'] = $options['from'] ?? $from; + $options = $message->getOptions()?->toArray() ?? []; + $options['sender'] = $message->getFrom() ?: $this->from; $options['recipients'] = [['msisdn' => $message->getPhone()]]; $options['message'] = $message->getSubject(); - if (isset($options['user_ref'])) { - $options['userref'] = $options['user_ref']; - unset($options['user_ref']); - } - $endpoint = sprintf('https://%s/rest/mtsms', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ diff --git a/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiOptionsTest.php index b0c40fad55e4f..085fabfeed000 100644 --- a/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiOptionsTest.php @@ -18,13 +18,15 @@ class GatewayApiOptionsTest extends TestCase { public function testGatewayApiOptions() { - $gatewayApiOptions = (new GatewayApiOptions())->setFrom('test_from')->setClass('test_class')->setCallbackUrl('test_callback_url')->setUserRef('test_user_ref')->setRecipientId('test_recipient'); + $gatewayApiOptions = (new GatewayApiOptions()) + ->class(123) + ->callbackUrl('test_callback_url') + ->userRef('test_user_ref'); self::assertSame([ - 'from' => 'test_from', - 'class' => 'test_class', + 'class' => 123, 'callback_url' => 'test_callback_url', - 'user_ref' => 'test_user_ref', + 'userref' => 'test_user_ref', ], $gatewayApiOptions->toArray()); } } diff --git a/src/Symfony/Component/Notifier/Bridge/GoogleChat/GoogleChatTransport.php b/src/Symfony/Component/Notifier/Bridge/GoogleChat/GoogleChatTransport.php index 2216afbc949db..b1a8507a98a43 100644 --- a/src/Symfony/Component/Notifier/Bridge/GoogleChat/GoogleChatTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/GoogleChat/GoogleChatTransport.php @@ -77,26 +77,20 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - if ($message->getOptions() && !$message->getOptions() instanceof GoogleChatOptions) { + if (($options = $message->getOptions()) && !$options instanceof GoogleChatOptions) { throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, GoogleChatOptions::class)); } - $opts = $message->getOptions(); - if (!$opts) { + if (!$options) { if ($notification = $message->getNotification()) { - $opts = GoogleChatOptions::fromNotification($notification); + $options = GoogleChatOptions::fromNotification($notification); } else { - $opts = GoogleChatOptions::fromMessage($message); + $options = GoogleChatOptions::fromMessage($message); } } - if (null !== $this->threadKey && null === $opts->getThreadKey()) { - $opts->setThreadKey($this->threadKey); - } - - $threadKey = $opts->getThreadKey() ?: $this->threadKey; + $threadKey = $options->getThreadKey() ?: $this->threadKey; - $options = $opts->toArray(); $url = sprintf('https://%s/v1/spaces/%s/messages?key=%s&token=%s%s', $this->getEndpoint(), $this->space, @@ -105,7 +99,7 @@ protected function doSend(MessageInterface $message): SentMessage $threadKey ? '&threadKey='.urlencode($threadKey) : '' ); $response = $this->client->request('POST', $url, [ - 'json' => array_filter($options), + 'json' => array_filter($options->toArray()), ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransport.php b/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransport.php index 4163a0b4ea410..1ba2a951a5df1 100644 --- a/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransport.php @@ -54,8 +54,6 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $endpoint = sprintf('https://%s/sms/2/text/advanced', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ @@ -65,7 +63,7 @@ protected function doSend(MessageInterface $message): SentMessage 'json' => [ 'messages' => [ [ - 'from' => $from, + 'from' => $message->getFrom() ?: $this->from, 'destinations' => [ [ 'to' => $message->getPhone(), diff --git a/src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransport.php b/src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransport.php index c7e87fbb9d780..331e6442eace2 100644 --- a/src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransport.php @@ -57,15 +57,13 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/messages/v2/send.json', [ 'json' => [ 'messages' => [ [ 'phone' => $message->getPhone(), 'text' => $message->getSubject(), - 'sender' => $from, + 'sender' => $message->getFrom() ?: $this->from, 'clientId' => uniqid(), ], ], diff --git a/src/Symfony/Component/Notifier/Bridge/KazInfoTeh/KazInfoTehTransport.php b/src/Symfony/Component/Notifier/Bridge/KazInfoTeh/KazInfoTehTransport.php index 60fcf0afca194..5d43bd353120f 100644 --- a/src/Symfony/Component/Notifier/Bridge/KazInfoTeh/KazInfoTehTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/KazInfoTeh/KazInfoTehTransport.php @@ -60,8 +60,6 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $originator = $message->getFrom() ?: $this->sender; - $endpoint = sprintf('http://%s/api', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'query' => [ @@ -70,7 +68,7 @@ protected function doSend(MessageInterface $message): SentMessage 'password' => $this->password, 'recipient' => $message->getPhone(), 'messagetype' => 'SMS:TEXT', - 'originator' => $originator, + 'originator' => $message->getFrom() ?: $this->sender, 'messagedata' => $message->getSubject(), ], ]); diff --git a/src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTransport.php b/src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTransport.php index dbfcd0c1289ca..af2fd456f79db 100644 --- a/src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTransport.php @@ -100,25 +100,19 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $data = [ 'login' => $this->login, 'phone' => $phone = $this->escapePhoneNumber($message->getPhone()), - 'sender' => $from, + 'sender' => $message->getFrom() ?: $this->from, 'text' => $message->getSubject(), 'timestamp' => time(), ]; $data['signature'] = $this->generateSignature($data); $endpoint = sprintf('https://%s/external/get/send.php', $this->getEndpoint()); - $response = $this->client->request( - 'GET', - $endpoint, - [ - 'query' => $data, - ] - ); + $response = $this->client->request('GET', $endpoint, [ + 'query' => $data, + ]); try { $statusCode = $response->getStatusCode(); diff --git a/src/Symfony/Component/Notifier/Bridge/LineNotify/LineNotifyTransport.php b/src/Symfony/Component/Notifier/Bridge/LineNotify/LineNotifyTransport.php index d4c14653d6e0d..035d1fc53e715 100644 --- a/src/Symfony/Component/Notifier/Bridge/LineNotify/LineNotifyTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/LineNotify/LineNotifyTransport.php @@ -41,13 +41,11 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - $content = $message->getSubject(); - $endpoint = sprintf('https://%s/api/notify', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'auth_bearer' => $this->token, 'query' => [ - 'message' => $content, + 'message' => $message->getSubject(), ], ]); diff --git a/src/Symfony/Component/Notifier/Bridge/LinkedIn/LinkedInTransport.php b/src/Symfony/Component/Notifier/Bridge/LinkedIn/LinkedInTransport.php index 8ad85e8f6511b..3cd4797a59e44 100644 --- a/src/Symfony/Component/Notifier/Bridge/LinkedIn/LinkedInTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/LinkedIn/LinkedInTransport.php @@ -30,8 +30,6 @@ */ final class LinkedInTransport extends AbstractTransport { - protected const PROTOCOL_VERSION = '2.0.0'; - protected const PROTOCOL_HEADER = 'X-Restli-Protocol-Version'; protected const HOST = 'api.linkedin.com'; private string $authToken; @@ -64,21 +62,21 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - if ($message->getOptions() && !$message->getOptions() instanceof LinkedInOptions) { + if (($options = $message->getOptions()) && !$options instanceof LinkedInOptions) { throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, LinkedInOptions::class)); } - if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) { - $opts = LinkedInOptions::fromNotification($notification); - $opts->author(new AuthorShare($this->accountId)); + if (!$options && $notification = $message->getNotification()) { + $options = LinkedInOptions::fromNotification($notification); + $options->author(new AuthorShare($this->accountId)); } $endpoint = sprintf('https://%s/v2/ugcPosts', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'auth_bearer' => $this->authToken, - 'headers' => [self::PROTOCOL_HEADER => self::PROTOCOL_VERSION], - 'json' => array_filter($opts ? $opts->toArray() : $this->bodyFromMessageWithNoOption($message)), + 'headers' => ['X-Restli-Protocol-Version' => '2.0.0'], + 'json' => array_filter($options?->toArray() ?? $this->bodyFromMessageWithNoOption($message)), ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/Mailjet/MailjetTransport.php b/src/Symfony/Component/Notifier/Bridge/Mailjet/MailjetTransport.php index d02760629cebe..acd39d9f34460 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mailjet/MailjetTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Mailjet/MailjetTransport.php @@ -55,14 +55,12 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $endpoint = sprintf('https://%s/v4/sms-send', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'auth_bearer' => $this->authToken, 'json' => [ - 'From' => $from, + 'From' => $message->getFrom() ?: $this->from, 'To' => $message->getPhone(), 'Text' => $message->getSubject(), ], diff --git a/src/Symfony/Component/Notifier/Bridge/Mastodon/MastodonOptions.php b/src/Symfony/Component/Notifier/Bridge/Mastodon/MastodonOptions.php index 477cd2d0b0082..2d6c316b03699 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mastodon/MastodonOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Mastodon/MastodonOptions.php @@ -33,8 +33,10 @@ public function getRecipientId(): ?string /** * @param string[] $choices + * + * @return $this */ - public function poll(array $choices, int $expiresIn): self + public function poll(array $choices, int $expiresIn): static { $this->options['poll'] = [ 'options' => $choices, @@ -44,7 +46,10 @@ public function poll(array $choices, int $expiresIn): self return $this; } - public function attachMedia(File $file, File $thumbnail = null, string $description = null, string $focus = null): self + /** + * @return $this + */ + public function attachMedia(File $file, File $thumbnail = null, string $description = null, string $focus = null): static { $this->options['attach'][] = [ 'file' => $file, diff --git a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostOptions.php b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostOptions.php index 41db757de13bb..564199b1211f8 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostOptions.php @@ -25,7 +25,10 @@ public function __construct(array $options = []) $this->options = $options; } - public function recipient(string $id): self + /** + * @return $this + */ + public function recipient(string $id): static { $this->options['recipient_id'] = $id; diff --git a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php index a863d4bcde846..36d9f11bc4574 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php @@ -46,7 +46,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof ChatMessage; + return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof MattermostOptions); } /** @@ -58,12 +58,9 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['message'] = $message->getSubject(); - - if (!isset($options['channel_id'])) { - $options['channel_id'] = $message->getRecipientId() ?: $this->channel; - } + $options['channel_id'] ??= $message->getRecipientId() ?: $this->channel; $endpoint = sprintf('https://%s/api/v4/posts', $this->getEndpoint()); diff --git a/src/Symfony/Component/Notifier/Bridge/Mercure/MercureTransport.php b/src/Symfony/Component/Notifier/Bridge/Mercure/MercureTransport.php index 84cd6c86eeca7..453bc33ca7dd2 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mercure/MercureTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Mercure/MercureTransport.php @@ -48,7 +48,7 @@ public function __construct(HubInterface $hub, string $hubId, string|array $topi public function __toString(): string { - return sprintf('mercure://%s?%s', $this->hubId, http_build_query(['topic' => $this->topics], '', '&')); + return sprintf('mercure://%s%s', $this->hubId, null !== $this->topics ? '?'.http_build_query(['topic' => $this->topics], '', '&') : ''); } public function supports(MessageInterface $message): bool diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdOptions.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdOptions.php index a7b270e00ab68..a701c81573bf8 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdOptions.php @@ -25,168 +25,125 @@ public function __construct(array $options = []) $this->options = $options; } - public function getCreatedDatetime(): ?string - { - return $this->options['created_datetime'] ?? null; - } - - public function getDataCoding(): ?string - { - return $this->options['data_coding'] ?? null; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - - public function getGateway(): ?int - { - return $this->options['gateway'] ?? null; - } - - public function getGroupIds(): ?array - { - return $this->options['group_ids'] ?? null; - } - - public function getMClass(): ?int - { - return $this->options['m_class'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; - } - - public function getReference(): ?string - { - return $this->options['reference'] ?? null; - } - - public function getReportUrl(): ?string - { - return $this->options['report_url'] ?? null; - } - - public function getScheduledDatetime(): ?string - { - return $this->options['scheduled_datetime'] ?? null; + return null; } - public function getShortenUrls(): ?bool + /** + * @return $this + */ + public function createdDatetime(string $createdDatetime): static { - return $this->options['shorten_urls'] ?? null; - } - - public function getType(): ?string - { - return $this->options['type'] ?? null; - } - - public function getTypeDetails(): ?string - { - return $this->options['type_details'] ?? null; - } - - public function getValidity(): ?int - { - return $this->options['validity'] ?? null; - } - - public function setCreatedDatetime(string $createdDatetime): self - { - $this->options['created_datetime'] = $createdDatetime; - - return $this; - } - - public function setDataCoding(string $dataCoding): self - { - $this->options['data_coding'] = $dataCoding; + $this->options['createdDatetime'] = $createdDatetime; return $this; } - public function setFrom(string $from): self + /** + * @return $this + */ + public function dataCoding(string $dataCoding): static { - $this->options['from'] = $from; + $this->options['dataCoding'] = $dataCoding; return $this; } - public function setGateway(int $gateway): self + /** + * @return $this + */ + public function gateway(int $gateway): static { $this->options['gateway'] = $gateway; return $this; } - public function setGroupIds(array $groupIds): self + /** + * @return $this + */ + public function groupIds(array $groupIds): static { - $this->options['group_ids'] = $groupIds; + $this->options['groupIds'] = $groupIds; return $this; } - public function setMClass(int $mClass): self + /** + * @return $this + */ + public function mClass(int $mClass): static { - $this->options['m_class'] = $mClass; + $this->options['mClass'] = $mClass; return $this; } - public function setRecipientId(string $id): self - { - $this->options['recipient_id'] = $id; - - return $this; - } - - public function setReference(string $reference): self + /** + * @return $this + */ + public function reference(string $reference): static { $this->options['reference'] = $reference; return $this; } - public function setReportUrl(string $reportUrl): self + /** + * @return $this + */ + public function reportUrl(string $reportUrl): static { - $this->options['report_url'] = $reportUrl; + $this->options['reportUrl'] = $reportUrl; return $this; } - public function setScheduledDatetime(string $scheduledDatetime): self + /** + * @return $this + */ + public function scheduledDatetime(string $scheduledDatetime): static { - $this->options['scheduled_datetime'] = $scheduledDatetime; + $this->options['scheduledDatetime'] = $scheduledDatetime; return $this; } - public function setShortenUrls(bool $shortenUrls): self + /** + * @return $this + */ + public function shortenUrls(bool $shortenUrls): static { - $this->options['shorten_urls'] = $shortenUrls; + $this->options['shortenUrls'] = $shortenUrls; return $this; } - public function setType(string $type): self + /** + * @return $this + */ + public function type(string $type): static { $this->options['type'] = $type; return $this; } - public function setTypeDetails(string $typeDetails): self + /** + * @return $this + */ + public function typeDetails(string $typeDetails): static { - $this->options['type_details'] = $typeDetails; + $this->options['typeDetails'] = $typeDetails; return $this; } - public function setValidity(int $validity): self + /** + * @return $this + */ + public function validity(int $validity): static { $this->options['validity'] = $validity; @@ -195,11 +152,6 @@ public function setValidity(int $validity): self public function toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php index 84a39bc771a84..502526b8ca3bf 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/MessageBirdTransport.php @@ -55,57 +55,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['originator'] = $options['from'] ?? $from; + $options = $message->getOptions()?->toArray() ?? []; + $options['originator'] = $message->getFrom() ?: $this->from; $options['recipients'] = [$message->getPhone()]; $options['body'] = $message->getSubject(); - if (isset($options['group_ids'])) { - $options['groupIds'] = $options['group_ids']; - unset($options['group_ids']); - } - - if (isset($options['report_url'])) { - $options['reportUrl'] = $options['report_url']; - unset($options['report_url']); - } - - if (isset($options['type_details'])) { - $options['typeDetails'] = $options['type_details']; - unset($options['type_details']); - } - - if (isset($options['data_coding'])) { - $options['datacoding'] = $options['data_coding']; - unset($options['data_coding']); - } - - if (isset($options['m_class'])) { - $options['mclass'] = $options['m_class']; - unset($options['m_class']); - } - - if (isset($options['shorten_urls'])) { - $options['shortenUrls'] = $options['shorten_urls']; - unset($options['shorten_urls']); - } - - if (isset($options['scheduled_datetime'])) { - $options['scheduledDatetime'] = $options['scheduled_datetime']; - unset($options['scheduled_datetime']); - } - - if (isset($options['created_datetime'])) { - $options['createdDatetime'] = $options['created_datetime']; - unset($options['created_datetime']); - } - $endpoint = sprintf('https://%s/messages', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ - 'auth_basic' => 'AccessKey:'.$this->token, + 'auth_basic' => ['AccessKey', $this->token], 'body' => array_filter($options), ]); diff --git a/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdOptionsTest.php index ab74f3220c992..b653271843710 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/MessageBird/Tests/MessageBirdOptionsTest.php @@ -18,21 +18,32 @@ class MessageBirdOptionsTest extends TestCase { public function testMessageBirdOptions() { - $messageBirdOptions = (new MessageBirdOptions())->setFrom('test_from')->setType('test_type')->setScheduledDatetime('test_scheduled_datetime')->setCreatedDatetime('test_created_datetime')->setRecipientId('test_recipient')->setDataCoding('test_data_coding')->setGateway(999)->setGroupIds(['test_group_ids'])->setMClass(888)->setReference('test_reference')->setReportUrl('test_report_url')->setShortenUrls(true)->setTypeDetails('test_type_details')->setValidity(777); + $messageBirdOptions = (new MessageBirdOptions()) + ->type('test_type') + ->scheduledDatetime('test_scheduled_datetime') + ->createdDatetime('test_created_datetime') + ->dataCoding('test_data_coding') + ->gateway(999) + ->groupIds(['test_group_ids']) + ->mClass(888) + ->reference('test_reference') + ->reportUrl('test_report_url') + ->shortenUrls(true) + ->typeDetails('test_type_details') + ->validity(777); self::assertSame([ - 'from' => 'test_from', 'type' => 'test_type', - 'scheduled_datetime' => 'test_scheduled_datetime', - 'created_datetime' => 'test_created_datetime', - 'data_coding' => 'test_data_coding', + 'scheduledDatetime' => 'test_scheduled_datetime', + 'createdDatetime' => 'test_created_datetime', + 'dataCoding' => 'test_data_coding', 'gateway' => 999, - 'group_ids' => ['test_group_ids'], - 'm_class' => 888, + 'groupIds' => ['test_group_ids'], + 'mClass' => 888, 'reference' => 'test_reference', - 'report_url' => 'test_report_url', - 'shorten_urls' => true, - 'type_details' => 'test_type_details', + 'reportUrl' => 'test_report_url', + 'shortenUrls' => true, + 'typeDetails' => 'test_type_details', 'validity' => 777, ], $messageBirdOptions->toArray()); } diff --git a/src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaOptions.php b/src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaOptions.php index e0ff03ace5289..e8f6804180ded 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaOptions.php @@ -25,120 +25,85 @@ public function __construct(array $options = []) $this->options = $options; } - public function getCallbackUrl(): ?string - { - return $this->options['callback_url'] ?? null; - } - - public function getDeliveryReport(): ?bool - { - return $this->options['delivery_report'] ?? null; - } - - public function getFormat(): ?string - { - return $this->options['format'] ?? null; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - - public function getMedia(): ?array - { - return $this->options['media'] ?? null; - } - - public function getMessageExpiryTimestamp(): ?int - { - return $this->options['message_expiry_timestamp'] ?? null; - } - - public function getMetadata(): ?array - { - return $this->options['metadata'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; - } - - public function getScheduled(): ?string - { - return $this->options['scheduled'] ?? null; + return null; } - public function getSubject(): ?string - { - return $this->options['subject'] ?? null; - } - - public function setCallbackUrl(string $callbackUrl): self + /** + * @return $this + */ + public function callbackUrl(string $callbackUrl): static { $this->options['callback_url'] = $callbackUrl; return $this; } - public function setDeliveryReport(bool $deliveryReport): self + /** + * @return $this + */ + public function deliveryReport(bool $deliveryReport): static { $this->options['delivery_report'] = $deliveryReport; return $this; } - public function setFormat(string $format): self + /** + * @return $this + */ + public function format(string $format): static { $this->options['format'] = $format; return $this; } - public function setFrom(string $from): self - { - $this->options['from'] = $from; - - return $this; - } - - public function setMedia(array $media): self + /** + * @return $this + */ + public function media(array $media): static { $this->options['media'] = $media; return $this; } - public function setMessageExpiryTimestamp(int $messageExpiryTimestamp): self + /** + * @return $this + */ + public function expiry(int $expiry): static { - $this->options['message_expiry_timestamp'] = $messageExpiryTimestamp; + $this->options['message_expiry_timestamp'] = $expiry; return $this; } - public function setMetadata(array $metadata): self + /** + * @return $this + */ + public function metadata(array $metadata): static { $this->options['metadata'] = $metadata; return $this; } - public function setRecipientId(string $id): self - { - $this->options['recipient_id'] = $id; - - return $this; - } - - public function setScheduled(string $scheduled): self + /** + * @return $this + */ + public function scheduled(string $scheduled): static { $this->options['scheduled'] = $scheduled; return $this; } - public function setSubject(string $subject): self + /** + * @return $this + */ + public function subject(string $subject): static { $this->options['subject'] = $subject; @@ -147,11 +112,6 @@ public function setSubject(string $subject): self public function toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaTransport.php b/src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaTransport.php index 3359816eac7c0..85b610b94cbfa 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaTransport.php @@ -62,29 +62,20 @@ 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['source_number'] = $options['from'] ?? $from; + $options = $message->getOptions()?->toArray() ?? []; + $options['source_number'] = $message->getFrom() ?: $this->from; $options['destination_number'] = $message->getPhone(); $options['content'] = $message->getSubject(); - unset($options['from']); - $endpoint = sprintf('https://%s/v1/messages', $this->getEndpoint()); - $response = $this->client->request( - 'POST', - $endpoint, - [ - 'auth_basic' => $this->apiKey.':'.$this->apiSecret, - 'json' => [ - 'messages' => [ - array_filter($options), - ], + $response = $this->client->request('POST', $endpoint, [ + 'auth_basic' => [$this->apiKey, $this->apiSecret], + 'json' => [ + 'messages' => [ + array_filter($options), ], - ] - ); + ], + ]); try { $statusCode = $response->getStatusCode(); diff --git a/src/Symfony/Component/Notifier/Bridge/MessageMedia/Tests/MessageMediaOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/MessageMedia/Tests/MessageMediaOptionsTest.php index bd349a0390162..917731a0b1189 100644 --- a/src/Symfony/Component/Notifier/Bridge/MessageMedia/Tests/MessageMediaOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/MessageMedia/Tests/MessageMediaOptionsTest.php @@ -18,10 +18,17 @@ class MessageMediaOptionsTest extends TestCase { public function testMessageMediaOptions() { - $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'); + $messageMediaOptions = (new MessageMediaOptions())-> + media(['test_media']) + ->callbackUrl('test_callback_url') + ->format('test_format') + ->deliveryReport(true) + ->expiry(999) + ->metadata(['test_metadata']) + ->scheduled('test_scheduled') + ->subject('test_subject'); self::assertSame([ - 'from' => 'test_from', 'media' => ['test_media'], 'callback_url' => 'test_callback_url', 'format' => 'test_format', diff --git a/src/Symfony/Component/Notifier/Bridge/MicrosoftTeams/MicrosoftTeamsTransport.php b/src/Symfony/Component/Notifier/Bridge/MicrosoftTeams/MicrosoftTeamsTransport.php index f392dce6fdb60..e725b91d9ad41 100644 --- a/src/Symfony/Component/Notifier/Bridge/MicrosoftTeams/MicrosoftTeamsTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/MicrosoftTeams/MicrosoftTeamsTransport.php @@ -46,7 +46,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof ChatMessage; + return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof MicrosoftTeamsOptions); } /** @@ -58,12 +58,7 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - if ($message->getOptions() && !$message->getOptions() instanceof MicrosoftTeamsOptions) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, MicrosoftTeamsOptions::class)); - } - - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; - + $options = $message->getOptions()?->toArray() ?? []; $options['text'] ??= $message->getSubject(); $path = $message->getRecipientId() ?? $this->path; diff --git a/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytOptions.php b/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytOptions.php index b2357951f4726..c226f371b1af3 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytOptions.php @@ -50,22 +50,24 @@ public static function fromNotification(Notification $notification): self public function toArray(): array { - $options = $this->options; - unset($options['message'], $options['recipient']); - - return $options; + return $this->options; } public function getRecipientId(): ?string { - return $this->options['recipient'] ?? null; + return null; } - public function messageType(string $type): void + /** + * @return $this + */ + public function messageType(string $type): static { self::validateMessageType($type); $this->options['message_type'] = $type; + + return $this; } public static function validateMessageType(string $type): string diff --git a/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransport.php b/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransport.php index 06f2fb3b15612..a9d48c4c7378a 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransport.php @@ -55,7 +55,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof SmsMessage; + return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof MobytOptions); } protected function doSend(MessageInterface $message): SentMessage @@ -64,27 +64,16 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - if ($message->getOptions() && !$message->getOptions() instanceof MobytOptions) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, MobytOptions::class)); - } - - $options = $message->getOptions() ? $message->getOptions()->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['message_type'] ??= $this->typeQuality; - - $options['message'] ??= $message->getSubject(); + $options['message'] = $message->getSubject(); $options['recipient'] = [$message->getPhone()]; - - if ('' !== $message->getFrom()) { - $options['sender'] = $message->getFrom(); - } else { - $options['sender'] ??= $this->from; - } + $options['sender'] = $message->getFrom() ?: $this->from; $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/API/v1.0/REST/sms', [ 'headers' => [ - 'Content-type: application/json', - 'user_key: '.$this->accountSid, - 'Access_token: '.$this->authToken, + 'user_key' => $this->accountSid, + 'Access_token' => $this->authToken, ], 'json' => array_filter($options), ]); diff --git a/src/Symfony/Component/Notifier/Bridge/Mobyt/Tests/MobytOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Mobyt/Tests/MobytOptionsTest.php index 40af3e310fe62..2d52b06c725a7 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mobyt/Tests/MobytOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Mobyt/Tests/MobytOptionsTest.php @@ -50,30 +50,6 @@ public function testFromNotificationDefaultLevel() $this->assertSame(MobytOptions::MESSAGE_TYPE_QUALITY_HIGH, $options['message_type']); } - public function testGetRecipientIdWhenSet() - { - $mobytOptions = new MobytOptions([ - 'recipient' => 'foo', - ]); - - $this->assertSame('foo', $mobytOptions->getRecipientId()); - } - - public function testGetRecipientIdWhenNotSet() - { - $this->assertNull((new MobytOptions())->getRecipientId()); - } - - public function testToArray() - { - $mobytOptions = new MobytOptions([ - 'message' => 'foo', - 'recipient' => 'bar', - ]); - - $this->assertEmpty($mobytOptions->toArray()); - } - /** * @dataProvider validMessageTypes */ diff --git a/src/Symfony/Component/Notifier/Bridge/Octopush/OctopushTransport.php b/src/Symfony/Component/Notifier/Bridge/Octopush/OctopushTransport.php index 601fdcd85fb0f..12fe24de95426 100644 --- a/src/Symfony/Component/Notifier/Bridge/Octopush/OctopushTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Octopush/OctopushTransport.php @@ -64,9 +64,6 @@ protected function doSend(MessageInterface $message): SentMessage $endpoint = sprintf('https://%s/api/sms/json', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ - 'headers' => [ - 'content_type' => 'multipart/form-data', - ], 'body' => [ 'user_login' => $this->userLogin, 'api_key' => $this->apiKey, diff --git a/src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalTransport.php b/src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalTransport.php index aae953843bed4..f146fca81b98f 100644 --- a/src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalTransport.php @@ -53,7 +53,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof PushMessage && (null !== $this->defaultRecipientId || ($message->getOptions() instanceof OneSignalOptions && null !== $message->getOptions()->getRecipientId())); + return $message instanceof PushMessage && (null === $message->getOptions() || $message->getOptions() instanceof OneSignalOptions); } /** @@ -65,12 +65,8 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message); } - if ($message->getOptions() && !$message->getOptions() instanceof OneSignalOptions) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, OneSignalOptions::class)); - } - - if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) { - $opts = OneSignalOptions::fromNotification($notification); + if (!($options = $message->getOptions()) && $notification = $message->getNotification()) { + $options = OneSignalOptions::fromNotification($notification); } $recipientId = $message->getRecipientId() ?? $this->defaultRecipientId; @@ -79,16 +75,11 @@ protected function doSend(MessageInterface $message): SentMessage throw new LogicException(sprintf('The "%s" transport should have configured `defaultRecipientId` via DSN or provided with message options.', __CLASS__)); } - $options = $opts ? $opts->toArray() : []; + $options = $options?->toArray() ?? []; $options['app_id'] = $this->appId; $options['include_player_ids'] = [$recipientId]; - - if (!isset($options['headings'])) { - $options['headings'] = ['en' => $message->getSubject()]; - } - if (!isset($options['contents'])) { - $options['contents'] = ['en' => $message->getContent()]; - } + $options['headings'] ??= ['en' => $message->getSubject()]; + $options['contents'] ??= ['en' => $message->getContent()]; $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/v1/notifications', [ 'headers' => [ diff --git a/src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalTransportTest.php b/src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalTransportTest.php index c5bf10f080cc5..868a87f8aaf9d 100644 --- a/src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalTransportTest.php @@ -72,11 +72,6 @@ public static function unsupportedMessagesProvider(): iterable yield [new DummyMessage()]; } - public function testUnsupportedWithoutRecipientId() - { - $this->assertFalse(self::createTransport()->supports(new PushMessage('Hello', 'World'))); - } - public function testSendThrowsWithoutRecipient() { $transport = self::createTransport(); diff --git a/src/Symfony/Component/Notifier/Bridge/OrangeSms/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/OrangeSms/CHANGELOG.md index d03cb3eda36e3..4662c26671e03 100644 --- a/src/Symfony/Component/Notifier/Bridge/OrangeSms/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/OrangeSms/CHANGELOG.md @@ -1,11 +1,6 @@ CHANGELOG ========= -6.3 ---- - - * Add `OrangeSmsOptions` class - 6.2 --- diff --git a/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsOptions.php b/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsOptions.php deleted file mode 100644 index 772be4aaac76b..0000000000000 --- a/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsOptions.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Notifier\Bridge\OrangeSms; - -use Symfony\Component\Notifier\Message\MessageOptionsInterface; - -/** - * @author gnito-org - */ -final class OrangeSmsOptions implements MessageOptionsInterface -{ - private array $options; - - public function __construct(array $options = []) - { - $this->options = $options; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - - public function getRecipientId(): ?string - { - return $this->options['recipient_id'] ?? null; - } - - 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 toArray(): array - { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; - } -} diff --git a/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsTransport.php b/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsTransport.php index c10687b761e83..bb11f65400c9a 100644 --- a/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/OrangeSms/OrangeSmsTransport.php @@ -50,7 +50,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof OrangeSmsOptions); + return $message instanceof SmsMessage; } public function doSend(MessageInterface $message): SentMessage @@ -60,21 +60,11 @@ public function doSend(MessageInterface $message): SentMessage } $from = $message->getFrom() ?: $this->from; - - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; - $options['from'] = $options['from'] ?? $from; - - $url = 'https://'.$this->getEndpoint().'/smsmessaging/v1/outbound/'.urlencode('tel:'.$options['from']).'/requests'; - $headers = [ - 'Authorization' => 'Bearer '.$this->getAccessToken(), - 'Content-Type' => 'application/json', - ]; - + $url = 'https://'.$this->getEndpoint().'/smsmessaging/v1/outbound/'.urlencode('tel:'.$from).'/requests'; $payload = [ 'outboundSMSMessageRequest' => [ 'address' => 'tel:'.$message->getPhone(), - 'senderAddress' => 'tel:'.$options['from'], + 'senderAddress' => 'tel:'.$from, 'outboundSMSTextMessage' => [ 'message' => $message->getSubject(), ], @@ -86,7 +76,7 @@ public function doSend(MessageInterface $message): SentMessage } $response = $this->client->request('POST', $url, [ - 'headers' => $headers, + 'auth_bearer' => $this->getAccessToken(), 'json' => $payload, ]); diff --git a/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsOptionsTest.php deleted file mode 100644 index 717cbc29e9034..0000000000000 --- a/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsOptionsTest.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Notifier\Bridge\OrangeSms\Tests; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsOptions; - -class OrangeSmsOptionsTest extends TestCase -{ - public function testOrangeSmsOptions() - { - $orangeSmsOptions = (new OrangeSmsOptions())->setFrom('test_from')->setRecipientId('test_recipient'); - - self::assertSame([ - 'from' => 'test_from', - ], $orangeSmsOptions->toArray()); - } -} diff --git a/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsTransportTest.php b/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsTransportTest.php index 2a869218ba02d..62d6a9f247dd2 100644 --- a/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/OrangeSms/Tests/OrangeSmsTransportTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Notifier\Bridge\OrangeSms\Tests; use Symfony\Component\HttpClient\MockHttpClient; -use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsOptions; use Symfony\Component\Notifier\Bridge\OrangeSms\OrangeSmsTransport; use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\SmsMessage; @@ -35,7 +34,6 @@ public static function toStringProvider(): iterable public static function supportedMessagesProvider(): iterable { yield [new SmsMessage('+243899999999', 'Hello World!')]; - yield [new SmsMessage('+243899999999', 'Hello World!'), 'from', new OrangeSmsOptions(['from' => 'foo'])]; } public static function unsupportedMessagesProvider(): iterable diff --git a/src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransport.php b/src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransport.php index a2356c6b6277b..75b144ef4a373 100644 --- a/src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransport.php @@ -47,11 +47,7 @@ public function __construct(string $applicationKey, #[\SensitiveParameter] strin public function __toString(): string { - if (null !== $this->sender) { - return sprintf('ovhcloud://%s?service_name=%s&sender=%s', $this->getEndpoint(), $this->serviceName, $this->sender); - } - - return sprintf('ovhcloud://%s?service_name=%s', $this->getEndpoint(), $this->serviceName); + return sprintf('ovhcloud://%s?service_name=%s%s', $this->getEndpoint(), $this->serviceName, $this->sender ? '&sender='.$this->sender : ''); } /** diff --git a/src/Symfony/Component/Notifier/Bridge/PagerDuty/PagerDutyTransport.php b/src/Symfony/Component/Notifier/Bridge/PagerDuty/PagerDutyTransport.php index b01697d24c2c6..122993b28960c 100644 --- a/src/Symfony/Component/Notifier/Bridge/PagerDuty/PagerDutyTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/PagerDuty/PagerDutyTransport.php @@ -41,7 +41,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof PushMessage; + return $message instanceof PushMessage && (null === $message->getOptions() || $message->getOptions() instanceof PagerDutyOptions); } protected function doSend(MessageInterface $message = null): SentMessage @@ -50,20 +50,16 @@ protected function doSend(MessageInterface $message = null): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message); } - if (null !== $message->getOptions() && !($message->getOptions() instanceof PagerDutyOptions)) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, PagerDutyOptions::class)); - } - - $body = ($opts = $message->getOptions()) ? $opts->toArray() : []; - $body['payload']['summary'] = $message->getContent(); - $body['payload']['source'] = $message->getSubject(); + $options = $message->getOptions()?->toArray() ?? []; + $options['payload']['summary'] = $message->getContent(); + $options['payload']['source'] = $message->getSubject(); $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v2/enqueue', [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => $this->token, ], - 'json' => $body, + 'json' => $options, ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/Plivo/PlivoOptions.php b/src/Symfony/Component/Notifier/Bridge/Plivo/PlivoOptions.php index 9f9a3ee522ed0..b7c0ec41bbbf5 100644 --- a/src/Symfony/Component/Notifier/Bridge/Plivo/PlivoOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Plivo/PlivoOptions.php @@ -25,108 +25,75 @@ public function __construct(array $options = []) $this->options = $options; } - public function getLog(): ?bool - { - return $this->options['log'] ?? null; - } - - public function getMediaUrls(): ?string - { - return $this->options['media_urls'] ?? null; - } - - public function getMethod(): ?string - { - return $this->options['method'] ?? null; - } - - public function getPowerpackUuid(): ?string - { - return $this->options['powerpack_uuid'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; - } - - public function getSrc(): ?string - { - return $this->options['src'] ?? null; - } - - public function getTrackable(): ?bool - { - return $this->options['trackable'] ?? null; + return null; } - public function getType(): ?string - { - return $this->options['type'] ?? null; - } - - public function getUrl(): ?string - { - return $this->options['url'] ?? null; - } - - public function setLog(bool $log): self + /** + * @return $this + */ + public function log(bool $log): static { $this->options['log'] = $log; return $this; } - public function setMediaUrls(string $mediaUrls): self + /** + * @return $this + */ + public function mediaUrls(string $mediaUrls): static { $this->options['media_urls'] = $mediaUrls; return $this; } - public function setMethod(string $method): self + /** + * @return $this + */ + public function method(string $method): static { $this->options['method'] = $method; return $this; } - public function setPowerpackUuid(string $powerpackUuid): self + /** + * @return $this + */ + public function powerpackUuid(string $powerpackUuid): static { $this->options['powerpack_uuid'] = $powerpackUuid; return $this; } - public function setRecipientId(string $id): self - { - $this->options['recipient_id'] = $id; - - return $this; - } - - public function setSrc(string $src): self - { - $this->options['src'] = $src; - - return $this; - } - - public function setTrackable(bool $trackable): self + /** + * @return $this + */ + public function trackable(bool $trackable): static { $this->options['trackable'] = $trackable; return $this; } - public function setType(string $type): self + /** + * @return $this + */ + public function type(string $type): static { $this->options['type'] = $type; return $this; } - public function setUrl(string $url): self + /** + * @return $this + */ + public function url(string $url): static { $this->options['url'] = $url; @@ -135,11 +102,6 @@ public function setUrl(string $url): self public function toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/Plivo/PlivoTransport.php b/src/Symfony/Component/Notifier/Bridge/Plivo/PlivoTransport.php index bfc1ad23b9784..0f382ef23f0d1 100644 --- a/src/Symfony/Component/Notifier/Bridge/Plivo/PlivoTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Plivo/PlivoTransport.php @@ -56,18 +56,20 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['text'] = $message->getSubject(); - $options['src'] = $options['src'] ?? $this->from; - $options['dst'] = $options['dst'] ?? $message->getPhone(); + $options['src'] = $message->getFrom() ?: $this->from; + $options['dst'] = $message->getPhone(); if (!preg_match('/^[a-zA-Z0-9\s]{2,11}$/', $options['src']) && !preg_match('/^\+?[1-9]\d{1,14}$/', $options['src'])) { - throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID. Phone number must contain only numbers and optional + character.', $this->from)); + throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID. Phone number must contain only numbers and optional + character.', $options['src'])); } $endpoint = sprintf('https://%s/v1/Account/%s/Message/', $this->getEndpoint(), $this->authId); - $response = $this->client->request('POST', $endpoint, ['auth_basic' => $this->authId.':'.$this->authToken, 'json' => array_filter($options)]); + $response = $this->client->request('POST', $endpoint, [ + 'auth_basic' => [$this->authId, $this->authToken], + 'json' => array_filter($options), + ]); try { $statusCode = $response->getStatusCode(); diff --git a/src/Symfony/Component/Notifier/Bridge/Plivo/Tests/PlivoOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Plivo/Tests/PlivoOptionsTest.php index 1ecf549847b83..f8aeb239091a2 100644 --- a/src/Symfony/Component/Notifier/Bridge/Plivo/Tests/PlivoOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Plivo/Tests/PlivoOptionsTest.php @@ -18,11 +18,17 @@ class PlivoOptionsTest extends TestCase { public function testPlivoOptions() { - $plivoOptions = (new PlivoOptions())->setRecipientId('test_recipient')->setLog(true)->setSrc('test_src')->setMethod('test_method')->setUrl('test_url')->setMediaUrls('test_media_urls')->setPowerpackUuid('test_powerpack_uuid')->setTrackable(true)->setType('test_type'); + $plivoOptions = (new PlivoOptions()) + ->log(true) + ->method('test_method') + ->url('test_url') + ->mediaUrls('test_media_urls') + ->powerpackUuid('test_powerpack_uuid') + ->trackable(true) + ->type('test_type'); self::assertSame([ 'log' => true, - 'src' => 'test_src', 'method' => 'test_method', 'url' => 'test_url', 'media_urls' => 'test_media_urls', diff --git a/src/Symfony/Component/Notifier/Bridge/Pushover/PushoverOptions.php b/src/Symfony/Component/Notifier/Bridge/Pushover/PushoverOptions.php index 5733e389efe14..4d8f4be2411f5 100644 --- a/src/Symfony/Component/Notifier/Bridge/Pushover/PushoverOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Pushover/PushoverOptions.php @@ -103,7 +103,7 @@ public function device(string $device): static */ public function asHtml(bool $bool): static { - $this->options['html'] = $bool ? 1 : 0; + $this->options['html'] = (int) $bool; return $this; } diff --git a/src/Symfony/Component/Notifier/Bridge/Pushover/PushoverTransport.php b/src/Symfony/Component/Notifier/Bridge/Pushover/PushoverTransport.php index be7e6f75dbed0..cf96f7757ae70 100644 --- a/src/Symfony/Component/Notifier/Bridge/Pushover/PushoverTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Pushover/PushoverTransport.php @@ -39,7 +39,7 @@ public function __construct( public function supports(MessageInterface $message): bool { - return $message instanceof PushMessage; + return $message instanceof PushMessage && (null === $message->getOptions() || $message->getOptions() instanceof PushoverOptions); } public function __toString(): string @@ -53,14 +53,13 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message); } - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['message'] = $message->getContent(); $options['title'] = $message->getSubject(); $options['token'] = $this->appToken; $options['user'] = $this->userKey; - $endpoint = sprintf('https://%s/1/messages.json', self::HOST); + $endpoint = sprintf('https://%s/1/messages.json', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'body' => $options, ]); diff --git a/src/Symfony/Component/Notifier/Bridge/Pushover/Tests/PushoverTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Pushover/Tests/PushoverTransportTest.php index 9b2af410122c8..dbe24dfaffbf9 100644 --- a/src/Symfony/Component/Notifier/Bridge/Pushover/Tests/PushoverTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Pushover/Tests/PushoverTransportTest.php @@ -64,7 +64,7 @@ public function testSendWithOptions() 'title' => 'testMessageSubject', 'token' => 'appToken', 'user' => 'userKey', - ]); + ], '', '&'); $client = new MockHttpClient(function (string $method, string $url, array $options = []) use ( $response, diff --git a/src/Symfony/Component/Notifier/Bridge/RingCentral/RingCentralOptions.php b/src/Symfony/Component/Notifier/Bridge/RingCentral/RingCentralOptions.php index 25f61560685b0..c3d3c6fff9476 100644 --- a/src/Symfony/Component/Notifier/Bridge/RingCentral/RingCentralOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/RingCentral/RingCentralOptions.php @@ -25,97 +25,29 @@ public function __construct(array $options = []) $this->options = $options; } - public function getCountryCallingCode(): ?string - { - return $this->options['country_calling_code'] ?? null; - } - - public function getCountryId(): ?string - { - return $this->options['country_id'] ?? null; - } - - public function getCountryIsoCode(): ?string - { - return $this->options['country_iso_code'] ?? null; - } - - public function getCountryName(): ?string - { - return $this->options['country_name'] ?? null; - } - - public function getCountryUri(): ?string - { - return $this->options['country_uri'] ?? null; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; - } - - public function setCountryCallingCode(string $countryCallingCode): self - { - $this->options['country_calling_code'] = $countryCallingCode; - - return $this; + return null; } - public function setCountryId(string $countryId): self + /** + * @return $this + */ + public function country(string $id, string $isoCode = null, string $name = null, string $uri = null, string $callingCode = null): static { - $this->options['country_id'] = $countryId; - - return $this; - } - - public function setCountryIsoCode(string $countryIsoCode): self - { - $this->options['country_iso_code'] = $countryIsoCode; - - return $this; - } - - public function setCountryName(string $countryName): self - { - $this->options['country_name'] = $countryName; - - return $this; - } - - public function setCountryUri(string $countryUri): self - { - $this->options['country_uri'] = $countryUri; - - 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; + $this->options['country'] = [ + 'id' => $id, + 'isoCode' => $isoCode, + 'name' => $name, + 'uri' => $uri, + 'callingCode' => $callingCode, + ]; return $this; } public function toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/RingCentral/RingCentralTransport.php b/src/Symfony/Component/Notifier/Bridge/RingCentral/RingCentralTransport.php index 934c11810224c..5148d0074803f 100644 --- a/src/Symfony/Component/Notifier/Bridge/RingCentral/RingCentralTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/RingCentral/RingCentralTransport.php @@ -40,7 +40,7 @@ public function __construct( public function __toString(): string { - return sprintf('ringcentral://%s?from=%s', $this->getEndpoint(), $this->from); + return sprintf('ringcentral://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : ''); } public function supports(MessageInterface $message): bool @@ -54,27 +54,20 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['text'] = $message->getSubject(); - $options['from']['phoneNumber'] = $options['from'] ?? $this->from; + $options['from']['phoneNumber'] = $message->getFrom() ?: $this->from; $options['to'][]['phoneNumber'] = $message->getPhone(); - if (isset($options['country_id'])) { - $options['country']['id'] = $options['country_id'] ?? null; - $options['country']['uri'] = $options['country_uri'] ?? null; - $options['country']['name'] = $options['country_name'] ?? null; - $options['country']['isoCode'] = $options['country_iso_code'] ?? null; - $options['country']['callingCode'] = $options['country_calling_code'] ?? null; - unset($options['country_id'], $options['country_uri'], $options['country_name'], $options['country_iso_code'], $options['country_calling_code']); - } - - if (!preg_match('/^\+[1-9]\d{1,14}$/', $options['from']['phoneNumber'])) { - throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number. Phone number must be in E.164 format.', $this->from)); + if (!preg_match('/^\+[1-9]\d{1,14}$/', $options['from']['phoneNumber'] ?? '')) { + throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number. Phone number must be in E.164 format.', $options['from']['phoneNumber'] ?? '')); } $endpoint = sprintf('https://%s/restapi/v1.0/account/~/extension/~/sms', $this->getEndpoint()); - $response = $this->client->request('POST', $endpoint, ['auth_bearer' => $this->apiToken, 'json' => array_filter($options)]); + $response = $this->client->request('POST', $endpoint, [ + 'auth_bearer' => $this->apiToken, + 'json' => array_filter($options), + ]); try { $statusCode = $response->getStatusCode(); diff --git a/src/Symfony/Component/Notifier/Bridge/RingCentral/Tests/RingCentralOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/RingCentral/Tests/RingCentralOptionsTest.php index 69f0fdee87555..a932c7f33e2e0 100644 --- a/src/Symfony/Component/Notifier/Bridge/RingCentral/Tests/RingCentralOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/RingCentral/Tests/RingCentralOptionsTest.php @@ -18,15 +18,17 @@ class RingCentralOptionsTest extends TestCase { public function testRingCentralOptions() { - $ringCentralOptions = (new RingCentralOptions())->setFrom('test_from')->setRecipientId('test_recipient')->setCountryId('test_country_id')->setCountryName('test_country_name')->setCountryUri('test_country_uri')->setCountryCallingCode('test_country_calling_code')->setCountryIsoCode('test_country_iso_code'); + $ringCentralOptions = (new RingCentralOptions()) + ->country('test_country_id', 'test_country_iso_code', 'test_country_name', 'test_country_uri', 'test_country_calling_code'); self::assertSame([ - 'from' => 'test_from', - 'country_id' => 'test_country_id', - 'country_name' => 'test_country_name', - 'country_uri' => 'test_country_uri', - 'country_calling_code' => 'test_country_calling_code', - 'country_iso_code' => 'test_country_iso_code', + 'country' => [ + 'id' => 'test_country_id', + 'isoCode' => 'test_country_iso_code', + 'name' => 'test_country_name', + 'uri' => 'test_country_uri', + 'callingCode' => 'test_country_calling_code', + ], ], $ringCentralOptions->toArray()); } } diff --git a/src/Symfony/Component/Notifier/Bridge/RocketChat/RocketChatTransport.php b/src/Symfony/Component/Notifier/Bridge/RocketChat/RocketChatTransport.php index e1cfd92bab0a3..54d2b8031d88f 100644 --- a/src/Symfony/Component/Notifier/Bridge/RocketChat/RocketChatTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/RocketChat/RocketChatTransport.php @@ -43,11 +43,7 @@ public function __construct(#[\SensitiveParameter] string $accessToken, string $ public function __toString(): string { - if (null === $this->chatChannel) { - return sprintf('rocketchat://%s', $this->getEndpoint()); - } - - return sprintf('rocketchat://%s?channel=%s', $this->getEndpoint(), $this->chatChannel); + return sprintf('rocketchat://%s%s', $this->getEndpoint(), null !== $this->chatChannel ? '?channel='.$this->chatChannel : ''); } public function supports(MessageInterface $message): bool @@ -63,23 +59,15 @@ protected function doSend(MessageInterface $message): SentMessage if (!$message instanceof ChatMessage) { throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - if ($message->getOptions() && !$message->getOptions() instanceof RocketChatOptions) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, RocketChatOptions::class)); - } - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; - if (!isset($options['channel'])) { - $options['channel'] = $message->getRecipientId() ?: $this->chatChannel; - } + $options = $message->getOptions()?->toArray() ?? []; + $options['channel'] ??= $message->getRecipientId() ?: $this->chatChannel; $options['text'] = $message->getSubject(); - $response = $this->client->request( - 'POST', - sprintf('https://%s/hooks/%s', $this->getEndpoint(), $this->accessToken), - [ - 'json' => array_filter($options), - ] - ); + $endpoint = sprintf('https://%s/hooks/%s', $this->getEndpoint(), $this->accessToken); + $response = $this->client->request('POST', $endpoint, [ + 'json' => array_filter($options), + ]); try { $statusCode = $response->getStatusCode(); diff --git a/src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransport.php b/src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransport.php index dae546ba55eee..2a83177eff98b 100644 --- a/src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransport.php @@ -55,11 +55,9 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $sender = $message->getFrom() ?: $this->sender; - $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v3/transactionalSMS/sms', [ 'json' => [ - 'sender' => $sender, + 'sender' => $message->getFrom() ?: $this->sender, 'recipient' => $message->getPhone(), 'content' => $message->getSubject(), ], diff --git a/src/Symfony/Component/Notifier/Bridge/SimpleTextin/SimpleTextinOptions.php b/src/Symfony/Component/Notifier/Bridge/SimpleTextin/SimpleTextinOptions.php deleted file mode 100644 index aed250801c0e8..0000000000000 --- a/src/Symfony/Component/Notifier/Bridge/SimpleTextin/SimpleTextinOptions.php +++ /dev/null @@ -1,59 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Notifier\Bridge\SimpleTextin; - -use Symfony\Component\Notifier\Message\MessageOptionsInterface; - -/** - * @author gnito-org - */ -final class SimpleTextinOptions implements MessageOptionsInterface -{ - private array $options; - - public function __construct(array $options = []) - { - $this->options = $options; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - - public function getRecipientId(): ?string - { - return $this->options['recipient_id'] ?? null; - } - - 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 toArray(): array - { - $options = $this->options; - unset($options['recipient_id']); - - return $options; - } -} diff --git a/src/Symfony/Component/Notifier/Bridge/SimpleTextin/SimpleTextinTransport.php b/src/Symfony/Component/Notifier/Bridge/SimpleTextin/SimpleTextinTransport.php index 670be78836ea1..3d6c48a5fac5b 100644 --- a/src/Symfony/Component/Notifier/Bridge/SimpleTextin/SimpleTextinTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/SimpleTextin/SimpleTextinTransport.php @@ -40,17 +40,12 @@ public function __construct( public function __toString(): string { - $queryParameters = []; - if ($this->from) { - $queryParameters['from'] = $this->from; - } - - return sprintf('simpletextin://%s', $this->getEndpoint()).($queryParameters ? '?'.http_build_query($queryParameters) : ''); + return sprintf('simpletextin://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : ''); } public function supports(MessageInterface $message): bool { - return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof SimpleTextinOptions); + return $message instanceof SmsMessage; } /** @@ -63,23 +58,14 @@ protected function doSend(MessageInterface $message): SentMessage } $endpoint = sprintf('https://%s/v2/api/messages', $this->getEndpoint()); - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; + $options = []; $options['text'] = $message->getSubject(); $options['contactPhone'] = $message->getPhone(); $options['mode'] = 'AUTO'; + $options['accountPhone'] = $message->getFrom() ?: $this->from; - if (!isset($options['from']) && $this->from) { - $options['from'] = $this->from; - } - - if (isset($options['from']) && !preg_match('/^\+?[1-9]\d{1,14}$/', $options['from'])) { - throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number.', $this->from)); - } - - if ($options['from'] ?? false) { - $options['accountPhone'] = $options['from']; - unset($options['from']); + if (null !== $options['accountPhone'] && !preg_match('/^\+?[1-9]\d{1,14}$/', $options['accountPhone'])) { + throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number.', $options['accountPhone'])); } $response = $this->client->request('POST', $endpoint, [ diff --git a/src/Symfony/Component/Notifier/Bridge/SimpleTextin/Tests/SimpleTextinOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/SimpleTextin/Tests/SimpleTextinOptionsTest.php deleted file mode 100644 index 215f13a584961..0000000000000 --- a/src/Symfony/Component/Notifier/Bridge/SimpleTextin/Tests/SimpleTextinOptionsTest.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Notifier\Bridge\SimpleTextin\Tests; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Notifier\Bridge\SimpleTextin\SimpleTextinOptions; - -class SimpleTextinOptionsTest extends TestCase -{ - public function testSimpleTextinOptions() - { - $simpleTextinOptions = (new SimpleTextinOptions())->setFrom('test_from')->setRecipientId('test_recipient'); - - self::assertSame(['from' => 'test_from'], $simpleTextinOptions->toArray()); - } -} diff --git a/src/Symfony/Component/Notifier/Bridge/SimpleTextin/Tests/SimpleTextinTransportTest.php b/src/Symfony/Component/Notifier/Bridge/SimpleTextin/Tests/SimpleTextinTransportTest.php index b4dc58946e3b9..642231c90260d 100644 --- a/src/Symfony/Component/Notifier/Bridge/SimpleTextin/Tests/SimpleTextinTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/SimpleTextin/Tests/SimpleTextinTransportTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Notifier\Bridge\SimpleTextin\Tests; use Symfony\Component\HttpClient\MockHttpClient; -use Symfony\Component\Notifier\Bridge\SimpleTextin\SimpleTextinOptions; use Symfony\Component\Notifier\Bridge\SimpleTextin\SimpleTextinTransport; use Symfony\Component\Notifier\Exception\InvalidArgumentException; use Symfony\Component\Notifier\Message\ChatMessage; @@ -38,7 +37,6 @@ public function invalidFromProvider(): iterable public static function supportedMessagesProvider(): iterable { yield [new SmsMessage('0611223344', 'Hello!')]; - yield [new SmsMessage('0611223344', 'Hello!', 'from', new SimpleTextinOptions(['from' => 'from_new']))]; } /** diff --git a/src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransport.php b/src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransport.php index 0d14770910d47..6626294d21cfe 100644 --- a/src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransport.php @@ -57,13 +57,11 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $endpoint = sprintf('https://%s/xms/v1/%s/batches', $this->getEndpoint(), $this->accountSid); $response = $this->client->request('POST', $endpoint, [ 'auth_bearer' => $this->authToken, 'json' => [ - 'from' => $from, + 'from' => $message->getFrom() ?: $this->from, 'to' => [$message->getPhone()], 'body' => $message->getSubject(), ], diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php b/src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php index 1f30d8a0fd74f..fe7a3ecf31bd6 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php @@ -47,11 +47,11 @@ public function __construct(#[\SensitiveParameter] string $accessToken, string $ public function __toString(): string { - if (null === $this->chatChannel) { - return sprintf('slack://%s', $this->getEndpoint()); - } + $query = array_filter([ + 'channel' => $this->chatChannel, + ]); - return sprintf('slack://%s?channel=%s', $this->getEndpoint(), urlencode($this->chatChannel)); + return sprintf('slack://%s%s', $this->getEndpoint(), $query ? '?'.http_build_query($query, '', '&') : ''); } public function supports(MessageInterface $message): bool @@ -68,21 +68,15 @@ protected function doSend(MessageInterface $message): SlackSentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - if ($message->getOptions() && !$message->getOptions() instanceof SlackOptions) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, SlackOptions::class)); + if (!($options = $message->getOptions()) && $notification = $message->getNotification()) { + $options = SlackOptions::fromNotification($notification); } - if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) { - $opts = SlackOptions::fromNotification($notification); - } - - $options = $opts ? $opts->toArray() : []; - if (!isset($options['channel'])) { - $options['channel'] = $message->getRecipientId() ?: $this->chatChannel; - } + $options = $options?->toArray() ?? []; + $options['channel'] ??= $message->getRecipientId() ?: $this->chatChannel; $options['text'] = $message->getSubject(); - $apiMethod = $opts instanceof UpdateMessageSlackOptions ? 'chat.update' : 'chat.postMessage'; + $apiMethod = $message->getOptions() instanceof UpdateMessageSlackOptions ? 'chat.update' : 'chat.postMessage'; $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/'.$apiMethod, [ 'json' => array_filter($options), 'auth_bearer' => $this->accessToken, diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportTest.php index 2f34cfaca7420..8619b85ff3e30 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportTest.php @@ -16,10 +16,8 @@ use Symfony\Component\Notifier\Bridge\Slack\SlackSentMessage; use Symfony\Component\Notifier\Bridge\Slack\SlackTransport; use Symfony\Component\Notifier\Exception\InvalidArgumentException; -use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Exception\TransportException; use Symfony\Component\Notifier\Message\ChatMessage; -use Symfony\Component\Notifier\Message\MessageOptionsInterface; use Symfony\Component\Notifier\Message\SmsMessage; use Symfony\Component\Notifier\Notification\Notification; use Symfony\Component\Notifier\Test\TransportTestCase; @@ -169,17 +167,6 @@ public function testSendWithNotification() $this->assertSame('1503435956.000247', $sentMessage->getMessageId()); } - public function testSendWithInvalidOptions() - { - $this->expectException(LogicException::class); - - $client = new MockHttpClient(fn (string $method, string $url, array $options = []): ResponseInterface => $this->createMock(ResponseInterface::class)); - - $transport = self::createTransport($client, 'testChannel'); - - $transport->send(new ChatMessage('testMessage', $this->createMock(MessageOptionsInterface::class))); - } - public function testSendWith200ResponseButNotOk() { $channel = 'testChannel'; diff --git a/src/Symfony/Component/Notifier/Bridge/Sms77/Sms77Transport.php b/src/Symfony/Component/Notifier/Bridge/Sms77/Sms77Transport.php index 2522a211994e2..6916daaebb33b 100644 --- a/src/Symfony/Component/Notifier/Bridge/Sms77/Sms77Transport.php +++ b/src/Symfony/Component/Notifier/Bridge/Sms77/Sms77Transport.php @@ -41,11 +41,7 @@ public function __construct(#[\SensitiveParameter] string $apiKey, string $from public function __toString(): string { - if (null === $this->from) { - return sprintf('sms77://%s', $this->getEndpoint()); - } - - return sprintf('sms77://%s?from=%s', $this->getEndpoint(), $this->from); + return sprintf('sms77://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : ''); } public function supports(MessageInterface $message): bool @@ -59,8 +55,6 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $endpoint = sprintf('https://%s/api/sms', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'headers' => [ @@ -69,7 +63,7 @@ protected function doSend(MessageInterface $message): SentMessage 'X-Api-Key' => $this->apiKey, ], 'json' => [ - 'from' => $from, + 'from' => $message->getFrom() ?: $this->from, 'json' => 1, 'text' => $message->getSubject(), 'to' => $message->getPhone(), diff --git a/src/Symfony/Component/Notifier/Bridge/SmsBiuras/SmsBiurasTransport.php b/src/Symfony/Component/Notifier/Bridge/SmsBiuras/SmsBiurasTransport.php index bc6257422fe50..b68cb2897f4c1 100644 --- a/src/Symfony/Component/Notifier/Bridge/SmsBiuras/SmsBiurasTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/SmsBiuras/SmsBiurasTransport.php @@ -59,11 +59,7 @@ public function __construct(string $uid, #[\SensitiveParameter] string $apiKey, public function __toString(): string { - if ($this->testMode) { - return sprintf('smsbiuras://%s?from=%s&test_mode=%s', $this->getEndpoint(), $this->from, $this->testMode); - } - - return sprintf('smsbiuras://%s?from=%s', $this->getEndpoint(), $this->from); + return sprintf('smsbiuras://%s?from=%s%s', $this->getEndpoint(), $this->from, $this->testMode ? '&test_mode=1' : ''); } public function supports(MessageInterface $message): bool @@ -77,8 +73,6 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $endpoint = sprintf('https://%s/api?', $this->getEndpoint()); $response = $this->client->request('GET', $endpoint, [ @@ -86,8 +80,8 @@ protected function doSend(MessageInterface $message): SentMessage 'uid' => $this->uid, 'apikey' => $this->apiKey, 'message' => $message->getSubject(), - 'from' => $from, - 'test' => $this->testMode ? 1 : 0, + 'from' => $message->getFrom() ?: $this->from, + 'test' => (int) $this->testMode, 'to' => $message->getPhone(), ], ]); diff --git a/src/Symfony/Component/Notifier/Bridge/SmsFactor/SmsFactorTransport.php b/src/Symfony/Component/Notifier/Bridge/SmsFactor/SmsFactorTransport.php index ffef8929c93da..5f446631af649 100644 --- a/src/Symfony/Component/Notifier/Bridge/SmsFactor/SmsFactorTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/SmsFactor/SmsFactorTransport.php @@ -43,16 +43,12 @@ public function __construct(#[\SensitiveParameter] string $tokenApi, ?string $se public function __toString(): string { - $arguments = []; - if (null !== $this->sender) { - $arguments[] = sprintf('sender=%s', $this->sender); - } - - if (null !== $this->pushType) { - $arguments[] = sprintf('push_type=%s', $this->pushType->value); - } + $query = array_filter([ + 'sender' => $this->sender, + 'push_type' => $this->pushType?->value, + ]); - return sprintf('sms-factor://%s?%s', $this->getEndpoint(), implode('&', $arguments)); + return sprintf('sms-factor://%s%s', $this->getEndpoint(), $query ? '?'.http_build_query($query, '', '&') : ''); } public function supports(MessageInterface $message): bool @@ -71,22 +67,14 @@ protected function doSend(MessageInterface $message): SentMessage 'to' => $message->getPhone(), 'text' => $message->getSubject(), 'gsmsmsid' => $messageId, + 'sender' => $message->getFrom() ?: $this->sender, + 'pushtype' => $this->pushType?->value, ]; - if ('' !== $message->getFrom()) { - $query['sender'] = $message->getFrom(); - } elseif (null !== $this->sender) { - $query['sender'] = $this->sender; - } - - if (null !== $this->pushType) { - $query['pushtype'] = $this->pushType->value; - } - $response = $this->client->request('GET', 'https://'.$this->getEndpoint().'/send', [ - 'query' => $query, + 'query' => array_filter($query), + 'auth_bearer' => $this->tokenApi, 'headers' => [ - 'Authorization' => sprintf('Bearer %s', $this->tokenApi), 'Accept' => 'application/json', ], ]); diff --git a/src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransport.php b/src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransport.php index b596b5b46638e..16d10c208f390 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransport.php @@ -64,28 +64,13 @@ public function setTest(bool $test): static public function __toString(): string { - $dsn = sprintf('smsapi://%s', $this->getEndpoint()); - $params = []; - - if ('' !== $this->from) { - $params['from'] = $this->from; - } - - if ($this->fast) { - $params['fast'] = (int) $this->fast; - } - - if ($this->test) { - $params['test'] = (int) $this->test; - } - - $query = http_build_query($params, '', '&'); - - if ('' !== $query) { - $dsn .= sprintf('?%s', $query); - } + $query = array_filter([ + 'from' => $this->from, + 'fast' => (int) $this->fast, + 'test' => (int) $this->test, + ]); - return $dsn; + return sprintf('smsapi://%s%s', $this->getEndpoint(), $query ? '?'.http_build_query($query, '', '&') : ''); } public function supports(MessageInterface $message): bool @@ -109,10 +94,7 @@ protected function doSend(MessageInterface $message): SentMessage 'test' => $this->test, ]; - $from = $message->getFrom() ?: $this->from; - - // if from is not empty add it to request body - if ('' !== $from) { + if ('' !== $from = $message->getFrom() ?: $this->from) { $body['from'] = $from; } diff --git a/src/Symfony/Component/Notifier/Bridge/Smsc/SmscTransport.php b/src/Symfony/Component/Notifier/Bridge/Smsc/SmscTransport.php index b2edb6a5ff1d0..2ae998cd4164d 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsc/SmscTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsc/SmscTransport.php @@ -59,12 +59,10 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $body = [ 'login' => $this->login, 'psw' => $this->password, - 'sender' => $from, + 'sender' => $message->getFrom() ?: $this->from, 'phones' => $message->getPhone(), 'mes' => $message->getSubject(), 'fmt' => 3, // response as JSON diff --git a/src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeOptions.php b/src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeOptions.php index 519878026b37a..40d1312298ffa 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeOptions.php @@ -25,61 +25,33 @@ public function __construct(array $options = []) $this->options = $options; } - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; - } - - public function getRefClient(): ?string - { - return $this->options['ref_client'] ?? null; - } - - public function getSentDate(): ?string - { - return $this->options['sent_date'] ?? null; - } - - 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; + return null; } - public function setRefClient(string $refClient): self + /** + * @return $this + */ + public function refClient(string $refClient): static { - $this->options['ref_client'] = $refClient; + $this->options['refClient'] = $refClient; return $this; } - public function setSentDate(string $sentDate): self + /** + * @return $this + */ + public function sentDate(string $sentDate): static { - $this->options['sent_date'] = $sentDate; + $this->options['sentDate'] = $sentDate; return $this; } public function toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeTransport.php b/src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeTransport.php index d3d20d94ab77c..fcca45f4886cc 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeTransport.php @@ -40,12 +40,7 @@ public function __construct( public function __toString(): string { - $queryParameters = []; - if ($this->from) { - $queryParameters['from'] = $this->from; - } - - return sprintf('smsmode://%s', $this->getEndpoint()).($queryParameters ? '?'.http_build_query($queryParameters) : null); + return sprintf('smsmode://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : ''); } public function supports(MessageInterface $message): bool @@ -64,32 +59,18 @@ protected function doSend(MessageInterface $message): SentMessage $endpoint = sprintf('https://%s/sms/v1/messages', $this->getEndpoint()); - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['body']['text'] = $message->getSubject(); $options['recipient']['to'] = $message->getPhone(); - - if (!isset($options['from'])) { - $options['from'] = $this->from; - } + $options['from'] = $message->getFrom() ?: $this->from; if (!preg_match('/^[a-zA-Z0-9\s]{1,11}$/', $options['from'] ?? '')) { - throw new InvalidArgumentException(sprintf('The "From" value "%s" is not a valid sender ID.', $this->from)); - } - - if (isset($options['sent_date'])) { - $options['sentDate'] = $options['sent_date']; - unset($options['sent_date']); - } - - if (isset($options['ref_client'])) { - $options['refClient'] = $options['ref_client']; - unset($options['ref_client']); + throw new InvalidArgumentException(sprintf('The "From" value "%s" is not a valid sender ID.', $options['from'])); } $response = $this->client->request('POST', $endpoint, [ - 'headers' => ['X-Api-Key' => $this->apiKey], - 'json' => array_filter($options), + 'headers' => ['X-Api-Key' => $this->apiKey], + 'json' => array_filter($options), ]); try { diff --git a/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeOptionsTest.php index 93d1ad9d03c9f..1640a0069255b 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeOptionsTest.php @@ -18,12 +18,13 @@ class SmsmodeOptionsTest extends TestCase { public function testSmsmodeOptions() { - $smsmodeOptions = (new SmsmodeOptions())->setFrom('test_from')->setRecipientId('test_recipient')->setRefClient('test_ref_client')->setSentDate('test_sent_date'); + $smsmodeOptions = (new SmsmodeOptions()) + ->refClient('test_ref_client') + ->sentDate('test_sent_date'); self::assertSame([ - 'from' => 'test_from', - 'ref_client' => 'test_ref_client', - 'sent_date' => 'test_sent_date', + 'refClient' => 'test_ref_client', + 'sentDate' => 'test_sent_date', ], $smsmodeOptions->toArray()); } } diff --git a/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeTransportFactoryTest.php index b5ce68db1ab0d..4e8aee66e09fc 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeTransportFactoryTest.php @@ -21,28 +21,28 @@ public function createFactory(): SmsmodeTransportFactory return new SmsmodeTransportFactory(); } - public function createProvider(): iterable + public static function createProvider(): iterable { yield ['smsmode://host.test?from=test', 'smsmode://ApiKey@host.test?from=test']; } - public function incompleteDsnProvider(): iterable + public static function incompleteDsnProvider(): iterable { yield 'missing API key' => ['smsmode://@default?from=test']; } - public function missingRequiredOptionProvider(): iterable + public static function missingRequiredOptionProvider(): iterable { yield 'missing option: from' => ['smsmode://apiKey@default']; } - public function supportsProvider(): iterable + public static function supportsProvider(): iterable { yield [true, 'smsmode://apiKey@default?from=test']; yield [false, 'somethingElse://apiKey@default?from=test']; } - public function unsupportedSchemeProvider(): iterable + public static function unsupportedSchemeProvider(): iterable { yield ['somethingElse://apiKey@default?from=test']; } diff --git a/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeTransportTest.php index 2fb605b84df89..2cb4bf3a38820 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeTransportTest.php @@ -19,22 +19,23 @@ use Symfony\Component\Notifier\Message\MessageInterface; use Symfony\Component\Notifier\Message\SmsMessage; use Symfony\Component\Notifier\Test\TransportTestCase; +use Symfony\Component\Notifier\Tests\Transport\DummyMessage; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; final class SmsmodeTransportTest extends TransportTestCase { - public function createTransport(HttpClientInterface $client = null, string $from = 'test_from'): SmsmodeTransport + public static function createTransport(HttpClientInterface $client = null, string $from = 'test_from'): SmsmodeTransport { - return new SmsmodeTransport('test_api_key', $from, $client ?? $this->createMock(HttpClientInterface::class)); + return new SmsmodeTransport('test_api_key', $from, $client ?? new MockHttpClient()); } - public function invalidFromProvider(): iterable + public static function invalidFromProvider(): iterable { yield 'sender number too send' => ['aaaaaaaaaaaa']; } - public function supportedMessagesProvider(): iterable + public static function supportedMessagesProvider(): iterable { yield [new SmsMessage('0611223344', 'Hello!')]; yield [new SmsMessage('0611223344', 'Hello!', 'from', new SmsmodeOptions(['ref_client' => 'test_ref_client']))]; @@ -80,18 +81,18 @@ public function testNoInvalidArgumentExceptionIsThrownIfFromIsValid(string $from self::assertSame('foo', $sentMessage->getMessageId()); } - public function toStringProvider(): iterable + public static function toStringProvider(): iterable { - yield ['smsmode://rest.smsmode.com?from=test_from', $this->createTransport()]; + yield ['smsmode://rest.smsmode.com?from=test_from', self::createTransport()]; } - public function unsupportedMessagesProvider(): iterable + public static function unsupportedMessagesProvider(): iterable { yield [new ChatMessage('Hello!')]; - yield [$this->createMock(MessageInterface::class)]; + yield [new DummyMessage()]; } - public function validFromProvider(): iterable + public static function validFromProvider(): iterable { yield ['a']; yield ['ab']; diff --git a/src/Symfony/Component/Notifier/Bridge/SpotHit/SpotHitTransport.php b/src/Symfony/Component/Notifier/Bridge/SpotHit/SpotHitTransport.php index 696d01213037a..1f3434974cb43 100644 --- a/src/Symfony/Component/Notifier/Bridge/SpotHit/SpotHitTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/SpotHit/SpotHitTransport.php @@ -46,11 +46,7 @@ public function __construct(#[\SensitiveParameter] string $token, string $from = public function __toString(): string { - if (!$this->from) { - return sprintf('spothit://%s', $this->getEndpoint()); - } - - return sprintf('spothit://%s?from=%s', $this->getEndpoint(), $this->from); + return sprintf('spothit://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : ''); } public function supports(MessageInterface $message): bool @@ -72,8 +68,6 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $endpoint = sprintf('https://www.%s/api/envoyer/sms', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ 'body' => [ @@ -81,7 +75,7 @@ protected function doSend(MessageInterface $message): SentMessage 'destinataires' => $message->getPhone(), 'type' => 'premium', 'message' => $message->getSubject(), - 'expediteur' => $from, + 'expediteur' => $message->getFrom() ?: $this->from, ], ]); diff --git a/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php b/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php index ad8763371d5ad..9bb25bee5f587 100644 --- a/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php @@ -56,7 +56,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof ChatMessage; + return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof TelegramOptions); } /** @@ -68,15 +68,8 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - if ($message->getOptions() && !$message->getOptions() instanceof TelegramOptions) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, TelegramOptions::class)); - } - - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; - if (!isset($options['chat_id'])) { - $options['chat_id'] = $message->getRecipientId() ?: $this->chatChannel; - } - + $options = $message->getOptions()?->toArray() ?? []; + $options['chat_id'] ??= $message->getRecipientId() ?: $this->chatChannel; $options['text'] = $message->getSubject(); if (!isset($options['parse_mode']) || TelegramOptions::PARSE_MODE_MARKDOWN_V2 === $options['parse_mode']) { diff --git a/src/Symfony/Component/Notifier/Bridge/Termii/TermiiOptions.php b/src/Symfony/Component/Notifier/Bridge/Termii/TermiiOptions.php index 728c0eb795303..929da85d331c1 100644 --- a/src/Symfony/Component/Notifier/Bridge/Termii/TermiiOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Termii/TermiiOptions.php @@ -25,72 +25,38 @@ public function __construct(array $options = []) $this->options = $options; } - public function getChannel(): ?string - { - return $this->options['channel'] ?? null; - } - - public function getFrom(): ?string - { - return $this->options['from'] ?? null; - } - - public function getMediaCaption(): ?string - { - return $this->options['media_caption'] ?? null; - } - - public function getMediaUrl(): ?string - { - return $this->options['media_url'] ?? null; - } - public function getRecipientId(): ?string { - return $this->options['recipient_id'] ?? null; + return null; } - public function getType(): ?string - { - return $this->options['type'] ?? null; - } - - public function setChannel(string $channel): self + /** + * @return $this + */ + public function channel(string $channel): static { $this->options['channel'] = $channel; return $this; } - public function setFrom(string $from): self + /** + * @return $this + */ + public function media(string $url, string $caption = ''): static { - $this->options['from'] = $from; + $this->options['media'] = [ + 'url' => $url, + 'caption' => $caption, + ]; return $this; } - public function setMediaCaption(string $mediaCaption): self - { - $this->options['media_caption'] = $mediaCaption; - - return $this; - } - - public function setMediaUrl(string $mediaUrl): self - { - $this->options['media_url'] = $mediaUrl; - - return $this; - } - - public function setRecipientId(string $id): self - { - $this->options['recipient_id'] = $id; - - return $this; - } - - public function setType(string $type): self + /** + * @return $this + */ + public function type(string $type): static { $this->options['type'] = $type; @@ -99,11 +65,6 @@ public function setType(string $type): self public function toArray(): array { - $options = $this->options; - if (isset($options['recipient_id'])) { - unset($options['recipient_id']); - } - - return $options; + return $this->options; } } diff --git a/src/Symfony/Component/Notifier/Bridge/Termii/TermiiTransport.php b/src/Symfony/Component/Notifier/Bridge/Termii/TermiiTransport.php index db1f0284f1ef5..753d280421623 100644 --- a/src/Symfony/Component/Notifier/Bridge/Termii/TermiiTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Termii/TermiiTransport.php @@ -56,27 +56,22 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $opts = $message->getOptions(); - $options = $opts ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['api_key'] = $this->apiKey; $options['sms'] = $message->getSubject(); - $options['from'] = $options['from'] ?? $this->from; + $options['from'] = $message->getFrom() ?: $this->from; $options['to'] = $message->getPhone(); - $options['channel'] = $options['channel'] ?? $this->channel; - $options['type'] = $options['type'] ?? 'plain'; - - if (isset($options['media_url'])) { - $options['media']['url'] = $options['media_url'] ?? null; - $options['media']['caption'] = $options['media_caption'] ?? null; - unset($options['media_url'], $options['media_caption']); - } + $options['channel'] ??= $this->channel; + $options['type'] ??= 'plain'; if (!preg_match('/^[a-zA-Z0-9\s]{3,11}$/', $options['from']) && !preg_match('/^\+?[1-9]\d{1,14}$/', $options['from'])) { - throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $this->from)); + throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $options['from'])); } $endpoint = sprintf('https://%s/api/sms/send', $this->getEndpoint()); - $response = $this->client->request('POST', $endpoint, ['json' => array_filter($options)]); + $response = $this->client->request('POST', $endpoint, [ + 'json' => array_filter($options), + ]); try { $statusCode = $response->getStatusCode(); diff --git a/src/Symfony/Component/Notifier/Bridge/Termii/Tests/TermiiOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Termii/Tests/TermiiOptionsTest.php index 6ad2508e34526..01dc55a80dba2 100644 --- a/src/Symfony/Component/Notifier/Bridge/Termii/Tests/TermiiOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Termii/Tests/TermiiOptionsTest.php @@ -18,14 +18,18 @@ class TermiiOptionsTest extends TestCase { public function testTermiiOptions() { - $termiiOptions = (new TermiiOptions())->setFrom('test_from')->setRecipientId('test_recipient')->setType('test_type')->setChannel('test_channel')->setMediaCaption('test_media_caption')->setMediaUrl('test_media_url'); + $termiiOptions = (new TermiiOptions()) + ->type('test_type') + ->channel('test_channel') + ->media('test_media_url', 'test_media_caption'); self::assertSame([ - 'from' => 'test_from', 'type' => 'test_type', 'channel' => 'test_channel', - 'media_caption' => 'test_media_caption', - 'media_url' => 'test_media_url', + 'media' => [ + 'url' => 'test_media_url', + 'caption' => 'test_media_caption', + ], ], $termiiOptions->toArray()); } } diff --git a/src/Symfony/Component/Notifier/Bridge/TurboSms/TurboSmsTransport.php b/src/Symfony/Component/Notifier/Bridge/TurboSms/TurboSmsTransport.php index 1d885abd2d07d..5d08fe352aa15 100644 --- a/src/Symfony/Component/Notifier/Bridge/TurboSms/TurboSmsTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/TurboSms/TurboSmsTransport.php @@ -109,7 +109,7 @@ private function assertValidFrom(string $from): void private function assertValidSubject(string $subject): void { // Detect if there is at least one cyrillic symbol in the text - if (1 === preg_match("/\p{Cyrillic}/u", $subject)) { + if (preg_match("/\p{Cyrillic}/u", $subject)) { $subjectLimit = self::SUBJECT_CYRILLIC_LIMIT; $symbols = 'cyrillic'; } else { diff --git a/src/Symfony/Component/Notifier/Bridge/Twilio/TwilioTransport.php b/src/Symfony/Component/Notifier/Bridge/Twilio/TwilioTransport.php index adced312f5f3a..b51ba0c32b6f5 100644 --- a/src/Symfony/Component/Notifier/Bridge/Twilio/TwilioTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Twilio/TwilioTransport.php @@ -59,10 +59,6 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - if ($message->getOptions() && !$message->getOptions() instanceof TwilioOptions) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, TwilioOptions::class)); - } - $from = $message->getFrom() ?: $this->from; if (!preg_match('/^[a-zA-Z0-9\s]{2,11}$/', $from) && !preg_match('/^\+[1-9]\d{1,14}$/', $from)) { @@ -70,7 +66,7 @@ protected function doSend(MessageInterface $message): SentMessage } $endpoint = sprintf('https://%s/2010-04-01/Accounts/%s/Messages.json', $this->getEndpoint(), $this->accountSid); - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $body = [ 'From' => $from, 'To' => $message->getPhone(), @@ -80,7 +76,7 @@ protected function doSend(MessageInterface $message): SentMessage $body['StatusCallback'] = $options['webhook_url']; } $response = $this->client->request('POST', $endpoint, [ - 'auth_basic' => $this->accountSid.':'.$this->authToken, + 'auth_basic' => [$this->accountSid, $this->authToken], 'body' => $body, ]); diff --git a/src/Symfony/Component/Notifier/Bridge/Vonage/VonageTransport.php b/src/Symfony/Component/Notifier/Bridge/Vonage/VonageTransport.php index aaefa85d5dffb..40ce9e650d72b 100644 --- a/src/Symfony/Component/Notifier/Bridge/Vonage/VonageTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Vonage/VonageTransport.php @@ -58,11 +58,9 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); } - $from = $message->getFrom() ?: $this->from; - $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/sms/json', [ 'body' => [ - 'from' => $from, + 'from' => $message->getFrom() ?: $this->from, 'to' => $message->getPhone(), 'text' => $message->getSubject(), 'api_key' => $this->apiKey, diff --git a/src/Symfony/Component/Notifier/Bridge/Zendesk/ZendeskTransport.php b/src/Symfony/Component/Notifier/Bridge/Zendesk/ZendeskTransport.php index eedb81ea825bc..36d7690f0de33 100644 --- a/src/Symfony/Component/Notifier/Bridge/Zendesk/ZendeskTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Zendesk/ZendeskTransport.php @@ -45,7 +45,7 @@ public function __toString(): string public function supports(MessageInterface $message): bool { - return $message instanceof ChatMessage; + return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof ZendeskOptions); } protected function doSend(MessageInterface $message = null): SentMessage @@ -54,23 +54,19 @@ protected function doSend(MessageInterface $message = null): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - if (null !== $message->getOptions() && !($message->getOptions() instanceof ZendeskOptions)) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, ZendeskOptions::class)); - } - $endpoint = sprintf('https://%s/api/v2/tickets.json', $this->getEndpoint()); $body = [ 'ticket' => [ 'subject' => $message->getSubject(), 'comment' => [ - 'body' => $message->getNotification() ? $message->getNotification()->getContent() : '', + 'body' => $message->getNotification()?->getContent() ?? '', ], ], ]; - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; - if ($options['priority'] ?? null) { + $options = $message->getOptions()?->toArray() ?? []; + if (isset($options['priority'])) { $body['ticket']['priority'] = $options['priority']; } diff --git a/src/Symfony/Component/Notifier/Bridge/Zulip/ZulipTransport.php b/src/Symfony/Component/Notifier/Bridge/Zulip/ZulipTransport.php index 1f601024ac2f6..f9b42fc7f9d3f 100644 --- a/src/Symfony/Component/Notifier/Bridge/Zulip/ZulipTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Zulip/ZulipTransport.php @@ -59,11 +59,7 @@ protected function doSend(MessageInterface $message): SentMessage throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); } - if (null !== $message->getOptions() && !($message->getOptions() instanceof ZulipOptions)) { - throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, ZulipOptions::class)); - } - - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; + $options = $message->getOptions()?->toArray() ?? []; $options['content'] = $message->getSubject(); if (null === $message->getRecipientId() && empty($options['topic'])) { @@ -81,7 +77,7 @@ protected function doSend(MessageInterface $message): SentMessage $endpoint = sprintf('https://%s/api/v1/messages', $this->getEndpoint()); $response = $this->client->request('POST', $endpoint, [ - 'auth_basic' => $this->email.':'.$this->token, + 'auth_basic' => [$this->email, $this->token], 'body' => $options, ]);