Skip to content

Commit f091d78

Browse files
committed
feat(service-provider): add a new service provider
0 parents  commit f091d78

File tree

9 files changed

+359
-0
lines changed

9 files changed

+359
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
composer.lock
3+
vendor
4+
logs
5+
.php_cs.cache
6+
.phpunit.result.cache

LICENCE.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2019 Craftsys, Inc
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
Software.
11+
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Laravel service provider for MSG91
2+
3+
_This library requires a minimum PHP version of 7.1_
4+
5+
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.
6+
7+
> **NOTE**: The project is under active development and so, some apis are subjected to change before of `v1.0.0` release.
8+
9+
## Table of Contents
10+
11+
- [Installation](#installation)
12+
- [Configuration](#configuration)
13+
- [Usage](#usage)
14+
- [Examples](#examples)
15+
- [Create a Client](#create-a-client)
16+
- [Managing OTPs](#managing-otps)
17+
- [Send OTP](#send-otp)
18+
- [Verify OTP](#verify-otp)
19+
- [Resend OTP](#resend-otp)
20+
21+
## Installation
22+
23+
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.
24+
25+
```bash
26+
composer require creaftsys/msg91-laravel
27+
```
28+
29+
### Laravel 5.5+
30+
31+
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`.
32+
33+
### Laravel 5.4 and below
34+
35+
Add `Craftsys\Laravel\MSGClient91\MSG91ServiceProvider` to the `providers` array in your `config/app.php`:
36+
37+
```php
38+
'providers' => [
39+
// Other service providers...
40+
Craftsys\Laravel\MSGClient91\MSG91ServiceProvider::class,
41+
],
42+
```
43+
44+
If you want to use the facade interface, you can `use` the facade class when needed:
45+
46+
```php
47+
use Craftsys\Laravel\MSGClient91\Facade;
48+
```
49+
50+
Or add an alias in your `config/app.php`
51+
52+
```php
53+
'aliases' => [
54+
// other aliases here
55+
'MSG91' => Craftsys\Laravel\MSGClient91\Facade::class,
56+
],
57+
```
58+
59+
## Configuration
60+
61+
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.
62+
63+
The package can be configured by providing a `msg91` key inside your `config/services.php` configuration file.
64+
65+
```php
66+
<?php
67+
68+
return [
69+
// along with other services
70+
"msg91": [
71+
'key' => env("MSG91_KEY"),
72+
],
73+
];
74+
```
75+
76+
and update the `.env` file to get the desired values e.g. `MSG91_KEY`.
77+
78+
Please visit [msg91-php configuration][client-configuration] for a detailed description about the available options and their default values.
79+
80+
## Usage
81+
82+
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.
83+
84+
```php
85+
MSG91::otp()
86+
->to(919999999999)
87+
->send()
88+
```
89+
90+
Next, follow along with [examples](#examples) to learn more
91+
92+
## Examples
93+
94+
### Managing OTPs
95+
96+
OTP services like sending, verifying, and resending etc, can be accessed via `otp` method on the client instance e.g. `MGS91::otp()`.
97+
98+
> For a detailed usage, please visit [msg91-php's documentation][client-managing-otps] on managing OTPs.
99+
100+
#### Send OTP
101+
102+
```php
103+
MGS91::otp()
104+
->to(912343434312) // phone number with country code
105+
->send(); // send the otp
106+
```
107+
108+
### Verify OTP
109+
110+
```php
111+
MSG91::otp(1234) // OTP to be verified
112+
->to(912343434312) // phone number with country code
113+
->verify(); // Verify
114+
```
115+
116+
### Resend OTP
117+
118+
```php
119+
MSG91::otp()
120+
->to(912343434312) // set the mobile with country code
121+
->via("text") // way of retry
122+
->resend(); // resend otp
123+
```
124+
125+
> For all the examples and options, please consult [msg91-php examples section][client-examples]
126+
127+
[client]: https://github.com/craftsys/msg91-php
128+
[client-configuration]: https://github.com/craftsys/msg91-php#configuration
129+
[client-examples]: https://github.com/craftsys/msg91-php#examples
130+
[client-managing-otps]: https://github.com/craftsys/msg91-php#managing-otps

composer.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "craftsys/msg91-laravel",
3+
"description": "Laravel service provider for MSG91 apis.",
4+
"license": "MIT",
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "Sudhir Mitharwal",
9+
"email": "[email protected]",
10+
"role": "Developer",
11+
"homepage": "http://github.com/sudkumar"
12+
}
13+
],
14+
"keywords": [
15+
"msg91",
16+
"php",
17+
"laravel",
18+
"otp"
19+
],
20+
"homepage": "https://github.com/craftsys/msg91-laravel",
21+
"support": {
22+
"issues": "https://github.com/craftsys/msg91-laravel/issues",
23+
"source": "https://github.com/craftsys/msg91-laravel",
24+
"email": "[email protected]"
25+
},
26+
"require": {
27+
"php": ">=7.1",
28+
"craftsys/msg91-php": "0.4.2",
29+
"illuminate/support": "^6.3"
30+
},
31+
"require-dev": {
32+
"orchestra/testbench": "^4.2",
33+
"phpunit/phpunit": "^8.4"
34+
},
35+
"extra": {
36+
"branch-alias": {
37+
"dev-master": "0.x-dev",
38+
"laravel": {
39+
"aliases": {
40+
"MSG91": "Craftsys\\Laravel\\MSG91\\Facade"
41+
},
42+
"providers": [
43+
"Craftsys\\Laravel\\MSG91\\MSGServiceProvider"
44+
]
45+
}
46+
}
47+
},
48+
"config": {
49+
"optimize-autoloader": true,
50+
"sort-packages": true
51+
},
52+
"prefer-stable": true,
53+
"minimum-stability": "dev",
54+
"autoload": {
55+
"psr-4": {
56+
"Craftsys\\Laravel\\MSG91Client\\": "src/"
57+
}
58+
},
59+
"autoload-dev": {
60+
"psr-4": {
61+
"Craftsys\\Laravel\\MSG91Client\\Test\\": "tests/"
62+
}
63+
},
64+
"scripts": {
65+
"test": "phpunit --colors=always"
66+
}
67+
}

phpunit.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<phpunit colors="true" bootstrap="vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="unit">
4+
<directory>./tests/</directory>
5+
</testsuite>
6+
</testsuites>
7+
<filter>
8+
<whitelist>
9+
<directory suffix=".php">./src/</directory>
10+
</whitelist>
11+
</filter>
12+
</phpunit>

src/Facade.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Craftsys\Laravel\MSG91Client;
4+
5+
use Craftsys\MSG91Client\Client;
6+
use Illuminate\Support\Facades\Facade as BaseFacade;
7+
8+
/**
9+
* Facade for Craftsys\MSG91Client\Client
10+
*
11+
* @method static \Craftsys\MSG91Client\OTPMessage otp(int|null $otp = null)
12+
*/
13+
class Facade extends BaseFacade
14+
{
15+
/**
16+
* Get the registered name of the component.
17+
*
18+
* @return string
19+
*
20+
* @throws \RuntimeException
21+
*/
22+
protected static function getFacadeAccessor()
23+
{
24+
return Client::class;
25+
}
26+
}

src/MSGServiceProvider.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Craftsys\Laravel\MSG91Client;
4+
5+
use Craftsys\MSG91Client\Client;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class MSGServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Register the application services.
12+
*
13+
* @return void
14+
*/
15+
public function register()
16+
{
17+
// Bind MSG91 Client in Service Container.
18+
$this->app->singleton(Client::class, function ($app) {
19+
$config = $app['config'];
20+
return new Client($config->get('services.msg91'));
21+
});
22+
}
23+
24+
/**
25+
* Get the services provided by the provider.
26+
*
27+
* @return array
28+
*/
29+
public function provides()
30+
{
31+
return [Client::class];
32+
}
33+
}

tests/ServiceProviderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Craftsys\Laravel\MSG91Client\Test;
4+
5+
use Craftsys\MSG91Client\Client;
6+
7+
class ServiceProviderTest extends TestCase
8+
{
9+
/**
10+
* Define environment setup.
11+
*
12+
* @param \Illuminate\Foundation\Application $app
13+
*
14+
* @return void
15+
*/
16+
protected function getEnvironmentSetUp($app)
17+
{
18+
$app['config']->set('services.msg91.key', 'my_api_key');
19+
}
20+
21+
/**
22+
* Test that we can create the Nexmo client
23+
* from container binding.
24+
*
25+
* @return void
26+
*/
27+
public function testClientResolutionFromContainer()
28+
{
29+
$client = app(Client::class);
30+
$this->assertInstanceOf(Client::class, $client);
31+
}
32+
}

tests/TestCase.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Craftsys\Laravel\MSG91Client\Test;
4+
5+
use Orchestra\Testbench\TestCase as BaseTestCase;
6+
use Craftsys\Laravel\MSG91Client\MSGServiceProvider;
7+
use Craftsys\Laravel\MSG91Client\Facade;
8+
9+
abstract class TestCase extends BaseTestCase
10+
{
11+
/**
12+
* Get package providers.
13+
*
14+
* @param \Illuminate\Foundation\Application $app
15+
*
16+
* @return array
17+
*/
18+
protected function getPackageProviders($app)
19+
{
20+
return [
21+
MSGServiceProvider::class,
22+
];
23+
}
24+
25+
/**
26+
* Get package aliases.
27+
*
28+
* @param \Illuminate\Foundation\Application $app
29+
*
30+
* @return array
31+
*/
32+
protected function getPackageAliases($app)
33+
{
34+
return [
35+
'MSG91' => Facade::class
36+
];
37+
}
38+
}

0 commit comments

Comments
 (0)