Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat-app-adapters
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
#	composer.lock
#	src/Utopia/Messaging/Adapter/Push/APNS.php
#	src/Utopia/Messaging/Messages/Email.php
#	src/Utopia/Messaging/Messages/Push.php
#	src/Utopia/Messaging/Messages/SMS.php
#	tests/Messaging/Adapter/Email/EmailTest.php
#	tests/Messaging/Adapter/Email/MailgunTest.php
#	tests/Messaging/Adapter/Email/SendgridTest.php
#	tests/Messaging/Adapter/Push/APNSTest.php
#	tests/Messaging/Adapter/Push/FCMTest.php
#	tests/Messaging/Adapter/SMS/GEOSMS/CallingCodeTest.php
#	tests/Messaging/Adapter/SMS/GEOSMSTest.php
#	tests/Messaging/Adapter/SMS/Msg91Test.php
#	tests/Messaging/Adapter/SMS/SMSTest.php
#	tests/Messaging/Adapter/SMS/TelesignTest.php
#	tests/Messaging/Adapter/SMS/TelnyxTest.php
#	tests/Messaging/Adapter/SMS/TwilioTest.php
#	tests/Messaging/Adapter/SMS/VonageTest.php
  • Loading branch information
abnegate committed Nov 27, 2023
2 parents 40e2484 + fb2a096 commit 996cc78
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 116 deletions.
175 changes: 87 additions & 88 deletions composer.lock

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions src/Utopia/Messaging/Adapter/Push/APNS.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
private string $authKeyId,
private string $teamId,
private string $bundleId,
private string $endpoint
private bool $sandbox = false
) {
}

Expand Down Expand Up @@ -93,12 +93,19 @@ private function notify(array $to, array $payload): array
CURLOPT_HEADER => true,
]);

$response = '';
$endpoint = 'https://api.push.apple.com';

if ($this->sandbox) {
$endpoint = 'https://api.development.push.apple.com';
}

$mh = \curl_multi_init();
$handles = [];

// Create a handle for each request
foreach ($to as $token) {
\curl_setopt($ch, CURLOPT_URL, $this->endpoint.'/3/device/'.$token);
\curl_setopt($ch, CURLOPT_URL, $endpoint.'/3/device/'.$token);

$handle = \curl_copy_handle($ch);
\curl_multi_add_handle($mh, $handle);
Expand Down
3 changes: 2 additions & 1 deletion src/Utopia/Messaging/Adapter/SMS/Clickatell.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Clickatell extends SMSAdapter
*/
public function __construct(
private string $apiKey,
private ?string $from = null
) {
}

Expand Down Expand Up @@ -43,7 +44,7 @@ protected function process(SMS $message): string
],
body: \json_encode([
'content' => $message->getContent(),
'from' => $message->getFrom(),
'from' => $this->from ?? $message->getFrom(),
'to' => $message->getTo(),
]),
);
Expand Down
5 changes: 3 additions & 2 deletions src/Utopia/Messaging/Adapter/SMS/Infobip.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Infobip extends SMSAdapter
*/
public function __construct(
private string $apiBaseUrl,
private string $apiKey
private string $apiKey,
private ?string $from = null
) {
}

Expand Down Expand Up @@ -48,7 +49,7 @@ protected function process(SMS $message): string
body: \json_encode([
'messages' => [
'text' => $message->getContent(),
'from' => $message->getFrom(),
'from' => $this->from ?? $message->getFrom(),
'destinations' => $to,
],
]),
Expand Down
26 changes: 19 additions & 7 deletions src/Utopia/Messaging/Adapter/SMS/Msg91.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class Msg91 extends SMSAdapter
{
private string $templateId;

/**
* @param string $senderId Msg91 Sender ID
* @param string $authKey Msg91 Auth Key
Expand All @@ -31,17 +33,28 @@ public function getMaxMessagesPerRequest(): int
return 1000;
}

public function setTemplate(string $templateId): self
{
$this->templateId = $templateId;

return $this;
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(SMS $message): string
{
$to = \array_map(
fn ($to) => ['mobiles' => \ltrim($to, '+')],
$message->getTo()
);
$recipients = [];
foreach ($message->getTo() as $recipient) {
$recipients[] = [
'mobiles' => \ltrim($recipient, '+'),
'content' => $message->getContent(),
'otp' => $message->getContent(),
];
}

return $this->request(
method: 'POST',
Expand All @@ -52,9 +65,8 @@ protected function process(SMS $message): string
],
body: \json_encode([
'sender' => $this->senderId,
'otp' => $message->getContent(),
'flow_id' => $message->getFrom(),
'recipients' => [$to],
'template_id' => $this->templateId,
'recipients' => $recipients,
]),
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Utopia/Messaging/Adapter/SMS/Plivo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Plivo extends SMSAdapter
*/
public function __construct(
private string $authId,
private string $authToken
private string $authToken,
private ?string $from = null
) {
}

Expand Down Expand Up @@ -44,7 +45,7 @@ protected function process(SMS $message): string
],
body: \http_build_query([
'text' => $message->getContent(),
'src' => $message->getFrom() ?? 'Plivo',
'src' => $this->from ?? $message->getFrom() ?? 'Plivo',
'dst' => \implode('<', $message->getTo()),
]),
);
Expand Down
5 changes: 3 additions & 2 deletions src/Utopia/Messaging/Adapter/SMS/Seven.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Seven extends SMSAdapter
* @param string $apiKey Seven API token
*/
public function __construct(
private string $apiKey
private string $apiKey,
private ?string $from = null
) {
}

Expand Down Expand Up @@ -42,7 +43,7 @@ protected function process(SMS $message): string
'content-type: application/json',
],
body: \json_encode([
'from' => $message->getFrom(),
'from' => $this->from ?? $message->getFrom(),
'to' => \implode(',', $message->getTo()),
'text' => $message->getContent(),
]),
Expand Down
5 changes: 3 additions & 2 deletions src/Utopia/Messaging/Adapter/SMS/Sinch.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Sinch extends SMSAdapter
*/
public function __construct(
private string $servicePlanId,
private string $apiToken
private string $apiToken,
private ?string $from = null
) {
}

Expand Down Expand Up @@ -46,7 +47,7 @@ protected function process(SMS $message): string
'content-type: application/json',
],
body: \json_encode([
'from' => $message->getFrom(),
'from' => $this->from ?? $message->getFrom(),
'to' => $to,
'body' => $message->getContent(),
]),
Expand Down
3 changes: 2 additions & 1 deletion src/Utopia/Messaging/Adapter/SMS/Telnyx.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Telnyx extends SMSAdapter
*/
public function __construct(
private string $apiKey,
private ?string $from = null
) {
}

Expand Down Expand Up @@ -41,7 +42,7 @@ protected function process(SMS $message): string
],
body: \json_encode([
'text' => $message->getContent(),
'from' => $message->getFrom(),
'from' => $this->from ?? $message->getFrom(),
'to' => $message->getTo()[0],
]),
);
Expand Down
5 changes: 3 additions & 2 deletions src/Utopia/Messaging/Adapter/SMS/TextMagic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class TextMagic extends SMSAdapter
*/
public function __construct(
private string $username,
private string $apiKey
private string $apiKey,
private ?string $from = null
) {
}

Expand Down Expand Up @@ -51,7 +52,7 @@ protected function process(SMS $message): string
],
body: \http_build_query([
'text' => $message->getContent(),
'from' => \ltrim($message->getFrom(), '+'),
'from' => \ltrim($this->from ?? $message->getFrom(), '+'),
'phones' => \implode(',', $to),
]),
);
Expand Down
5 changes: 3 additions & 2 deletions src/Utopia/Messaging/Adapter/SMS/Twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Twilio extends SMSAdapter
*/
public function __construct(
private string $accountSid,
private string $authToken
private string $authToken,
private ?string $from = null
) {
}

Expand Down Expand Up @@ -42,7 +43,7 @@ protected function process(SMS $message): string
],
body: \http_build_query([
'Body' => $message->getContent(),
'From' => $message->getFrom(),
'From' => $this->from ?? $message->getFrom(),
'To' => $message->getTo()[0],
]),
);
Expand Down
5 changes: 3 additions & 2 deletions src/Utopia/Messaging/Adapter/SMS/Vonage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Vonage extends SMSAdapter
*/
public function __construct(
private string $apiKey,
private string $apiSecret
private string $apiSecret,
private ?string $from = null
) {
}

Expand Down Expand Up @@ -47,7 +48,7 @@ protected function process(SMS $message): string
url: 'https://rest.nexmo.com/sms/json',
body: \http_build_query([
'text' => $message->getContent(),
'from' => $message->getFrom(),
'from' => $this->from ?? $message->getFrom(),
'to' => $to[0], //\implode(',', $to),
'api_key' => $this->apiKey,
'api_secret' => $this->apiSecret,
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions tests/Messaging/Adapter/Push/APNSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ public function testSend(): void
$authKeyId = \getenv('APNS_AUTH_ID');
$teamId = \getenv('APNS_TEAM_ID');
$bundleId = \getenv('APNS_BUNDLE_ID');
$endpoint = 'https://api.sandbox.push.apple.com:443';

$adapter = new APNSAdapter($authKey, $authKeyId, $teamId, $bundleId, $endpoint);
$adapter = new APNSAdapter($authKey, $authKeyId, $teamId, $bundleId);

$message = new Push(
to: [\getenv('APNS_TO')],
Expand Down
2 changes: 1 addition & 1 deletion tests/Messaging/Adapter/SMS/GEOSMS/CallingCodeTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Utopia\Tests\Messaging\Adapter\SMS\GEOSMS;
namespace Utopia\Tests\Adapter\SMS\GEOSMS;

use Utopia\Messaging\Adapter\SMS\GEOSMS\CallingCode;
use Utopia\Tests\Adapter\Base;
Expand Down

0 comments on commit 996cc78

Please sign in to comment.