Skip to content

Commit

Permalink
feat(service-provider): add a new service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
sudkumar committed Oct 19, 2019
0 parents commit f091d78
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
composer.lock
vendor
logs
.php_cs.cache
.phpunit.result.cache
15 changes: 15 additions & 0 deletions LICENCE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
The MIT License (MIT)
Copyright (c) 2019 Craftsys, Inc

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
130 changes: 130 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Laravel service provider for MSG91

_This library requires a minimum PHP version of 7.1_

This is a **[laravel](https://laravel.com) service provider** for [MSG91 APIs](https://docs.msg91.com/collection/msg91-api-integration/5/pages/139). It wraps the [msg91-php][client] client and provides the same functionality for Laravel applications by exposing a Service Provider and Facade.

> **NOTE**: The project is under active development and so, some apis are subjected to change before of `v1.0.0` release.
## Table of Contents

- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Examples](#examples)
- [Create a Client](#create-a-client)
- [Managing OTPs](#managing-otps)
- [Send OTP](#send-otp)
- [Verify OTP](#verify-otp)
- [Resend OTP](#resend-otp)

## Installation

The packages is available on [Packagist](https://packagist.org/packages/craftsys/msg91-laravel) and can be installed via [Composer](https://getcomposer.org/) by executing following command in shell.

```bash
composer require creaftsys/msg91-laravel
```

### Laravel 5.5+

If you're using Laravel 5.5 or above, the package will automatically register the `Craftsys\Laravel\MSGClient91\MSG91ServiceProvider` provider and aliases `Craftsys\Laravel\MSGClient91\Facade` facade to `MSG91`.

### Laravel 5.4 and below

Add `Craftsys\Laravel\MSGClient91\MSG91ServiceProvider` to the `providers` array in your `config/app.php`:

```php
'providers' => [
// Other service providers...
Craftsys\Laravel\MSGClient91\MSG91ServiceProvider::class,
],
```

If you want to use the facade interface, you can `use` the facade class when needed:

```php
use Craftsys\Laravel\MSGClient91\Facade;
```

Or add an alias in your `config/app.php`

```php
'aliases' => [
// other aliases here
'MSG91' => Craftsys\Laravel\MSGClient91\Facade::class,
],
```

## Configuration

As the [msg91-php][client] offers configuration that are similar to Laravel's configuration, this package simply ports the Laravel's configuration to the msg91-php client.

The package can be configured by providing a `msg91` key inside your `config/services.php` configuration file.

```php
<?php

return [
// along with other services
"msg91": [
'key' => env("MSG91_KEY"),
],
];
```

and update the `.env` file to get the desired values e.g. `MSG91_KEY`.

Please visit [msg91-php configuration][client-configuration] for a detailed description about the available options and their default values.

## Usage

Once you have [Configured](#configuration) the Laravel/Lumen application to use the service provider and have aliased the facade to `MSG91`, you will have to [msg91-php][client] client `Craftsys\MSG91\Client`'s instance.

```php
MSG91::otp()
->to(919999999999)
->send()
```

Next, follow along with [examples](#examples) to learn more

## Examples

### Managing OTPs

OTP services like sending, verifying, and resending etc, can be accessed via `otp` method on the client instance e.g. `MGS91::otp()`.

> For a detailed usage, please visit [msg91-php's documentation][client-managing-otps] on managing OTPs.
#### Send OTP

```php
MGS91::otp()
->to(912343434312) // phone number with country code
->send(); // send the otp
```

### Verify OTP

```php
MSG91::otp(1234) // OTP to be verified
->to(912343434312) // phone number with country code
->verify(); // Verify
```

### Resend OTP

```php
MSG91::otp()
->to(912343434312) // set the mobile with country code
->via("text") // way of retry
->resend(); // resend otp
```

> For all the examples and options, please consult [msg91-php examples section][client-examples]
[client]: https://github.com/craftsys/msg91-php
[client-configuration]: https://github.com/craftsys/msg91-php#configuration
[client-examples]: https://github.com/craftsys/msg91-php#examples
[client-managing-otps]: https://github.com/craftsys/msg91-php#managing-otps
67 changes: 67 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "craftsys/msg91-laravel",
"description": "Laravel service provider for MSG91 apis.",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Sudhir Mitharwal",
"email": "[email protected]",
"role": "Developer",
"homepage": "http://github.com/sudkumar"
}
],
"keywords": [
"msg91",
"php",
"laravel",
"otp"
],
"homepage": "https://github.com/craftsys/msg91-laravel",
"support": {
"issues": "https://github.com/craftsys/msg91-laravel/issues",
"source": "https://github.com/craftsys/msg91-laravel",
"email": "[email protected]"
},
"require": {
"php": ">=7.1",
"craftsys/msg91-php": "0.4.2",
"illuminate/support": "^6.3"
},
"require-dev": {
"orchestra/testbench": "^4.2",
"phpunit/phpunit": "^8.4"
},
"extra": {
"branch-alias": {
"dev-master": "0.x-dev",
"laravel": {
"aliases": {
"MSG91": "Craftsys\\Laravel\\MSG91\\Facade"
},
"providers": [
"Craftsys\\Laravel\\MSG91\\MSGServiceProvider"
]
}
}
},
"config": {
"optimize-autoloader": true,
"sort-packages": true
},
"prefer-stable": true,
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Craftsys\\Laravel\\MSG91Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Craftsys\\Laravel\\MSG91Client\\Test\\": "tests/"
}
},
"scripts": {
"test": "phpunit --colors=always"
}
}
12 changes: 12 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="unit">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
26 changes: 26 additions & 0 deletions src/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Craftsys\Laravel\MSG91Client;

use Craftsys\MSG91Client\Client;
use Illuminate\Support\Facades\Facade as BaseFacade;

/**
* Facade for Craftsys\MSG91Client\Client
*
* @method static \Craftsys\MSG91Client\OTPMessage otp(int|null $otp = null)
*/
class Facade extends BaseFacade
{
/**
* Get the registered name of the component.
*
* @return string
*
* @throws \RuntimeException
*/
protected static function getFacadeAccessor()
{
return Client::class;
}
}
33 changes: 33 additions & 0 deletions src/MSGServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Craftsys\Laravel\MSG91Client;

use Craftsys\MSG91Client\Client;
use Illuminate\Support\ServiceProvider;

class MSGServiceProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
// Bind MSG91 Client in Service Container.
$this->app->singleton(Client::class, function ($app) {
$config = $app['config'];
return new Client($config->get('services.msg91'));
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [Client::class];
}
}
32 changes: 32 additions & 0 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Craftsys\Laravel\MSG91Client\Test;

use Craftsys\MSG91Client\Client;

class ServiceProviderTest extends TestCase
{
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
*
* @return void
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('services.msg91.key', 'my_api_key');
}

/**
* Test that we can create the Nexmo client
* from container binding.
*
* @return void
*/
public function testClientResolutionFromContainer()
{
$client = app(Client::class);
$this->assertInstanceOf(Client::class, $client);
}
}
38 changes: 38 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Craftsys\Laravel\MSG91Client\Test;

use Orchestra\Testbench\TestCase as BaseTestCase;
use Craftsys\Laravel\MSG91Client\MSGServiceProvider;
use Craftsys\Laravel\MSG91Client\Facade;

abstract class TestCase extends BaseTestCase
{
/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [
MSGServiceProvider::class,
];
}

/**
* Get package aliases.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageAliases($app)
{
return [
'MSG91' => Facade::class
];
}
}

0 comments on commit f091d78

Please sign in to comment.