Skip to content

Commit 5dcd0a1

Browse files
Merge pull request #12 from shailesh-ladumor/develop
Release v0.4.0
2 parents bc16ef5 + 16f0a0c commit 5dcd0a1

8 files changed

+322
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### [v0.4.0](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.4.0) (Jun 21, 2021)
2+
3+
#### Added
4+
* Add User Device Mobile API support [(#12)](https://github.com/shailesh-ladumor/one-signal/pull/12) [(#7a3243d)](https://github.com/shailesh-ladumor/one-signal/commit/7a3243d30ab64e05fed5edea22386a3ea3a6ff74)
5+
* Readme updated
6+
17
### [v0.3.4](https://github.com/shailesh-ladumor/one-signal/releases/tag/v0.3.4) (Dec 25, 2020)
28

39
##### Changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Laravel One Signal is Laravel Wrapper for [One Signal](https://onesignal.com). O
3737
- [View App](#view-app)
3838
- [Create App](#create-app)
3939
- [Update App](#update-app)
40+
- [User Device](#user-device)
4041
- [Change Log](#change-log)
4142
- [License](#license)
4243

@@ -215,6 +216,28 @@ Update a new OneSignal app.
215216

216217
You can check [here](https://documentation.onesignal.com/reference#update-an-app) supported parameters and guide.
217218

219+
## User Device
220+
You can generate a User Device APIs with just one command,
221+
222+
`php artisan one-signal.userDevice:publish`
223+
224+
this command generate following files,
225+
226+
* UserDeviceAPIController
227+
* UserDeviceAPIRepository
228+
* UserDevice (model)
229+
* Migration
230+
231+
Also, do not forget to add following routes in to the `api.php` file.
232+
233+
```angular2html
234+
use App\Http\Controllers\API\UserDeviceAPIController;
235+
```
236+
237+
```
238+
Route::post('user-device/register', [UserDeviceAPIController::class, 'registerDevice']);
239+
Route::get('user-device/{playerId}/update-status', [UserDeviceAPIController::class, 'updateNotificationStatus']);
240+
```
218241

219242
### Change Log
220243
Please see [Change Log](CHANGELOG.md) here

src/OneSignalServiceProvider.php

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ladumor\OneSignal;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Ladumor\OneSignal\commands\PublishUserDevice;
67

78
class OneSignalServiceProvider extends ServiceProvider
89
{
@@ -31,5 +32,13 @@ public function register()
3132
$this->app->singleton('one-signal', function ($app) {
3233
return new OneSignalManager();
3334
});
35+
36+
$this->app->singleton('one-signal.userDevice:publish', function ($app) {
37+
return new PublishUserDevice();
38+
});
39+
40+
$this->commands([
41+
'one-signal.userDevice:publish',
42+
]);
3443
}
3544
}

src/commands/PublishUserDevice.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Ladumor\OneSignal\commands;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Illuminate\Console\Command;
7+
8+
class PublishUserDevice extends Command
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'one-signal.userDevice:publish';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Publish Migration|Controller|Service of User Device APIs';
23+
24+
public $composer;
25+
/**
26+
* Create a new command instance.
27+
*/
28+
public function __construct()
29+
{
30+
parent::__construct();
31+
32+
$this->composer = app()['composer'];
33+
}
34+
public function handle()
35+
{
36+
$controllerDir = app_path('Http/Controllers/API');
37+
if (!File::isDirectory($controllerDir)) {
38+
File::makeDirectory($controllerDir);
39+
}
40+
41+
$controllerTemplate = file_get_contents(__DIR__.'/../stubs/UserDeviceAPIController.stub');
42+
$this->createFile($controllerDir. DIRECTORY_SEPARATOR, 'UserDeviceAPIController.php', $controllerTemplate);
43+
$this->info('UserDeviceController published.');
44+
45+
$modelDir = app_path('Models');
46+
47+
if (!File::isDirectory($modelDir)) {
48+
File::makeDirectory($modelDir);
49+
}
50+
51+
$modelTemplate = file_get_contents(__DIR__.'/../stubs/UserDevice.stub');
52+
$this->createFile($modelDir. DIRECTORY_SEPARATOR, 'UserDevice.php', $modelTemplate);
53+
$this->info('UserDevice published.');
54+
55+
$repoDir = app_path('Repositories');
56+
57+
if (!File::isDirectory($repoDir)) {
58+
File::makeDirectory($repoDir);
59+
}
60+
61+
$repoTemplate = file_get_contents(__DIR__.'/../stubs/UserDeviceRepository.stub');
62+
$this->createFile($repoDir. DIRECTORY_SEPARATOR, 'UserDeviceRepository.php', $repoTemplate);
63+
$this->info('UserDeviceRepository published.');
64+
65+
$fileName = date('Y_m_d_His').'_'.'create_user_device_table.php';
66+
67+
$repoTemplate = file_get_contents(__DIR__.'/../stubs/CreateUserDeviceTable.stub');
68+
$this->createFile(base_path('database/migrations/'), $fileName, $repoTemplate);
69+
$this->info('UserDevice migration created.');
70+
71+
$this->info('Generating autoload files');
72+
$this->composer->dumpOptimized();
73+
74+
if ($this->confirm("\nDo you want to migrate database? [y|N]", false)) {
75+
$this->call('migrate');
76+
}
77+
78+
$this->info('Greeting From Shailesh Ladumor!');
79+
}
80+
81+
public static function createFile($path, $fileName, $contents)
82+
{
83+
if (!file_exists($path)) {
84+
mkdir($path, 0755, true);
85+
}
86+
87+
$path = $path.$fileName;
88+
89+
file_put_contents($path, $contents);
90+
}
91+
}

src/stubs/CreateUserDeviceTable.stub

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateUserDevicesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('user_devices', function (Blueprint $table) {
17+
$table->id();
18+
$table->unsignedBigInteger('user_id');
19+
$table->string('device_type');
20+
$table->string('os_player_id')->unique();
21+
$table->boolean('is_active')->default(true);
22+
$table->timestamps();
23+
24+
$table->foreign('user_id')
25+
->references('id')
26+
->on('users')
27+
->onDelete('CASCADE')
28+
->onUpdate('CASCADE');
29+
});
30+
}
31+
32+
/**
33+
* Reverse the migrations.
34+
*
35+
* @return void
36+
*/
37+
public function down()
38+
{
39+
Schema::dropIfExists('user_devices');
40+
}
41+
}

src/stubs/UserDevice.stub

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Carbon;
8+
9+
/**
10+
* App\Models\UserDevice
11+
*
12+
* @property int $id
13+
* @property int $user_id
14+
* @property string $device_type
15+
* @property string $os_player_id
16+
* @property int $is_active
17+
* @property Carbon|null $created_at
18+
* @property Carbon|null $updated_at
19+
* @method static Builder|UserDevice newModelQuery()
20+
* @method static Builder|UserDevice newQuery()
21+
* @method static Builder|UserDevice query()
22+
* @method static Builder|UserDevice whereCreatedAt($value)
23+
* @method static Builder|UserDevice whereDeviceType($value)
24+
* @method static Builder|UserDevice whereId($value)
25+
* @method static Builder|UserDevice whereIsActive($value)
26+
* @method static Builder|UserDevice whereOsPlayerId($value)
27+
* @method static Builder|UserDevice whereUpdatedAt($value)
28+
* @method static Builder|UserDevice whereUserId($value)
29+
* @mixin \Eloquent
30+
*/
31+
class UserDevice extends Model
32+
{
33+
public $table = 'user_devices';
34+
35+
public $fillable = [
36+
'user_id',
37+
'device_type',
38+
'os_player_id',
39+
'is_active',
40+
];
41+
}
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\API;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
use App\Repositories\UserDeviceRepository;
8+
use Illuminate\Http\JsonResponse;
9+
use Illuminate\Support\Facades\Response;
10+
11+
/**
12+
* Class UserDeviceAPIController
13+
*/
14+
class UserDeviceAPIController extends Controller
15+
{
16+
/** @var UserDeviceRepository */
17+
public $userDeviceRepo;
18+
19+
/**
20+
* UserDeviceAPIController constructor.
21+
* @param UserDeviceRepository $userDeviceRepo
22+
*/
23+
public function __construct(UserDeviceRepository $userDeviceRepo)
24+
{
25+
$this->userDeviceRepo = $userDeviceRepo;
26+
}
27+
28+
/**
29+
* @param Request $request
30+
*
31+
* @return JsonResponse
32+
*/
33+
public function registerDevice(Request $request)
34+
{
35+
$this->userDeviceRepo->updateOrCreate($request->all());
36+
37+
return $this->sendSuccess('The device has been registered successfully.');
38+
}
39+
40+
/**
41+
* @param $playerId
42+
*
43+
* @return JsonResponse
44+
*/
45+
public function updateNotificationStatus($playerId)
46+
{
47+
$this->userDeviceRepo->updateStatus($playerId);
48+
49+
return $this->sendSuccess('The notification status has been updated successfully.');
50+
}
51+
52+
/**
53+
* @param $message
54+
*
55+
* @return JsonResponse
56+
*/
57+
private function sendSuccess($message)
58+
{
59+
return Response::json([
60+
'success' => true,
61+
'message' => $message
62+
], 200);
63+
}
64+
}

src/stubs/UserDeviceRepository.stub

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Repositories;
4+
5+
use App\Models\UserDevice;
6+
use Exception;
7+
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
8+
9+
/**
10+
* Class RegisterDeviceRepository
11+
*/
12+
class UserDeviceRepository
13+
{
14+
15+
/**
16+
* @param $input
17+
*
18+
* @return UserDevice
19+
*/
20+
public function updateOrCreate($input)
21+
{
22+
try {
23+
return UserDevice::updateOrCreate([
24+
'os_player_id' => $input['os_player_id'],
25+
], [
26+
'user_id' => auth()->user()->id,
27+
'os_player_id' => $input['os_player_id'],
28+
'device_type' => $input['device_type']
29+
]);
30+
} catch (Exception $e) {
31+
throw new UnprocessableEntityHttpException($e->getMessage());
32+
}
33+
}
34+
35+
/**
36+
* @param $playerId
37+
*
38+
* @return bool
39+
*/
40+
public function updateStatus($playerId): bool
41+
{
42+
$userDevice = UserDevice::whereOsPlayerId($playerId)->first();
43+
$userDevice->update(['is_active' => !$userDevice->is_active]);
44+
45+
return true;
46+
}
47+
}

0 commit comments

Comments
 (0)