Completed
Push — master ( 587974...ba2473 )
by
unknown
02:21
created

NotifynderServiceProvider::notifications()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 28
rs 8.8571
cc 1
eloc 14
nc 1
nop 0
1
<?php namespace Fenos\Notifynder;
2
3
use Fenos\Notifynder\Artisan\CreateCategory;
4
use Fenos\Notifynder\Artisan\DeleteCategory;
5
use Fenos\Notifynder\Artisan\CreateGroup;
6
use Fenos\Notifynder\Artisan\PushCategoryToGroup;
7
use Fenos\Notifynder\Builder\NotifynderBuilder;
8
use Fenos\Notifynder\Categories\CategoryRepository;
9
use Fenos\Notifynder\Categories\CategoryManager;
10
use Fenos\Notifynder\Contracts\CategoryDB;
11
use Fenos\Notifynder\Contracts\NotificationDB;
12
use Fenos\Notifynder\Contracts\NotifynderCategory;
13
use Fenos\Notifynder\Contracts\NotifynderDispatcher;
14
use Fenos\Notifynder\Contracts\NotifynderGroup;
15
use Fenos\Notifynder\Contracts\NotifynderGroupCategoryDB;
16
use Fenos\Notifynder\Contracts\NotifynderGroupDB;
17
use Fenos\Notifynder\Contracts\NotifynderNotification;
18
use Fenos\Notifynder\Contracts\NotifynderSender;
19
use Fenos\Notifynder\Contracts\NotifynderTranslator;
20
use Fenos\Notifynder\Contracts\StoreNotification;
21
use Fenos\Notifynder\Groups\GroupManager;
22
use Fenos\Notifynder\Groups\GroupCategoryRepository;
23
use Fenos\Notifynder\Groups\GroupRepository;
24
use Fenos\Notifynder\Handler\Dispatcher;
25
use Fenos\Notifynder\Models\Notification;
26
use Fenos\Notifynder\Models\NotificationCategory;
27
use Fenos\Notifynder\Models\NotificationGroup;
28
use Fenos\Notifynder\Notifications\NotificationManager;
29
use Fenos\Notifynder\Notifications\NotificationRepository;
30
use Fenos\Notifynder\Parsers\ArtisanOptionsParser;
31
use Fenos\Notifynder\Parsers\NotifynderParser;
32
use Fenos\Notifynder\Senders\SenderFactory;
33
use Fenos\Notifynder\Senders\SenderManager;
34
use Fenos\Notifynder\Translator\Compiler;
35
use Fenos\Notifynder\Translator\TranslatorManager;
36
use Illuminate\Container\Container;
37
use Illuminate\Support\ServiceProvider;
38
39
class NotifynderServiceProvider extends ServiceProvider
40
{
41
42
    /**
43
     * Register Bindings
44
     */
45
    public function register()
46
    {
47
        $this->notifynder();
48
        $this->senders();
49
        $this->notifications();
50
        $this->categories();
51
        $this->builder();
52
        $this->groups();
53
        $this->translator();
54
        $this->events();
55
        $this->contracts();
56
        $this->artisan();
57
    }
58
59
    /*
60
     * Boot the publishing config
61
     */
62
    public function boot()
63
    {
64
        $this->config();
65
        $this->migration();
66
    }
67
68
    /**
69
     * Bind Notifynder
70
     */
71
    protected function notifynder()
72
    {
73
        $this->app->singleton('notifynder', function ($app) {
74
            return new NotifynderManager(
75
                $app['notifynder.category'],
76
                $app['notifynder.sender'],
77
                $app['notifynder.notification'],
78
                $app['notifynder.dispatcher'],
79
                $app['notifynder.group']
80
            );
81
        });
82
83
        // Register Facade
84
        $this->app->alias('notifynder', 'Notifynder');
85
    }
86
87
    /**
88
     * Bind Notifynder Categories to IoC
89
     */
90
    protected function categories()
91
    {
92
        $this->app->singleton('notifynder.category', function ($app) {
93
            return new CategoryManager(
94
                $app->make('notifynder.category.repository')
95
            );
96
        });
97
98
        $this->app->singleton('notifynder.category.repository', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
            return new CategoryRepository(
100
                new NotificationCategory()
101
            );
102
        });
103
    }
104
105
    /**
106
     * Bind the notifications
107
     */
108
    protected function notifications()
109
    {
110
        $this->app->singleton('notifynder.notification', function ($app) {
111
            return new NotificationManager(
112
                $app['notifynder.notification.repository']
113
            );
114
        });
115
116
        $this->app->singleton('notifynder.notification.repository', function ($app) {
117
118
            $notificationModel = $app['config']->get('notifynder.notification_model');
119
            $notificationIstance = $app->make($notificationModel);
120
121
            return new NotificationRepository(
122
                $notificationIstance,
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 ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 configuraiton 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('CreateNotificationCategories')) {
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
    }
301
302
    /**
303
     * @param string $filename
304
     */
305
    protected function publishMigration($filename)
306
    {
307
        $extension = '.php';
308
        $filename = trim($filename, $extension).$extension;
309
        $stub = __DIR__.'/../migrations/'.$filename;
310
        $target = $this->migrationFilepath($filename);
311
        $this->publishes([$stub => $target], 'migrations');
312
    }
313
314
    /**
315
     * @param string $filename
316
     * @return string
317
     */
318
    protected function migrationFilepath($filename)
319
    {
320
        if(function_exists('database_path')) {
321
            return database_path('/migrations/'.$filename);
322
        } else {
323
            return base_path('/database/migrations/'.$filename);
324
        }
325
    }
326
327
    /**
328
     * Register Artisan commands
329
     */
330
    protected function artisan()
331
    {
332
        // Categories
333
        $this->app->singleton('notifynder.artisan.category-add', function ($app) {
334
            return new CreateCategory(
335
                $app['notifynder.category']
336
            );
337
        });
338
339
        $this->app->singleton('notifynder.artisan.category-delete', function ($app) {
340
            return new DeleteCategory(
341
                $app['notifynder.category']
342
            );
343
        });
344
345
        // Groups
346
        $this->app->singleton('notifynder.artisan.group-add', function ($app) {
347
            return new CreateGroup(
348
                $app['notifynder.group']
349
            );
350
        });
351
352
        $this->app->singleton('notifynder.artisan.group-add-categories', function ($app) {
353
            return new PushCategoryToGroup(
354
                $app['notifynder.group'],
355
                new ArtisanOptionsParser()
356
            );
357
        });
358
359
        // Register commands
360
        $this->commands([
361
            'notifynder.artisan.category-add',
362
            'notifynder.artisan.category-delete',
363
            'notifynder.artisan.group-add',
364
            'notifynder.artisan.group-add-categories',
365
        ]);
366
    }
367
}
368