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

coding based on overtrue/easy-sms. #1

Merged
merged 16 commits into from
Apr 10, 2020
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
57 changes: 57 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "zing/laravel-sms",
"description": "Provides sms notification channel for Laravel.",
"license": "MIT",
"authors": [
{
"name": "zingimmick",
"email": "zingimmick@outlook.com"
}
],
"require": {
"php": "^7.2.5",
"ext-json": "*",
"ext-simplexml": "*",
"graham-campbell/manager": "^4.5",
"guzzlehttp/guzzle": "^6.5",
"illuminate/notifications": "^6.0|^7.0|^8.0"
},
"require-dev": {
"mockery/mockery": "^1.0",
"orchestra/testbench": "^4.0|^5.0",
"phpunit/phpunit": "^8.0|^9.0",
"roave/security-advisories": "dev-master",
"symplify/easy-coding-standard": "^7.2"
},
"autoload": {
"psr-4": {
"Zing\\LaravelSms\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Zing\\LaravelSms\\Tests\\": "tests"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true,
"preferred-install": "dist"
},
"extra": {
"laravel": {
"providers": [
"Zing\\LaravelSms\\SmsServiceProvider"
]
}
},
"scripts": {
"ecs:test": "ecs check --ansi",
"phpunit:test": "phpunit --color=always",
"test": [
"@ecs:test",
"@phpunit:test"
]
}
}
23 changes: 23 additions & 0 deletions config/sms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Zing\LaravelSms\Drivers\LogDriver;
use Zing\LaravelSms\Drivers\NullDriver;
use Zing\LaravelSms\Drivers\YunPianDriver;

return [
'default' => env('SMS_CONNECTION', 'log'),
'connections' => [
'log' => [
'driver' => LogDriver::class,
'channel' => env('SMS_LOG_CHANNEL', null),
'level' => env('SMS_LOG_LEVEL', 'info'),
],
'null' => [
'driver' => NullDriver::class,
],
'yunpian' => [
'driver' => YunPianDriver::class,
'api_key' => env('YUNPIAN_KEY'),
],
],
];
36 changes: 36 additions & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
PhpCsFixer\Fixer\Operator\ConcatSpaceFixer:
spacing: one
PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer:
align: left
PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer:
syntax: short
PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer:
elements:
- 'property'
- 'method'
- 'const'
PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer:
order:
- 'use_trait'
- 'constant_public'
- 'constant_protected'
- 'constant_private'
PhpCsFixer\Fixer\PhpUnit\PhpUnitMethodCasingFixer:
case: snake_case
PhpCsFixer\Fixer\Operator\IncrementStyleFixer:
style: post

parameters:
sets:
- symfony
- common
- clean-code
skip:
PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer: ~
PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer: ~
PhpCsFixer\Fixer\Phpdoc\PhpdocNoEmptyReturnFixer: ~
paths:
- config
- src
- tests
28 changes: 28 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Laravel Manager Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
75 changes: 75 additions & 0 deletions src/Channels/SmsChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Zing\LaravelSms\Channels;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Notifications\Notification;
use Zing\LaravelSms\Message;
use Zing\LaravelSms\SmsManager;

/**
* Class SmsChannel.
*/
class SmsChannel
{
/**
* The event dispatcher.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;

protected $smsManager;

/**
* Create a new database channel.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @param \Zing\LaravelSms\SmsManager $smsManager
*/
public function __construct(Dispatcher $events, SmsManager $smsManager)
{
$this->events = $events;
$this->smsManager = $smsManager;
}

/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @return array|null
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toSms($notifiable);
$receiver = $notifiable->routeNotificationFor('sms', $notification);
if (! $receiver) {
return;
}
if (is_string($message)) {
$message = Message::text($message);
}
if (! $message instanceof Message) {
return;
}

return $this->smsManager->connection($message->connection)->send($receiver, $message);
}

public function resolveReceiver($notifiable, Notification $notification)
{
if ($notifiable instanceof AnonymousNotifiable) {
$receiver = $notifiable->routeNotificationFor(static::class);
if ($receiver) {
return $receiver;
}

return $notifiable->routeNotificationFor('sms');
}

return $notifiable->routeNotificationFor('sms', $notification);
}
}
8 changes: 8 additions & 0 deletions src/Contracts/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Zing\LaravelSms\Contracts;

interface Driver
{
public function send($number, $message);
}
20 changes: 20 additions & 0 deletions src/Contracts/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Zing\LaravelSms\Contracts;

use JsonSerializable;

interface Message extends JsonSerializable
{
public const VOICE = 'voice';

public const TEXT = 'text';

public function getContent($gateway = null): ?string;

public function getTemplate($gateway = null): ?string;

public function getData($gateway = null): ?array;

public function __toString();
}
41 changes: 41 additions & 0 deletions src/Contracts/PhoneNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Zing\LaravelSms\Contracts;

use JsonSerializable;

interface PhoneNumber extends JsonSerializable
{
/**
* 86.
*
* @return int
*/
public function getIDDCode();

/**
* 18888888888.
*
* @return int
*/
public function getNumber();

/**
* +8618888888888.
*
* @return string
*/
public function getUniversalNumber();

/**
* 008618888888888.
*
* @return string
*/
public function getZeroPrefixedNumber();

/**
* @return string
*/
public function __toString();
}
84 changes: 84 additions & 0 deletions src/Drivers/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Zing\LaravelSms\Drivers;

use Zing\LaravelSms\Contracts\Driver as DriverContract;
use Zing\LaravelSms\Contracts\Message as MessageContract;
use Zing\LaravelSms\Contracts\PhoneNumber as PhoneNumberContract;
use Zing\LaravelSms\Exceptions\CannotSendNotification;
use Zing\LaravelSms\Message;
use Zing\LaravelSms\PhoneNumber;
use Zing\LaravelSms\Support\Config;
use Zing\LaravelSms\Support\HasHttpRequest;

abstract class Driver implements DriverContract
{
use HasHttpRequest;

public $client;

public $config;

public function __construct($config = null)
{
$this->config = new Config($config);
$this->client = $this->getHttpClient($this->getBaseOptions());
}

/**
* @param PhoneNumberContract|string $number
*
* @return PhoneNumberContract
*/
protected function formatPhoneNumber($number)
{
if ($number instanceof PhoneNumberContract) {
return $number;
}

return new PhoneNumber(trim($number));
}

/**
* @param array|string|MessageContract $message
*
* @return array|string|\Zing\LaravelSms\Contracts\Message
*/
protected function formatMessage($message): MessageContract
{
if ($message instanceof MessageContract) {
return $message;
}
if (is_array($message)) {
return Message::template($message['template'] ?? '', $message['data'] ?? []);
}

return Message::text($message);
}

/**
* @param mixed $number
* @param mixed $message
*
* @return bool|mixed
*
* @throws \Zing\LaravelSms\Exceptions\CannotSendNotification
*/
public function send($number, $message)
{
$number = $this->formatPhoneNumber($number);
$message = $this->formatMessage($message);

return $this->sendMessage($number, $message);
}

/**
* @param \Zing\LaravelSms\Contracts\PhoneNumber $number
* @param \Zing\LaravelSms\Contracts\Message $message
*
* @return mixed
*
* @throws CannotSendNotification
*/
abstract protected function sendMessage(PhoneNumberContract $number, MessageContract $message);
}
Loading