Skip to content

AllMySms notifier bridge #33956

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

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/AllMySMS/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\AllMySMS;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Quentin Dequippe <quentin@dequippe.tech>
*/
final class AllMySMSTransport extends AbstractTransport
{
protected const HOST = 'api.allmysms.com';

private $login;
private $apiKey;
private $tpoa;

public function __construct(string $login, string $apiKey, string $tpoa = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->login = $login;
$this->apiKey = $apiKey;
$this->tpoa = $tpoa;

parent::__construct($client, $dispatcher);
}

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

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage;
}

protected function doSend(MessageInterface $message): void
{
if (!$message instanceof SmsMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, \get_class($message)));
}

$endpoint = sprintf('https://%s/sms/send/', $this->getEndpoint());
$response = $this->client->request('POST', $endpoint, [
'auth_basic' => base64_encode($this->login.':'.$this->apiKey),
'body' => [
'from' => $this->tpoa,
'to' => $message->getPhone(),
'text' => $message->getSubject(),
],
]);

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

throw new TransportException(sprintf('Unable to send the SMS: %s (%s).', $error['description'], $error['code']), $response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\AllMySMS;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Transport\TransportInterface;

/**
* @author Quentin Dequippe <quentin@dequippe.tech>
*/
final class AllMySMSTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$login = $this->getUser($dsn);
$apiKey = $this->getPassword($dsn);
$tpoa = $dsn->getOption('tpoa');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

if ('allmysms' === $scheme) {
return (new AllMySMSTransport($login, $apiKey, $this->client, $tpoa, $this->dispatcher))->setHost($host)->setPort($port);
}

throw new UnsupportedSchemeException($dsn, 'allmysms', $this->getSupportedSchemes());
}

protected function getSupportedSchemes(): array
{
return ['allmysms'];
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/AllMySMS/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.0.0
-----

* Added the bridge
12 changes: 12 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/AllMySMS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
AllMySMS Notifier
=================

Provides AllMySMS integration for Symfony Notifier.

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
35 changes: 35 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/AllMySMS/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "symfony/allmysms-notifier",
"type": "symfony-bridge",
"description": "AllMySMS Notifier Bridge",
"keywords": ["sms", "allmysms", "notifier"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": "^7.1",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\AllMySMS\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
}
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/AllMySMS/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="AllMySMS Notifier Bridge Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>