Skip to content

Commit

Permalink
Init commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Mar 9, 2017
0 parents commit 655c814
Show file tree
Hide file tree
Showing 15 changed files with 712 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

[*.{vue,js,scss}]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text=auto

/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
phpunit.php export-ignore
phpunit.xml.dist export-ignore
phpunit.xml export-ignore
.php_cs export-ignore
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.DS_Store
/vendor
/coverage
sftp-config.json
composer.lock
.subsplit
.php_cs.cache
.idea
23 changes: 23 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
$header = <<<EOF
This file is part of the overtrue/easy-sms.
(c) overtrue <[email protected]>
This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;
Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header);
return Symfony\CS\Config\Config::create()
// use default SYMFONY_LEVEL and extra fixers:
->fixers(array(
'header_comment',
'short_array_syntax',
'ordered_use',
'php_unit_construct',
'php_unit_strict',
))
->finder(
Symfony\CS\Finder\DefaultFinder::create()
->exclude('vendor')
->in(__DIR__)
)
;
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Easy-sms

The easiest way to send short message.

# Usage

```php
use Overtrue\EasySms\EasySms;

$config = [...];
$easySms = new EasySms($config);
$easySms->gateway('Log')->send(1888888888, 'hello world!');
```

# License

MIT
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "overtrue/easy-sms",
"description": "The easiest way to send short message.",
"type": "library",
"require": {
"guzzlehttp/guzzle": "^6.2",
"php": ">=5.5"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"mockery/mockery": "^0.9.9"
},
"autoload": {
"psr-4": {
"Overtrue\\EasySms\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Overtrue\\EasySms\\Tests\\": "tests"
}
},
"license": "MIT",
"authors": [
{
"name": "overtrue",
"email": "[email protected]"
}
]
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
20 changes: 20 additions & 0 deletions src/Contracts/GatewayInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Overtrue\EasySms\Contracts;

/**
* Class GatewayInterface
*/
interface GatewayInterface
{
/**
* Send a short message.
*
* @param string|int $to
* @param string $template
* @param array $data
*
* @return mixed
*/
public function send($to, $template, array $data = []);
}
208 changes: 208 additions & 0 deletions src/EasySms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php

namespace Overtrue\EasySms;

use Closure;
use InvalidArgumentException;
use Overtrue\EasySms\Contracts\GatewayInterface;
use Overtrue\EasySms\Support\Config;
use RuntimeException;

/**
* Class EasySms
*/
class EasySms
{
/**
* @var array
*/
protected $config;

/**
* @var string
*/
protected $defaultGateway;

/**
* @var array
*/
protected $customCreators = [];

/**
* @var array
*/
protected $gateways = [];

/**
* Constructor.
*
* @param array $config
*/
public function __construct(array $config)
{
$this->config = new Config($config);

if (!empty($config['default'])) {
$this->setDefaultGateway($config['default']);
}
}

/**
* Create a gateway.
*
* @param string $name
*
* @return \Overtrue\EasySms\Contracts\GatewayInterface
*/
public function gateway($name = null)
{
$name = $name ?: $this->getDefaultGateway();

if (!isset($this->gateways[$name])) {
$this->gateways[$name] = $this->createGateway($name);
}

return $this->gateways[$name];
}

/**
* Register a custom driver creator Closure.
*
* @param string $name
* @param \Closure $callback
*
* @return $this
*/
public function extend($name, Closure $callback)
{
$this->customCreators[$name] = $callback;

return $this;
}

/**
* Get default gateway name.
*
* @return string
*
* @throws if no default gateway configured.
*/
public function getDefaultGateway()
{
if (empty($this->defaultGateway)) {
throw new RuntimeException('No default gateway configured.');
}

return $this->defaultGateway;
}

/**
* Set default gateway name.
*
* @param string $name
*
* @return $this
*/
public function setDefaultGateway($name)
{
$this->defaultGateway = $name;

return $this;
}

/**
* Create a new driver instance.
*
* @param string $name
* @throws \InvalidArgumentException
*
* @return mixed
*/
protected function createGateway($name)
{
if (isset($this->customCreators[$name])) {
$gateway = $this->callCustomCreator($name);
} else {
$name = $this->formatGatewayClassName($name);
$gateway = $this->makeGateway($name, $this->config->get($name, []));
}

if (!($gateway instanceof GatewayInterface)) {
throw new InvalidArgumentException(sprintf('Gateway "%s" not inherited from %s.', $name, GatewayInterface::class));
}

return $gateway;
}

/**
* Make gateway instance.
*
* @param string $gateway
* @param array $config
*
* @return \Overtrue\EasySms\Contracts\GatewayInterface
*/
protected function makeGateway($gateway, $config)
{
if (!class_exists($gateway)) {
throw new InvalidArgumentException(sprintf('Gateway "%s" not exists.', $gateway));
}

return new $gateway($config);
}

/**
* Format gateway name.
*
* @param string $name
*
* @return string
*/
protected function formatGatewayClassName($name)
{
if (class_exists($name)) {
return $name;
}

$name = $this->camelCase($name);

return __NAMESPACE__."\\Gateways\\{$name}Gateway";
}

/**
* Call a custom gateway creator.
*
* @param string gateway
*
* @return mixed
*/
protected function callCustomCreator($gateway)
{
return call_user_func($this->customCreators[$gateway], $this->config->get($gateway, []));
}

/**
* Convert string to camel case.
*
* @param string $name
*
* @return string
*/
protected function camelCase($name)
{
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $name)));
}

/**
* Dynamically call the default gateway instance.
*
* @param string $method
* @param array $parameters
*
* @return mixed
*/
public function __call($method, $parameters)
{
return call_user_func_array([$this->gateway(), $method], $parameters);
}
}
25 changes: 25 additions & 0 deletions src/Gateways/LogGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Overtrue\EasySms\Gateways;

use Overtrue\EasySms\Contracts\GatewayInterface;

/**
* Class LogGateway
*/
class LogGateway implements GatewayInterface
{
/**
* Send a short message.
*
* @param string|int $to
* @param string $template
* @param array $data
*
* @return mixed
*/
public function send($to, $template, array $data = [])
{
// TODO: Implement send() method.
}
}
Loading

0 comments on commit 655c814

Please sign in to comment.