|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Usamamuneerchaudhary\Notifier; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Log; |
|
6
|
|
|
use Illuminate\Support\Facades\Route; |
|
7
|
|
|
use Spatie\LaravelPackageTools\Package; |
|
8
|
|
|
use Spatie\LaravelPackageTools\PackageServiceProvider; |
|
9
|
|
|
use Usamamuneerchaudhary\Notifier\Commands\CleanupAnalyticsCommand; |
|
10
|
|
|
use Usamamuneerchaudhary\Notifier\Commands\NotifierInstallCommand; |
|
11
|
|
|
use Usamamuneerchaudhary\Notifier\Commands\SendTestNotificationCommand; |
|
12
|
|
|
use Usamamuneerchaudhary\Notifier\Http\Controllers\NotificationPreferenceController; |
|
13
|
|
|
use Usamamuneerchaudhary\Notifier\Http\Controllers\NotificationTrackingController; |
|
14
|
|
|
use Usamamuneerchaudhary\Notifier\Models\Notification; |
|
15
|
|
|
use Usamamuneerchaudhary\Notifier\Models\NotificationChannel; |
|
16
|
|
|
use Usamamuneerchaudhary\Notifier\Models\NotificationEvent; |
|
17
|
|
|
use Usamamuneerchaudhary\Notifier\Models\NotificationPreference; |
|
18
|
|
|
use Usamamuneerchaudhary\Notifier\Models\NotificationTemplate; |
|
19
|
|
|
use Usamamuneerchaudhary\Notifier\Services\AnalyticsService; |
|
20
|
|
|
use Usamamuneerchaudhary\Notifier\Services\ChannelDriverFactory; |
|
21
|
|
|
use Usamamuneerchaudhary\Notifier\Services\NotificationRepository; |
|
22
|
|
|
use Usamamuneerchaudhary\Notifier\Services\NotifierManager; |
|
23
|
|
|
use Usamamuneerchaudhary\Notifier\Services\PreferenceService; |
|
24
|
|
|
use Usamamuneerchaudhary\Notifier\Services\UrlTrackingService; |
|
25
|
|
|
|
|
26
|
|
|
class NotifierServiceProvider extends PackageServiceProvider |
|
27
|
|
|
{ |
|
28
|
|
|
public static string $name = 'notifier'; |
|
29
|
|
|
|
|
30
|
|
|
public function configurePackage(Package $package): void |
|
31
|
|
|
{ |
|
32
|
|
|
$package |
|
33
|
|
|
->name(static::$name) |
|
34
|
|
|
->hasConfigFile() |
|
35
|
|
|
->hasViews() |
|
36
|
|
|
->hasMigrations() |
|
37
|
|
|
->hasCommands([ |
|
38
|
|
|
NotifierInstallCommand::class, |
|
39
|
|
|
SendTestNotificationCommand::class, |
|
40
|
|
|
CleanupAnalyticsCommand::class, |
|
41
|
|
|
]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function packageRegistered(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->app->singleton('notifier', function () { |
|
47
|
|
|
return new NotifierManager(); |
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
|
|
$this->app->bind('notifier.channel', NotificationChannel::class); |
|
51
|
|
|
$this->app->bind('notifier.event', NotificationEvent::class); |
|
52
|
|
|
$this->app->bind('notifier.template', NotificationTemplate::class); |
|
53
|
|
|
$this->app->bind('notifier.preference', NotificationPreference::class); |
|
54
|
|
|
$this->app->bind('notifier.notification', Notification::class); |
|
55
|
|
|
|
|
56
|
|
|
$this->app->singleton(PreferenceService::class); |
|
57
|
|
|
$this->app->singleton(AnalyticsService::class); |
|
58
|
|
|
$this->app->singleton(UrlTrackingService::class); |
|
59
|
|
|
$this->app->singleton(NotificationRepository::class); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function packageBooted(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->loadMigrationsFrom(__DIR__.'/../database/migrations'); |
|
65
|
|
|
|
|
66
|
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views', 'notifier'); |
|
67
|
|
|
|
|
68
|
|
|
$this->registerChannelsFromDatabase(); |
|
69
|
|
|
$this->registerApiRoutes(); |
|
70
|
|
|
$this->registerTrackingRoutes(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Register API routes for user preferences |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function registerApiRoutes(): void |
|
77
|
|
|
{ |
|
78
|
|
|
Route::prefix('api/notifier/preferences')->group(function () { |
|
79
|
|
|
Route::get('/', [NotificationPreferenceController::class, 'index']); |
|
80
|
|
|
Route::get('/available', [NotificationPreferenceController::class, 'available']); |
|
81
|
|
|
Route::get('/{eventKey}', [NotificationPreferenceController::class, 'show']); |
|
82
|
|
|
Route::put('/{eventKey}', [NotificationPreferenceController::class, 'update']); |
|
83
|
|
|
}); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Register tracking routes for analytics ---- public routes |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function registerTrackingRoutes(): void |
|
90
|
|
|
{ |
|
91
|
|
|
Route::prefix('notifier/track')->group(function () { |
|
92
|
|
|
Route::get('/open/{token}', [NotificationTrackingController::class, 'trackOpen']); |
|
93
|
|
|
Route::get('/click/{token}', [NotificationTrackingController::class, 'trackClick']); |
|
94
|
|
|
}); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function registerChannelsFromDatabase(): void |
|
98
|
|
|
{ |
|
99
|
|
|
try { |
|
100
|
|
|
if (!\Illuminate\Support\Facades\Schema::hasTable('notifier_channels')) { |
|
101
|
|
|
return; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$notifier = $this->app->make('notifier'); |
|
105
|
|
|
$channels = \Usamamuneerchaudhary\Notifier\Models\NotificationChannel::where('is_active', true)->get(); |
|
106
|
|
|
|
|
107
|
|
|
$driverFactory = new ChannelDriverFactory(); |
|
108
|
|
|
foreach ($channels as $channel) { |
|
109
|
|
|
$driver = $driverFactory->create($channel->type); |
|
110
|
|
|
if ($driver) { |
|
111
|
|
|
$notifier->registerChannel($channel->type, $driver); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} catch (\Exception $e) { |
|
115
|
|
|
Log::info($e->getMessage()); |
|
116
|
|
|
// Silently fail |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|