Skip to content

Commit 5b32ef7

Browse files
committed
Merge branch 'patrickkivits-feature-database-driver'
2 parents bad6622 + 62cd23a commit 5b32ef7

16 files changed

+512
-50
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ You can install the package via composer:
2828
composer require alymosul/laravel-exponent-push-notifications
2929
```
3030

31-
You must install the service provider:
31+
If you are using Laravel 5.5 or higher this package will automatically register itself using [Package Discovery](https://laravel.com/docs/5.5/packages#package-discovery). For older versions of Laravel you must install the service provider manually:
3232

3333
```php
3434
// config/app.php
@@ -38,6 +38,39 @@ You must install the service provider:
3838
],
3939
```
4040

41+
You can publish the migration with:
42+
```bash
43+
php artisan vendor:publish --provider="NotificationChannels\ExpoPushNotifications\ExpoPushNotificationsServiceProvider" --tag="migrations"
44+
```
45+
46+
After publishing the migration you can create the `exponent_push_notification_interests` table by running the migrations:
47+
48+
```bash
49+
php artisan migrate
50+
```
51+
52+
You can optionally publish the config file with:
53+
```bash
54+
php artisan vendor:publish --provider="NotificationChannels\ExpoPushNotifications\ExpoPushNotificationsServiceProvider" --tag="config"
55+
```
56+
57+
This is the contents of the published config file:
58+
59+
```php
60+
return [
61+
'interests' => [
62+
/*
63+
* Supported: "file", "database"
64+
*/
65+
'driver' => env('EXPONENT_PUSH_NOTIFICATION_INTERESTS_STORAGE_DRIVER', 'file'),
66+
67+
'database' => [
68+
'table_name' => 'exponent_push_notification_interests',
69+
],
70+
]
71+
];
72+
```
73+
4174
## Usage
4275

4376
``` php

composer.json

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@
1212
],
1313
"require": {
1414
"php": ">=7.0",
15-
"alymosul/exponent-server-sdk-php": "1.0.*",
16-
"illuminate/notifications": "5.3.* || 5.4.* || 5.5.* || 5.6.*",
17-
"illuminate/support": "5.3.* || 5.4.* || 5.5.* || 5.6.*",
18-
"illuminate/events": "5.3.* || 5.4.* || 5.5.* || 5.6.*",
19-
"illuminate/queue": "5.3.* || 5.4.* || 5.5.* || 5.6.*",
20-
"illuminate/http": "5.3.* || 5.4.* || 5.5.* || 5.6.*",
21-
"illuminate/routing": "5.3.* || 5.4.* || 5.5.* || 5.6.*",
22-
"illuminate/validation": "5.3.* || 5.4.* || 5.5.* || 5.6.*",
23-
"illuminate/auth": "5.3.* || 5.4.* || 5.5.* || 5.6.*"
15+
"alymosul/exponent-server-sdk-php": "1.1.*",
16+
"illuminate/config": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
17+
"illuminate/notifications": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
18+
"illuminate/support": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
19+
"illuminate/events": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
20+
"illuminate/queue": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
21+
"illuminate/http": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
22+
"illuminate/routing": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
23+
"illuminate/validation": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*",
24+
"illuminate/auth": "5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.*"
2425
},
2526
"require-dev": {
2627
"mockery/mockery": "^0.9.5",
27-
"phpunit/phpunit": "~6.0"
28+
"phpunit/phpunit": "~5.4||~5.7||~6.0||^7.0",
29+
"orchestra/testbench": "~3.3||~3.4||~3.5||~3.6||~3.7"
2830
},
2931
"autoload": {
3032
"psr-4": {
@@ -41,5 +43,12 @@
4143
},
4244
"config": {
4345
"sort-packages": true
46+
},
47+
"extra": {
48+
"laravel": {
49+
"providers": [
50+
"NotificationChannels\\ExpoPushNotifications\\ExpoPushNotificationsServiceProvider"
51+
]
52+
}
4453
}
4554
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* Here you may define the configuration for the expo-notifications-driver.
5+
* The expo-notifications-driver can guide the sdk to use `database` or `file` repositories.
6+
* The database repository uses the same configuration for the database in your Laravel app.
7+
*/
8+
9+
return [
10+
'interests' => [
11+
'driver' => env('EXPONENT_PUSH_NOTIFICATION_INTERESTS_STORAGE_DRIVER', 'file'),
12+
13+
'database' => [
14+
'table_name' => 'exponent_push_notification_interests',
15+
],
16+
],
17+
];
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateExponentPushNotificationInterestsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up()
13+
{
14+
Schema::create(config('exponent-push-notifications.interests.database.table_name'), function (Blueprint $table) {
15+
$table->string('key')->index();
16+
$table->string('value');
17+
18+
$table->unique(['key','value']);
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*/
25+
public function down()
26+
{
27+
Schema::drop(config('exponent-push-notifications.interests.database.table_name'));
28+
}
29+
}

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<logging>
2323
<log type="tap" target="build/report.tap"/>
2424
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
25+
<log type="coverage-html" target="build/coverage"/>
2626
<log type="coverage-text" target="build/coverage.txt"/>
2727
<log type="coverage-clover" target="build/logs/clover.xml"/>
2828
</logging>

src/ExpoChannel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace NotificationChannels\ExpoPushNotifications;
44

55
use ExponentPhpSDK\Expo;
6-
use Illuminate\Events\Dispatcher;
76
use Illuminate\Notifications\Notification;
7+
use Illuminate\Contracts\Events\Dispatcher;
88
use ExponentPhpSDK\Exceptions\ExpoException;
99
use Illuminate\Notifications\Events\NotificationFailed;
1010
use NotificationChannels\ExpoPushNotifications\Exceptions\CouldNotSendNotification;

src/ExpoPushNotificationsServiceProvider.php

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,88 @@
77
use ExponentPhpSDK\ExpoRepository;
88
use Illuminate\Support\ServiceProvider;
99
use ExponentPhpSDK\Repositories\ExpoFileDriver;
10+
use NotificationChannels\ExpoPushNotifications\Repositories\ExpoDatabaseDriver;
1011

1112
class ExpoPushNotificationsServiceProvider extends ServiceProvider
1213
{
1314
/**
1415
* Bootstrap the application services.
16+
*
17+
* @return void
1518
*/
1619
public function boot()
1720
{
21+
$this->setupConfig();
22+
23+
$repository = $this->getInterestsDriver();
24+
25+
$this->shouldPublishMigrations($repository);
26+
1827
$this->app->when(ExpoChannel::class)
1928
->needs(Expo::class)
20-
->give(function () {
21-
return new Expo(new ExpoRegistrar(new ExpoFileDriver()));
29+
->give(function () use ($repository) {
30+
return new Expo(new ExpoRegistrar($repository));
2231
});
2332

24-
//Load routes
2533
$this->loadRoutesFrom(__DIR__.'/Http/routes.php');
2634
}
2735

2836
/**
2937
* Register the application services.
38+
*
39+
* @return void
3040
*/
3141
public function register()
3242
{
33-
$this->app->bind(ExpoRepository::class, ExpoFileDriver::class);
43+
$this->app->bind(ExpoRepository::class, get_class($this->getInterestsDriver()));
44+
}
45+
46+
/**
47+
* Gets the Expo repository driver based on config.
48+
*
49+
* @return ExpoRepository
50+
*/
51+
public function getInterestsDriver()
52+
{
53+
$driver = config('exponent-push-notifications.interests.driver');
54+
55+
switch ($driver) {
56+
case 'database':
57+
return new ExpoDatabaseDriver();
58+
break;
59+
default:
60+
return new ExpoFileDriver();
61+
}
62+
}
63+
64+
/**
65+
* Publishes the configuration files for the package.
66+
*
67+
* @return void
68+
*/
69+
protected function setupConfig()
70+
{
71+
$this->publishes([
72+
__DIR__ . '/../config/exponent-push-notifications.php' => config_path('exponent-push-notifications.php'),
73+
], 'config');
74+
75+
$this->mergeConfigFrom(__DIR__.'/../config/exponent-push-notifications.php', 'exponent-push-notifications');
76+
}
77+
78+
/**
79+
* Publishes the migration files needed in the package.
80+
*
81+
* @param ExpoRepository $repository
82+
*
83+
* @return void
84+
*/
85+
private function shouldPublishMigrations(ExpoRepository $repository)
86+
{
87+
if ($repository instanceof ExpoDatabaseDriver && !class_exists('CreateExponentPushNotificationInterestsTable')) {
88+
$timestamp = date('Y_m_d_His', time());
89+
$this->publishes([
90+
__DIR__ . '/../migrations/create_exponent_push_notification_interests_table.php.stub' => database_path("/migrations/{$timestamp}_create_exponent_push_notification_interests_table.php"),
91+
], 'migrations');
92+
}
3493
}
3594
}

src/Http/ExpoController.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,31 @@ public function subscribe(Request $request)
7272
/**
7373
* Handles removing subscription endpoint for the authenticated interest.
7474
*
75-
* @return \Illuminate\Http\JsonResponse
75+
* @param Request $request
76+
*
77+
* @return JsonResponse
7678
*/
77-
public function unsubscribe()
79+
public function unsubscribe(Request $request)
7880
{
7981
$interest = $this->expoChannel->interestName(Auth::user());
8082

83+
$validator = Validator::make($request->all(), [
84+
'expo_token' => 'sometimes|string',
85+
]);
86+
87+
if ($validator->fails()) {
88+
return JsonResponse::create([
89+
'status' => 'failed',
90+
'error' => [
91+
'message' => 'Expo Token is invalid',
92+
],
93+
], 422);
94+
}
95+
96+
$token = $request->get('expo_token') ?: null;
97+
8198
try {
82-
$deleted = $this->expoChannel->expo->unsubscribe($interest);
99+
$deleted = $this->expoChannel->expo->unsubscribe($interest, $token);
83100
} catch (\Exception $e) {
84101
return JsonResponse::create([
85102
'status' => 'failed',

src/Http/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
Route::group(['prefix' => 'api/exponent/devices', 'middleware' => 'auth:api, bindings'], function () {
3+
Route::group(['prefix' => 'api/exponent/devices', 'middleware' => ['auth:api', 'bindings']], function () {
44
Route::post('subscribe', [
55
'as' => 'register-interest',
66
'uses' => 'NotificationChannels\ExpoPushNotifications\Http\ExpoController@subscribe',

src/Models/Interest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace NotificationChannels\ExpoPushNotifications\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Interest extends Model
8+
{
9+
/**
10+
* The associated table.
11+
*
12+
* @var string
13+
*/
14+
protected $table;
15+
16+
/**
17+
* The attributes that are mass assignable.
18+
*
19+
* @var array
20+
*/
21+
protected $fillable = [
22+
'key',
23+
'value',
24+
];
25+
26+
/**
27+
* Indicates if the model should be timestamped.
28+
*
29+
* @var bool
30+
*/
31+
public $timestamps = false;
32+
33+
/**
34+
* Interest constructor.
35+
*
36+
* @param array $attributes
37+
*/
38+
public function __construct(array $attributes = [])
39+
{
40+
$this->table = config('exponent-push-notifications.interests.database.table_name');
41+
42+
parent::__construct($attributes);
43+
}
44+
}

0 commit comments

Comments
 (0)