Skip to content

Commit

Permalink
Merge branch 'master' of github.com:fd6130/exabytes-sms-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
fd6130 committed May 25, 2022
2 parents db697d9 + 2774fdb commit 17be4d3
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 16 deletions.
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# exabytes-sms-bundle
Send sms using exabytes api
exabytes-sms-bundle
===================

Send sms using exabytes malaysia api. For more detail regarding the api, please read [documentation](https://support.exabytes.com.my/en/support/solutions/articles/14000110847-bulk-sms-api-integration).

Requirement
===========
* PHP 7.2+
* Symfony 4.4+ and 5+

Installation
============

`composer require fd6130/exabytes-sms-bundle`

> If you are using flex, it will automatic add this bundle to `bundles.php`.
Configuration
=============

Update your .env to include this two variable:

```
# .env
EXABYTES_USERNAME=
EXABYTES_PASSWORD=
```

And then create the config file `/config/fd_exabytes.yaml` and put the following content:

```yaml
fd_exabytes:
username: '%env(EXABYTES_USERNAME)%'
password: '%env(EXABYTES_PASSWORD)%'
```
Inject `ExabytesInterface` and start sending sms:

```php
private $exabytes;
public function __construct(ExabytesInterface $exabytes)
{
$this->exabytes = $exabytes;
}
public function sendSMS()
{
$this->exabytes->send('mobile number', 'message', ExabytesInterface::ASCII);
//...
}
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fd6130/exabytes-sms-bundle",
"description": "This bundle provide sms service using exabytes api.",
"description": "This bundle provide sms service using exabytes malaysia api.",
"keywords": ["Symfony", "symfony", "bundle", "sms", "exabytes"],
"type": "symfony-bundle",
"license": "MIT",
Expand Down
24 changes: 24 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Fd\ExabytesBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('fd_exabytes');
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
->scalarNode('username')->isRequired()->cannotBeEmpty()->end()
->scalarNode('password')->isRequired()->cannotBeEmpty()->end()
->end()
;

return $treeBuilder;
}
}
23 changes: 23 additions & 0 deletions src/DependencyInjection/FdExabytesExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Fd\ExabytesBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class FdExabytesExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');

$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$definition = $container->getDefinition('fd.exabytes.exabytes');
$definition->setArgument(0, $config);
}
}
9 changes: 9 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
fd.exabytes.exabytes:
class: Fd\ExabytesBundle\Service\Exabytes
arguments:
$client: "@http_client"
Fd\ExabytesBundle\Service\Exabytes:
arguments:
$client: "@http_client"
Fd\ExabytesBundle\Service\ExabytesInterface: "@fd.exabytes.exabytes"
19 changes: 6 additions & 13 deletions src/Service/Exabytes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,28 @@

namespace Fd\ExabytesBundle\Service;

use Fd\Exabytes\Exception\ExabytesException;
use Fd\ExabytesBundle\Exception\ExabytesException;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author fd6130
*/
class Exabytes implements ExabytesInterface
{
private $client;
private $params;
private $messageType = 1;
private $config;

public function __construct(string $username, string $password, int $messageType, HttpClientInterface $client)
public function __construct(array $config, HttpClientInterface $client)
{
$this->messageType = $messageType;
$this->config = $config;
$this->client = $client;
}

public function send(string $mobile, string $message, int $messageType)
{
$username = $this->params->get('exabytes.username');
$password = $this->params->get('exabytes.password');

$endpoint = sprintf(
'https://smsportal.exabytes.my/isms_send.php?un=%s&pwd=%s&dstno=%s&msg=%s&type=%s&agreedterm=YES',
urlencode($username),
urlencode($password),
urlencode($this->config['username']),
urlencode($this->config['password']),
urlencode($mobile),
urlencode($message),
$messageType
Expand Down
7 changes: 7 additions & 0 deletions src/Service/ExabytesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

namespace Fd\ExabytesBundle\Service;

use Fd\ExabytesBundle\Exception\ExabytesException;

interface ExabytesInterface
{
const ASCII = 1;
const UNICODE = 2;

/**
* @param string $mobile Mobile number
* @param string $message Message
* @param int $messageType Message Type (1 - ASCII, 2 - Unicode)
*
* @throws ExabytesException
*/
public function send(string $mobile, string $message, int $messageType);
}

0 comments on commit 17be4d3

Please sign in to comment.