1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\ExpoPushNotifications; |
4
|
|
|
|
5
|
|
|
use ExponentPhpSDK\Expo; |
6
|
|
|
use ExponentPhpSDK\ExpoRegistrar; |
7
|
|
|
use ExponentPhpSDK\ExpoRepository; |
8
|
|
|
use ExponentPhpSDK\Repositories\ExpoFileDriver; |
9
|
|
|
use Illuminate\Support\ServiceProvider; |
10
|
|
|
use NotificationChannels\ExpoPushNotifications\Repositories\ExpoDatabaseDriver; |
11
|
|
|
|
12
|
|
|
class ExpoPushNotificationsServiceProvider extends ServiceProvider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Bootstrap the application services. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
23 |
|
public function boot() |
20
|
|
|
{ |
21
|
23 |
|
$this->setupConfig(); |
22
|
|
|
|
23
|
23 |
|
$repository = $this->getInterestsDriver(); |
24
|
|
|
|
25
|
23 |
|
$this->shouldPublishMigrations($repository); |
|
|
|
|
26
|
|
|
|
27
|
23 |
|
$this->app->when(ExpoChannel::class) |
28
|
23 |
|
->needs(Expo::class) |
29
|
|
|
->give(function () use ($repository) { |
30
|
|
|
return new Expo(new ExpoRegistrar($repository)); |
31
|
23 |
|
}); |
32
|
|
|
|
33
|
23 |
|
$router = $this->app['router']; |
34
|
23 |
|
$router->middlewareGroup('expo.middleware', config('exponent-push-notifications')['middleware']); |
35
|
|
|
|
36
|
23 |
|
$this->loadRoutesFrom(__DIR__.'/Http/routes.php'); |
37
|
23 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Register the application services. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
23 |
|
public function register() |
45
|
|
|
{ |
46
|
23 |
|
$this->app->bind(ExpoRepository::class, get_class($this->getInterestsDriver())); |
47
|
23 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the Expo repository driver based on config. |
51
|
|
|
* |
52
|
|
|
* @return ExpoRepository |
53
|
|
|
*/ |
54
|
23 |
|
public function getInterestsDriver() |
55
|
|
|
{ |
56
|
23 |
|
$driver = config('exponent-push-notifications.interests.driver'); |
57
|
|
|
|
58
|
23 |
|
switch ($driver) { |
59
|
23 |
|
case 'database': |
60
|
|
|
return new ExpoDatabaseDriver(); |
61
|
|
|
break; |
|
|
|
|
62
|
|
|
default: |
63
|
23 |
|
return new ExpoFileDriver(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Publishes the configuration files for the package. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
23 |
|
protected function setupConfig() |
73
|
|
|
{ |
74
|
23 |
|
$this->publishes([ |
75
|
23 |
|
__DIR__.'/../config/exponent-push-notifications.php' => config_path('exponent-push-notifications.php'), |
76
|
23 |
|
], 'config'); |
77
|
|
|
|
78
|
23 |
|
$this->mergeConfigFrom(__DIR__.'/../config/exponent-push-notifications.php', 'exponent-push-notifications'); |
79
|
23 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Publishes the migration files needed in the package. |
83
|
|
|
* |
84
|
|
|
* @param ExpoRepository $repository |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
23 |
|
private function shouldPublishMigrations(ExpoRepository $repository) |
89
|
|
|
{ |
90
|
23 |
|
if ($repository instanceof ExpoDatabaseDriver && ! class_exists('CreateExponentPushNotificationInterestsTable')) { |
91
|
|
|
$timestamp = date('Y_m_d_His', time()); |
92
|
|
|
$this->publishes([ |
93
|
|
|
__DIR__.'/../migrations/create_exponent_push_notification_interests_table.php.stub' => database_path("/migrations/{$timestamp}_create_exponent_push_notification_interests_table.php"), |
94
|
|
|
], 'migrations'); |
95
|
|
|
} |
96
|
23 |
|
} |
97
|
|
|
} |
98
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.