Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

NotifynderServiceProvider   C

Complexity

Total Complexity 26

Size/Duplication

Total Lines 333
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 25

Importance

Changes 21
Bugs 9 Features 4
Metric Value
wmc 26
c 21
b 9
f 4
lcom 1
cbo 25
dl 0
loc 333
rs 5

16 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 13 1
A boot() 0 5 1
A notifynder() 0 15 1
A categories() 0 14 1
A translator() 0 15 1
A senders() 0 17 1
A events() 0 8 1
A groups() 0 22 1
A builder() 0 12 1
A config() 0 13 1
A contracts() 0 22 1
F migration() 0 30 10
A publishMigration() 0 8 1
A migrationFilepath() 0 8 2
B artisan() 0 37 1
B notifications() 0 27 1
1
<?php
2
3
namespace Fenos\Notifynder;
4
5
use Fenos\Notifynder\Artisan\CreateCategory;
6
use Fenos\Notifynder\Artisan\DeleteCategory;
7
use Fenos\Notifynder\Artisan\CreateGroup;
8
use Fenos\Notifynder\Artisan\PushCategoryToGroup;
9
use Fenos\Notifynder\Builder\NotifynderBuilder;
10
use Fenos\Notifynder\Categories\CategoryRepository;
11
use Fenos\Notifynder\Categories\CategoryManager;
12
use Fenos\Notifynder\Contracts\CategoryDB;
13
use Fenos\Notifynder\Contracts\NotificationDB;
14
use Fenos\Notifynder\Contracts\NotifynderCategory;
15
use Fenos\Notifynder\Contracts\NotifynderDispatcher;
16
use Fenos\Notifynder\Contracts\NotifynderGroup;
17
use Fenos\Notifynder\Contracts\NotifynderGroupCategoryDB;
18
use Fenos\Notifynder\Contracts\NotifynderGroupDB;
19
use Fenos\Notifynder\Contracts\NotifynderNotification;
20
use Fenos\Notifynder\Contracts\NotifynderSender;
21
use Fenos\Notifynder\Contracts\NotifynderTranslator;
22
use Fenos\Notifynder\Contracts\StoreNotification;
23
use Fenos\Notifynder\Groups\GroupManager;
24
use Fenos\Notifynder\Groups\GroupCategoryRepository;
25
use Fenos\Notifynder\Groups\GroupRepository;
26
use Fenos\Notifynder\Handler\Dispatcher;
27
use Fenos\Notifynder\Models\Notification;
28
use Fenos\Notifynder\Models\NotificationCategory;
29
use Fenos\Notifynder\Models\NotificationGroup;
30
use Fenos\Notifynder\Notifications\NotificationManager;
31
use Fenos\Notifynder\Notifications\NotificationRepository;
32
use Fenos\Notifynder\Parsers\ArtisanOptionsParser;
33
use Fenos\Notifynder\Parsers\NotifynderParser;
34
use Fenos\Notifynder\Senders\SenderFactory;
35
use Fenos\Notifynder\Senders\SenderManager;
36
use Fenos\Notifynder\Translator\Compiler;
37
use Fenos\Notifynder\Translator\TranslatorManager;
38
use Illuminate\Container\Container;
39
use Illuminate\Support\ServiceProvider;
40
41
class NotifynderServiceProvider extends ServiceProvider
42
{
43
    /**
44
     * Register Bindings.
45
     */
46
    public function register()
47
    {
48
        $this->notifynder();
49
        $this->senders();
50
        $this->notifications();
51
        $this->categories();
52
        $this->builder();
53
        $this->groups();
54
        $this->translator();
55
        $this->events();
56
        $this->contracts();
57
        $this->artisan();
58
    }
59
60
    /*
61
     * Boot the publishing config
62
     */
63
    public function boot()
64
    {
65
        $this->config();
66
        $this->migration();
67
    }
68
69
    /**
70
     * Bind Notifynder.
71
     */
72
    protected function notifynder()
73
    {
74
        $this->app->singleton('notifynder', function ($app) {
75
            return new NotifynderManager(
76
                $app['notifynder.category'],
77
                $app['notifynder.sender'],
78
                $app['notifynder.notification'],
79
                $app['notifynder.dispatcher'],
80
                $app['notifynder.group']
81
            );
82
        });
83
84
        // Register Facade
85
        $this->app->alias('notifynder', 'Notifynder');
86
    }
87
88
    /**
89
     * Bind Notifynder Categories to IoC.
90
     */
91
    protected function categories()
92
    {
93
        $this->app->singleton('notifynder.category', function ($app) {
94
            return new CategoryManager(
95
                $app->make('notifynder.category.repository')
96
            );
97
        });
98
99
        $this->app->singleton('notifynder.category.repository', function () {
100
            return new CategoryRepository(
101
                new NotificationCategory()
102
            );
103
        });
104
    }
105
106
    /**
107
     * Bind the notifications.
108
     */
109
    protected function notifications()
110
    {
111
        $this->app->singleton('notifynder.notification', function ($app) {
112
            return new NotificationManager(
113
                $app['notifynder.notification.repository']
114
            );
115
        });
116
117
        $this->app->singleton('notifynder.notification.repository', function ($app) {
118
            $notificationModel = $app['config']->get('notifynder.notification_model');
119
            $notificationInstance = $app->make($notificationModel);
120
121
            return new NotificationRepository(
122
                $notificationInstance,
123
                $app['db']
124
            );
125
        });
126
127
        // Inject configs when model is resolved
128
        $this->app->resolving(Notification::class, function (Notification $object, $app) {
129
            $fillable = $app['config']->get('notifynder.additional_fields.fillable', []);
130
            $object->fillable(array_merge($object->getFillable(), $fillable));
131
        });
132
133
        // Default store notification
134
        $this->app->bind('notifynder.store', 'notifynder.notification.repository');
135
    }
136
137
    /**
138
     * Bind Translator.
139
     */
140
    protected function translator()
141
    {
142
        $this->app->singleton('notifynder.translator', function ($app) {
143
            return new TranslatorManager(
144
                $app['notifynder.translator.compiler'],
145
                $app['config']
146
            );
147
        });
148
149
        $this->app->singleton('notifynder.translator.compiler', function ($app) {
150
            return new Compiler(
151
                $app['filesystem.disk']
152
            );
153
        });
154
    }
155
156
    /**
157
     * Bind Senders.
158
     */
159
    protected function senders()
160
    {
161
        $this->app->singleton('notifynder.sender', function ($app) {
162
            return new SenderManager(
163
                 $app['notifynder.sender.factory'],
164
                 $app['notifynder.store'],
165
                 $app[Container::class]
166
             );
167
        });
168
169
        $this->app->singleton('notifynder.sender.factory', function ($app) {
170
            return new SenderFactory(
171
                $app['notifynder.group'],
172
                $app['notifynder.category']
173
            );
174
        });
175
    }
176
177
    /**
178
     * Bind Dispatcher.
179
     */
180
    protected function events()
181
    {
182
        $this->app->singleton('notifynder.dispatcher', function ($app) {
183
            return new Dispatcher(
184
                $app['events']
185
            );
186
        });
187
    }
188
189
    /**
190
     * Bind Groups.
191
     */
192
    protected function groups()
193
    {
194
        $this->app->singleton('notifynder.group', function ($app) {
195
            return new GroupManager(
196
                $app['notifynder.group.repository'],
197
                $app['notifynder.group.category']
198
            );
199
        });
200
201
        $this->app->singleton('notifynder.group.repository', function () {
202
            return new GroupRepository(
203
                new NotificationGroup()
204
            );
205
        });
206
207
        $this->app->singleton('notifynder.group.category', function ($app) {
208
            return new GroupCategoryRepository(
209
                $app['notifynder.category'],
210
                new NotificationGroup()
211
            );
212
        });
213
    }
214
215
    /**
216
     * Bind Builder.
217
     */
218
    protected function builder()
219
    {
220
        $this->app->singleton('notifynder.builder', function ($app) {
221
            return new NotifynderBuilder(
222
                $app['notifynder.category']
223
            );
224
        });
225
226
        $this->app->resolving(NotifynderBuilder::class, function (NotifynderBuilder $object, $app) {
227
            $object->setConfig($app['config']);
228
        });
229
    }
230
231
    /**
232
     * Contracts of notifynder.
233
     */
234
    protected function contracts()
235
    {
236
        // Notifynder
237
        $this->app->bind(Notifynder::class, 'notifynder');
238
239
        // Repositories
240
        $this->app->bind(CategoryDB::class, 'notifynder.category.repository');
241
        $this->app->bind(NotificationDB::class, 'notifynder.notification.repository');
242
        $this->app->bind(NotifynderGroupDB::class, 'notifynder.group.repository');
243
        $this->app->bind(NotifynderGroupCategoryDB::class, 'notifynder.group.category');
244
245
        // Main Classes
246
        $this->app->bind(NotifynderCategory::class, 'notifynder.category');
247
        $this->app->bind(NotifynderNotification::class, 'notifynder.notification');
248
        $this->app->bind(NotifynderTranslator::class, 'notifynder.translator');
249
        $this->app->bind(NotifynderGroup::class, 'notifynder.group');
250
251
        // Store notifications
252
        $this->app->bind(StoreNotification::class, 'notifynder.store');
253
        $this->app->bind(NotifynderSender::class, 'notifynder.sender');
254
        $this->app->bind(NotifynderDispatcher::class, 'notifynder.dispatcher');
255
    }
256
257
    /**
258
     * Publish config files.
259
     */
260
    protected function config()
261
    {
262
        $this->publishes([
263
            __DIR__.'/../config/notifynder.php' => config_path('notifynder.php'),
264
        ]);
265
266
        $this->mergeConfigFrom(__DIR__.'/../config/notifynder.php', 'notifynder');
267
268
        // Set use strict_extra config option,
269
        // you can toggle it in the configuration file
270
        $strictParam = $this->app['config']->get('notifynder.strict_extra', false);
271
        NotifynderParser::setStrictExtra($strictParam);
272
    }
273
274
    /**
275
     * Publish migration files.
276
     */
277
    protected function migration()
278
    {
279
        if (! class_exists('NotificationCategories')) {
280
            $this->publishMigration('2014_02_10_145728_notification_categories');
281
        }
282
        if (! class_exists('CreateNotificationGroupsTable')) {
283
            $this->publishMigration('2014_08_01_210813_create_notification_groups_table');
284
        }
285
        if (! class_exists('CreateNotificationCategoryNotificationGroupTable')) {
286
            $this->publishMigration('2014_08_01_211045_create_notification_category_notification_group_table');
287
        }
288
        if (! class_exists('CreateNotificationsTable')) {
289
            $this->publishMigration('2015_05_05_212549_create_notifications_table');
290
        }
291
        if (! class_exists('AddExpireTimeColumnToNotificationTable')) {
292
            $this->publishMigration('2015_06_06_211555_add_expire_time_column_to_notification_table');
293
        }
294
        if (! class_exists('ChangeTypeToExtraInNotificationsTable')) {
295
            $this->publishMigration('2015_06_06_211555_change_type_to_extra_in_notifications_table');
296
        }
297
        if (! class_exists('AlterCategoryNameToUnique')) {
298
            $this->publishMigration('2015_06_07_211555_alter_category_name_to_unique');
299
        }
300
        if (! class_exists('MakeNotificationUrlNullable')) {
301
            $this->publishMigration('2016_04_19_200827_make_notification_url_nullable');
302
        }
303
        if (! class_exists('AddStackIdToNotifications')) {
304
            $this->publishMigration('2016_05_19_144531_add_stack_id_to_notifications');
305
        }
306
    }
307
308
    /**
309
     * @param string $filename
310
     */
311
    protected function publishMigration($filename)
312
    {
313
        $extension = '.php';
314
        $filename = trim($filename, $extension).$extension;
315
        $stub = __DIR__.'/../migrations/'.$filename;
316
        $target = $this->migrationFilepath($filename);
317
        $this->publishes([$stub => $target], 'migrations');
318
    }
319
320
    /**
321
     * @param string $filename
322
     * @return string
323
     */
324
    protected function migrationFilepath($filename)
325
    {
326
        if (function_exists('database_path')) {
327
            return database_path('/migrations/'.$filename);
328
        } else {
329
            return base_path('/database/migrations/'.$filename);
330
        }
331
    }
332
333
    /**
334
     * Register Artisan commands.
335
     */
336
    protected function artisan()
337
    {
338
        // Categories
339
        $this->app->singleton('notifynder.artisan.category-add', function ($app) {
340
            return new CreateCategory(
341
                $app['notifynder.category']
342
            );
343
        });
344
345
        $this->app->singleton('notifynder.artisan.category-delete', function ($app) {
346
            return new DeleteCategory(
347
                $app['notifynder.category']
348
            );
349
        });
350
351
        // Groups
352
        $this->app->singleton('notifynder.artisan.group-add', function ($app) {
353
            return new CreateGroup(
354
                $app['notifynder.group']
355
            );
356
        });
357
358
        $this->app->singleton('notifynder.artisan.group-add-categories', function ($app) {
359
            return new PushCategoryToGroup(
360
                $app['notifynder.group'],
361
                new ArtisanOptionsParser()
362
            );
363
        });
364
365
        // Register commands
366
        $this->commands([
367
            'notifynder.artisan.category-add',
368
            'notifynder.artisan.category-delete',
369
            'notifynder.artisan.group-add',
370
            'notifynder.artisan.group-add-categories',
371
        ]);
372
    }
373
}
374