Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Feat app adapters #63

Merged
merged 14 commits into from
Nov 27, 2023
20 changes: 20 additions & 0 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Static Analysis"

on: [pull_request]
jobs:
analyse:
name: Analyse
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2

- run: git checkout HEAD^2

- name: Run Static Analysis
run: |
docker run --rm -v $PWD:/app composer sh -c \
"composer install --profile --ignore-platform-reqs && composer analyse"
File renamed without changes.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ jobs:
VONAGE_API_SECRET: ${{ secrets.VONAGE_API_SECRET }}
VONAGE_TO: ${{ secrets.VONAGE_TO }}
VONAGE_FROM: ${{ secrets.VONAGE_FROM }}
DISCORD_WEBHOOK_ID: ${{ secrets.DISCORD_WEBHOOK_ID }}
DISCORD_WEBHOOK_TOKEN: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
run: |
docker compose up -d --build
sleep 5
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ composer require utopia-php/messaging
<?php

use \Utopia\Messaging\Messages\Email;
use \Utopia\Messaging\Adapters\Email\SendGrid;
use \Utopia\Messaging\Adapters\Email\Mailgun;
use \Utopia\Messaging\Adapter\Email\SendGrid;
use \Utopia\Messaging\Adapter\Email\Mailgun;

$message = new Email(
to: ['[email protected]'],
Expand All @@ -43,8 +43,8 @@ $messaging->send($message);
<?php

use \Utopia\Messaging\Messages\SMS;
use \Utopia\Messaging\Adapters\SMS\Twilio;
use \Utopia\Messaging\Adapters\SMS\Telesign;
use \Utopia\Messaging\Adapter\SMS\Twilio;
use \Utopia\Messaging\Adapter\SMS\Telesign;

$message = new SMS(
to: ['+12025550139'],
Expand All @@ -64,7 +64,7 @@ $messaging->send($message);
<?php

use \Utopia\Messaging\Messages\Push;
use \Utopia\Messaging\Adapters\Push\FCM;
use \Utopia\Messaging\Adapter\Push\FCM;

$message = new Push(
to: ['eyJhGc...ssw5c'],
Expand Down
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"license": "MIT",
"minimum-stability": "stable",
"scripts": {
"test": "vendor/bin/phpunit",
"test": "./vendor/bin/phpunit",
"lint": "./vendor/bin/pint --test",
"format": "./vendor/bin/pint"
"format": "./vendor/bin/pint",
"analyse": "./vendor/bin/phpstan analyse --memory-limit=2G --level=6 src tests"
},
"autoload": {
"psr-4": {
Expand All @@ -17,18 +18,19 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\E2E\\": "tests/e2e",
"Tests\\Unit\\": "tests/unit"
"Utopia\\Tests\\": "tests/Messaging"
}
},
"require": {
"php": ">=8.0.0",
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "9.6.*",
"ext-openssl": "*",
"phpunit/phpunit": "9.6.10",
"phpmailer/phpmailer": "6.8.*",
"laravel/pint": "1.13.*"
"laravel/pint": "1.13.*",
"phpstan/phpstan": "1.10.*"
},
"config": {
"platform": {
Expand Down
68 changes: 66 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ services:
- VONAGE_API_SECRET
- VONAGE_TO
- VONAGE_FROM
- DISCORD_WEBHOOK_ID
- DISCORD_WEBHOOK_TOKEN
build:
context: .
volumes:
Expand Down
4 changes: 2 additions & 2 deletions docs/add-new-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ Putting it all together, the `SendGrid` adapter should look like this:
```php
<?php

namespace Utopia\Messaging\Adapters\Email;
namespace Utopia\Messaging\Adapter\Email;

use Utopia\Messaging\Messages\Email;
use Utopia\Messaging\Adapters\Email as EmailAdapter;
use Utopia\Messaging\Adapter\Email as EmailAdapter;

class Sendgrid extends EmailAdapter
{
Expand Down
7 changes: 2 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
stopOnFailure="false"
>
<testsuites>
<testsuite name="unit">
<directory>./tests/unit</directory>
</testsuite>
<testsuite name="e2e">
<directory>./tests/e2e/</directory>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
19 changes: 17 additions & 2 deletions src/Utopia/Messaging/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,30 @@ abstract public function getMaxMessagesPerRequest(): int;
*
* @param Message $message The message to send.
* @return string The response body.
*
* @throws \Exception
*/
abstract public function send(Message $message): string;
public function send(Message $message): string
{
if (! \is_a($message, $this->getMessageType())) {
throw new \Exception('Invalid message type.');
}
if (\method_exists($message, 'getTo') && \count($message->getTo()) > $this->getMaxMessagesPerRequest()) {
throw new \Exception("{$this->getName()} can only send {$this->getMaxMessagesPerRequest()} messages per request.");
}
if (! \method_exists($this, 'process')) {
throw new \Exception('Adapter does not implement process method.');
}

return $this->process($message);
}

/**
* Send an HTTP request.
*
* @param string $method The HTTP method to use.
* @param string $url The URL to send the request to.
* @param array $headers An array of headers to send with the request.
* @param array<string> $headers An array of headers to send with the request.
* @param string|null $body The body of the request.
* @return string The response body.
*
Expand Down
82 changes: 82 additions & 0 deletions src/Utopia/Messaging/Adapter/Chat/Discord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Utopia\Messaging\Adapter\Chat;

use Utopia\Messaging\Adapter;
use Utopia\Messaging\Messages\Discord as DiscordMessage;

class Discord extends Adapter
{
/**
* @param string $webhookId Your Discord webhook ID.
* @param string $webhookToken Your Discord webhook token.
*/
public function __construct(
private string $webhookId,
private string $webhookToken,
) {
}

public function getName(): string
{
return 'Discord';
}

public function getType(): string
{
return 'app';
}

public function getMessageType(): string
{
return DiscordMessage::class;
}

public function getMaxMessagesPerRequest(): int
{
return 1;
}

/**
* @throws \Exception
*/
protected function process(DiscordMessage $message): string
{
$query = [];

if (! \is_null($message->getWait())) {
$query['wait'] = $message->getWait();
}
if (! \is_null($message->getThreadId())) {
$query['thread_id'] = $message->getThreadId();
}

$queryString = '';
foreach ($query as $key => $value) {
if (empty($queryString)) {
$queryString .= '?';
}
$queryString .= $key.'='.$value;
}

return $this->request(
method: 'POST',
url: "https://discord.com/api/webhooks/{$this->webhookId}/{$this->webhookToken}{$queryString}",
headers: [
'Content-Type: application/json',
],
body: \json_encode([
'content' => $message->getContent(),
'username' => $message->getUsername(),
'avatar_url' => $message->getAvatarUrl(),
'tts' => $message->getTTS(),
'embeds' => $message->getEmbeds(),
'allowed_mentions' => $message->getAllowedMentions(),
'components' => $message->getComponents(),
'attachments' => $message->getAttachments(),
'flags' => $message->getFlags(),
'thread_name' => $message->getThreadName(),
]),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Utopia\Messaging\Adapters;
namespace Utopia\Messaging\Adapter;

use Utopia\Messaging\Adapter;
use Utopia\Messaging\Message;
Expand All @@ -18,23 +18,6 @@ public function getMessageType(): string
return EmailMessage::class;
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
public function send(Message $message): string
{
if (! \is_a($message, $this->getMessageType())) {
throw new \Exception('Invalid message type.');
}
if (\count($message->getTo()) > $this->getMaxMessagesPerRequest()) {
throw new \Exception("{$this->getName()} can only send {$this->getMaxMessagesPerRequest()} messages per request.");
}

return $this->process($message);
}

/**
* Process an email message.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Utopia\Messaging\Adapters\Email;
namespace Utopia\Messaging\Adapter\Email;

use Utopia\Messaging\Adapters\Email as EmailAdapter;
use Utopia\Messaging\Adapter\Email as EmailAdapter;
use Utopia\Messaging\Messages\Email;

class Mailgun extends EmailAdapter
Expand Down
Loading