1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LBHurtado\Missive; |
4
|
|
|
|
5
|
|
|
use Opis\Events\EventDispatcher; |
6
|
|
|
use LBHurtado\Missive\Routing\Router; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use LBHurtado\Missive\Container\LaravelContainer; |
9
|
|
|
use LBHurtado\Missive\Models\{SMS, Contact, Relay, Airtime, Topup}; |
10
|
|
|
use Illuminate\Database\Eloquent\Factory as EloquentFactory; |
11
|
|
|
use LBHurtado\Missive\Repositories\{SMSRepository, SMSRepositoryEloquent}; |
12
|
|
|
use LBHurtado\Missive\Repositories\{RelayRepository, RelayRepositoryEloquent}; |
13
|
|
|
use LBHurtado\Missive\Repositories\{ContactRepository, ContactRepositoryEloquent}; |
14
|
|
|
use LBHurtado\Missive\Repositories\{AirtimeRepository, AirtimeRepositoryEloquent}; |
15
|
|
|
use LBHurtado\Missive\Observers\{SMSObserver, ContactObserver, RelayObserver, AirtimeObserver, |
16
|
|
|
AirtimeContactObserver}; |
17
|
|
|
|
18
|
|
|
class MissiveServiceProvider extends ServiceProvider |
19
|
|
|
{ |
20
|
|
|
const APPLICATION_ROUTE_SMS = 'routes/sms.php'; |
21
|
|
|
const APPLICATION_AIRTIME_SEEDER = 'seeds/AirtimeSeeder.php'; |
22
|
|
|
|
23
|
|
|
const PACKAGE_ROUTE_API = __DIR__.'/../routes/api.php'; |
24
|
|
|
const PACKAGE_ROUTE_SMS = __DIR__.'/../routes/sms.php'; |
25
|
|
|
const PACKAGE_FACTORY_DIR = __DIR__ . '/../database/factories'; |
26
|
|
|
const PACKAGE_MISSIVE_CONFIG = __DIR__.'/../config/config.php'; |
27
|
|
|
const PACKAGE_AIRTIME_SEEDER = __DIR__.'/../database/seeds/AirtimeSeeder.php'; |
28
|
|
|
const PACKAGE_TACTICIAN_FIELDS_CONFIG = __DIR__ . '/../config/tactician.fields.php'; |
29
|
|
|
const PACKAGE_SMSS_TABLE_MIGRATION_STUB = __DIR__.'/../database/migrations/create_s_m_s_s_table.php.stub'; |
30
|
|
|
const PACKAGE_RELAYS_TABLE_MIGRATION_STUB = __DIR__.'/../database/migrations/create_relays_table.php.stub'; |
31
|
|
|
const PACKAGE_CONTACTS_TABLE_MIGRATION_STUB = __DIR__.'/../database/migrations/create_contacts_table.php.stub'; |
32
|
|
|
const PACKAGE_AIRTIMES_TABLE_MIGRATION_STUB = __DIR__.'/../database/migrations/create_airtimes_table.php.stub'; |
33
|
|
|
const PACKAGE_TOPUPS_TABLE_MIGRATION_STUB = __DIR__.'/../database/migrations/create_topups_table.php.stub'; |
34
|
|
|
|
35
|
|
|
public function boot() |
36
|
|
|
{ |
37
|
|
|
$this->observeModels(); |
38
|
|
|
$this->publishConfigs(); |
39
|
|
|
$this->publishMigrations(); |
40
|
|
|
$this->publishSeeds(); |
41
|
|
|
$this->publishRoutes(); |
42
|
|
|
$this->mapFactories(); |
43
|
|
|
$this->mapRoutes(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function register() |
47
|
|
|
{ |
48
|
|
|
$this->registerConfigs(); |
49
|
|
|
$this->registerRepositories(); |
50
|
|
|
$this->registerModels(); |
51
|
|
|
$this->registerPivots(); |
52
|
|
|
$this->registerFacades(); |
53
|
|
|
$this->registerClasses(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function observeModels() |
57
|
|
|
{ |
58
|
|
|
app('missive.sms')::observe(SMSObserver::class); |
59
|
|
|
app('missive.relay')::observe(RelayObserver::class); |
60
|
|
|
app('missive.contact')::observe(ContactObserver::class); |
61
|
|
|
app('missive.airtime')::observe(AirtimeObserver::class); |
62
|
|
|
app('missive.airtime_contact')::observe(AirtimeContactObserver::class); |
63
|
|
|
//TODO: create topup observer |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function publishConfigs() |
67
|
|
|
{ |
68
|
|
|
if ($this->app->runningInConsole()) { |
69
|
|
|
$this->publishes([ |
70
|
|
|
self::PACKAGE_MISSIVE_CONFIG => config_path('missive.php'), |
71
|
|
|
], 'missive-config'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function publishMigrations() |
76
|
|
|
{ |
77
|
|
|
if ($this->app->runningInConsole()) { |
78
|
|
View Code Duplication |
if (! class_exists(CreateRelaysTable::class)) { |
|
|
|
|
79
|
|
|
$this->publishes([ |
80
|
|
|
self::PACKAGE_RELAYS_TABLE_MIGRATION_STUB => database_path('migrations/'.date('Y_m_d_His', time()).'_create_relays_table.php'), |
81
|
|
|
], 'missive-migrations'); |
82
|
|
|
} |
83
|
|
View Code Duplication |
if (! class_exists(CreateSMSsTable::class)) { |
|
|
|
|
84
|
|
|
$this->publishes([ |
85
|
|
|
self::PACKAGE_SMSS_TABLE_MIGRATION_STUB => database_path('migrations/'.date('Y_m_d_His', time()).'_create_s_m_s_s_table.php'), |
86
|
|
|
], 'missive-migrations'); |
87
|
|
|
} |
88
|
|
View Code Duplication |
if (! class_exists(CreateContactsTable::class)) { |
|
|
|
|
89
|
|
|
$this->publishes([ |
90
|
|
|
self::PACKAGE_CONTACTS_TABLE_MIGRATION_STUB => database_path('migrations/'.date('Y_m_d_His', time()).'_create_contacts_table.php'), |
91
|
|
|
], 'missive-migrations'); |
92
|
|
|
} |
93
|
|
View Code Duplication |
if (! class_exists(CreateAirtimesTable::class)) { |
|
|
|
|
94
|
|
|
$this->publishes([ |
95
|
|
|
self::PACKAGE_AIRTIMES_TABLE_MIGRATION_STUB => database_path('migrations/'.date('Y_m_d_His', time()+60).'_create_airtimes_table.php'), |
96
|
|
|
], 'missive-migrations'); |
97
|
|
|
} |
98
|
|
View Code Duplication |
if (! class_exists(CreateTopupsTable::class)) { |
|
|
|
|
99
|
|
|
$this->publishes([ |
100
|
|
|
self::PACKAGE_TOPUPS_TABLE_MIGRATION_STUB => database_path('migrations/'.date('Y_m_d_His', time()+60).'_create_topups_table.php'), |
101
|
|
|
], 'missive-migrations'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function publishSeeds() |
107
|
|
|
{ |
108
|
|
|
if ($this->app->runningInConsole()) { |
109
|
|
|
$this->publishes([ |
110
|
|
|
self::PACKAGE_AIRTIME_SEEDER => database_path(self::APPLICATION_AIRTIME_SEEDER), |
111
|
|
|
], 'missive-seeds'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function publishRoutes() |
116
|
|
|
{ |
117
|
|
|
if ($this->app->runningInConsole()) { |
118
|
|
|
$this->publishes([ |
119
|
|
|
self::PACKAGE_ROUTE_SMS => base_path(self::APPLICATION_ROUTE_SMS), |
120
|
|
|
], 'missive-routes'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function registerConfigs() |
125
|
|
|
{ |
126
|
|
|
$this->mergeConfigFrom(self::PACKAGE_MISSIVE_CONFIG, 'missive'); |
127
|
|
|
$this->mergeConfigFrom(self::PACKAGE_TACTICIAN_FIELDS_CONFIG, 'tactician.fields'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function registerRepositories() |
131
|
|
|
{ |
132
|
|
|
$this->app->bind(SMSRepository::class, SMSRepositoryEloquent::class); |
133
|
|
|
$this->app->bind(RelayRepository::class, RelayRepositoryEloquent::class); |
134
|
|
|
$this->app->bind(ContactRepository::class, ContactRepositoryEloquent::class); |
135
|
|
|
$this->app->bind(AirtimeRepository::class, AirtimeRepositoryEloquent::class); |
136
|
|
|
//TODO: create topup repository |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function registerModels() |
140
|
|
|
{ |
141
|
|
|
$this->app->singleton('missive.sms', function () { |
142
|
|
|
$class = config('missive.classes.models.sms', SMS::class); |
143
|
|
|
return new $class; |
144
|
|
|
}); |
145
|
|
|
$this->app->singleton('missive.relay', function () { |
146
|
|
|
$class = config('missive.classes.models.relay', Relay::class); |
147
|
|
|
return new $class; |
148
|
|
|
}); |
149
|
|
|
$this->app->singleton('missive.contact', function () { |
150
|
|
|
$class = config('missive.classes.models.contact', Contact::class); |
151
|
|
|
return new $class; |
152
|
|
|
}); |
153
|
|
|
$this->app->singleton('missive.airtime', function () { |
154
|
|
|
$class = config('missive.classes.models.airtime', Airtime::class); |
155
|
|
|
return new $class; |
156
|
|
|
}); |
157
|
|
|
$this->app->singleton('missive.topup', function () { |
158
|
|
|
$class = config('missive.classes.models.topup', Topup::class); |
159
|
|
|
return new $class; |
160
|
|
|
}); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function registerPivots() |
164
|
|
|
{ |
165
|
|
|
$this->app->singleton('missive.airtime_contact', function () { |
166
|
|
|
$class = config('missive.classes.pivots.airtime_contact', Airtime::class); |
167
|
|
|
return new $class; |
168
|
|
|
}); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function registerFacades() |
172
|
|
|
{ |
173
|
|
|
$this->app->singleton('missive', function () { |
174
|
|
|
return new Missive(app(AirtimeRepository::class)); |
175
|
|
|
}); |
176
|
|
|
$this->app->singleton('missive:router', function () { |
177
|
|
|
$router = new Router(app(Missive::class)); |
178
|
|
|
$router->setContainer(new LaravelContainer($this->app)); |
179
|
|
|
|
180
|
|
|
return $router; |
181
|
|
|
}); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function registerClasses() |
185
|
|
|
{ |
186
|
|
|
$this->app->singleton(Router::class); |
187
|
|
|
$this->app->singleton(EventDispatcher::class); |
188
|
|
|
$this->app->singleton(Missive::class, function ($app) { |
189
|
|
|
return $app->make('missive'); |
190
|
|
|
}); |
191
|
|
|
$this->app->singleton(Router::class, function ($app) { |
192
|
|
|
return $app->make('missive:router'); |
193
|
|
|
}); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function mapFactories() |
197
|
|
|
{ |
198
|
|
|
$this->app->make(EloquentFactory::class)->load(self::PACKAGE_FACTORY_DIR); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function mapRoutes() |
202
|
|
|
{ |
203
|
|
|
$this->loadRoutesFrom(self::PACKAGE_ROUTE_API); |
204
|
|
|
$file = base_path(self::APPLICATION_ROUTE_SMS); |
205
|
|
|
if (file_exists($file)) include_once $file; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.