Skip to content

Commit

Permalink
🚀 Yes, it works!
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Mar 10, 2017
1 parent 655c814 commit 02b9c28
Show file tree
Hide file tree
Showing 19 changed files with 644 additions and 87 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ composer.lock
.subsplit
.php_cs.cache
.idea
index.php
config.php
25 changes: 14 additions & 11 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ This file is part of the overtrue/easy-sms.
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',

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(array(
'@Symfony' => true,
'header_comment' => array('header' => $header),
'array_syntax' => array('syntax' => 'short'),
'ordered_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_construct' => true,
'php_unit_strict' => true,
))
->finder(
Symfony\CS\Finder\DefaultFinder::create()
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
)
Expand Down
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
# Easy-sms

The easiest way to send short message.
<p align="center"><h1>Easy SMS</h1></p>

<p align="center">:calling: The easiest way to send short message.</p>

<p align="center">
<a href="https://travis-ci.org/overtrue/easy-sms"><img src="https://travis-ci.org/overtrue/easy-sms.svg?branch=master" alt="Build Status"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/v/unstable.svg" alt="Latest Unstable Version"></a>
<a href="https://scrutinizer-ci.com/g/overtrue/easy-sms/build-status/master"><img src="https://scrutinizer-ci.com/g/overtrue/easy-sms/badges/build.png?b=master" alt="Build Status"></a>
<a href="https://scrutinizer-ci.com/g/overtrue/easy-sms/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/easy-sms/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"></a>
<a href="https://scrutinizer-ci.com/g/overtrue/easy-sms/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/easy-sms/badges/coverage.png?b=master" alt="Code Coverage"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/downloads" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/license" alt="License"></a>
</p>

# Usage

```php
use Overtrue\EasySms\EasySms;

$config = [...];
$config = [
'default' => 'error-log',
'signature' => '【xxx公司】',
'gateways' => [
/*...*/
'error-log' => [
'file' => '/tmp/easy-sms.log',
],
/*...*/
'yun-pian' => [
'api_key' => '824f0ff2f71cab52936a13ede3xxxxx',
],
],
];
$easySms = new EasySms($config);
$easySms->gateway('Log')->send(1888888888, 'hello world!');
$easySms->gateway('error-log')->send(1888888888, 'hello world!');
```

# License
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"type": "library",
"require": {
"guzzlehttp/guzzle": "^6.2",
"php": ">=5.5"
"php": ">=5.5",
"mockery/mockery": "1.0.x-dev"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"mockery/mockery": "^0.9.9"
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": {
Expand Down
11 changes: 9 additions & 2 deletions src/Contracts/GatewayInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?php

/*
* 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.
*/

namespace Overtrue\EasySms\Contracts;

/**
* Class GatewayInterface
* Class GatewayInterface.
*/
interface GatewayInterface
{
Expand All @@ -17,4 +24,4 @@ interface GatewayInterface
* @return mixed
*/
public function send($to, $template, array $data = []);
}
}
24 changes: 18 additions & 6 deletions src/EasySms.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* 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.
*/

namespace Overtrue\EasySms;

use Closure;
Expand All @@ -9,7 +16,7 @@
use RuntimeException;

/**
* Class EasySms
* Class EasySms.
*/
class EasySms
{
Expand Down Expand Up @@ -50,7 +57,7 @@ public function __construct(array $config)
/**
* Create a gateway.
*
* @param string $name
* @param string $name
*
* @return \Overtrue\EasySms\Contracts\GatewayInterface
*/
Expand Down Expand Up @@ -85,7 +92,7 @@ public function extend($name, Closure $callback)
*
* @return string
*
* @throws if no default gateway configured.
* @throws if no default gateway configured
*/
public function getDefaultGateway()
{
Expand Down Expand Up @@ -114,6 +121,7 @@ public function setDefaultGateway($name)
* Create a new driver instance.
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return mixed
Expand All @@ -123,8 +131,12 @@ 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, []));
$className = $this->formatGatewayClassName($name);
$config = array_merge(
['signature' => $this->config->get('signature', '')],
$this->config->get("gateways.{$name}", [])
);
$gateway = $this->makeGateway($className, $config);
}

if (!($gateway instanceof GatewayInterface)) {
Expand Down Expand Up @@ -205,4 +217,4 @@ public function __call($method, $parameters)
{
return call_user_func_array([$this->gateway(), $method], $parameters);
}
}
}
38 changes: 38 additions & 0 deletions src/Gateways/ErrorLogGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* 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.
*/

namespace Overtrue\EasySms\Gateways;

/**
* Class ErrorLogGateway.
*/
class ErrorLogGateway extends Gateway
{
/**
* Send a short message.
*
* @param string|int $to
* @param string $message
* @param array $data
*
* @return mixed
*/
public function send($to, $message, array $data = [])
{
$message = sprintf(
"[%s] to: %s, message: \"%s\", data: %s\n",
date('Y-m-d H:i:s'),
$to,
addcslashes($message, '"'),
json_encode($data)
);

return error_log($message, 3, $this->config->get('file', ini_get('error_log')));
}
}
88 changes: 88 additions & 0 deletions src/Gateways/Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/*
* 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.
*/

namespace Overtrue\EasySms\Gateways;

use Overtrue\EasySms\Contracts\GatewayInterface;
use Overtrue\EasySms\Support\Config;

/**
* Class Gateway
*/
abstract class Gateway implements GatewayInterface
{
const DEFAULT_TIMEOUT = 5.0;

/**
* @var \Overtrue\EasySms\Support\Config
*/
protected $config;

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

/**
* @var float
*/
protected $timeout;

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

/**
* Return base uri.
*
* @return string
*/
public function getBaseUri()
{
return $this->baseUri;
}

/**
* Return timeout.
*
* @return int|mixed
*/
public function getTimeout()
{
return $this->timeout ?: $this->config->get('timeout', self::DEFAULT_TIMEOUT);
}

/**
* Set timeout.
*
* @param int $timeout
*
* @return $this
*/
public function timeout($timeout)
{
$this->timeout = floatval($timeout);

return $this;
}

/**
* @return \Overtrue\EasySms\Support\Config
*/
public function getConfig()
{
return $this->config;
}
}
25 changes: 0 additions & 25 deletions src/Gateways/LogGateway.php

This file was deleted.

Loading

0 comments on commit 02b9c28

Please sign in to comment.