Completed
Push — master ( 8a4d2e...bcb7d2 )
by
unknown
8s
created

NotifynderServiceProvider::migration()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 27
rs 3
cc 9
eloc 17
nc 256
nop 0
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 ($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...
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
119
            $notificationModel = $app['config']->get('notifynder.notification_model');
120
            $notificationIstance = $app->make($notificationModel);
121
122
            return new NotificationRepository(
123
                $notificationIstance,
124
                $app['db']
125
            );
126
        });
127
128
        // Inject configs when model is resolved
129
        $this->app->resolving(Notification::class, function (Notification $object, $app) {
130
            $fillable = $app['config']->get('notifynder.additional_fields.fillable', []);
131
            $object->fillable(array_merge($object->getFillable(), $fillable));
132
        });
133
134
        // Default store notification
135
        $this->app->bind('notifynder.store', 'notifynder.notification.repository');
136
    }
137
138
    /**
139
     * Bind Translator.
140
     */
141
    protected function translator()
142
    {
143
        $this->app->singleton('notifynder.translator', function ($app) {
144
            return new TranslatorManager(
145
                $app['notifynder.translator.compiler'],
146
                $app['config']
147
            );
148
        });
149
150
        $this->app->singleton('notifynder.translator.compiler', function ($app) {
151
            return new Compiler(
152
                $app['filesystem.disk']
153
            );
154
        });
155
    }
156
157
    /**
158
     * Bind Senders.
159
     */
160
    protected function senders()
161
    {
162
        $this->app->singleton('notifynder.sender', function ($app) {
163
             return new SenderManager(
164
                 $app['notifynder.sender.factory'],
165
                 $app['notifynder.store'],
166
                 $app[Container::class]
167
             );
168
        });
169
170
        $this->app->singleton('notifynder.sender.factory', function ($app) {
171
            return new SenderFactory(
172
                $app['notifynder.group'],
173
                $app['notifynder.category']
174
            );
175
        });
176
    }
177
178
    /**
179
     * Bind Dispatcher.
180
     */
181
    protected function events()
182
    {
183
        $this->app->singleton('notifynder.dispatcher', function ($app) {
184
            return new Dispatcher(
185
                $app['events']
186
            );
187
        });
188
    }
189
190
    /**
191
     * Bind Groups.
192
     */
193
    protected function groups()
194
    {
195
        $this->app->singleton('notifynder.group', function ($app) {
196
            return new GroupManager(
197
                $app['notifynder.group.repository'],
198
                $app['notifynder.group.category']
199
            );
200
        });
201
202
        $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...
203
            return new GroupRepository(
204
                new NotificationGroup()
205
            );
206
        });
207
208
        $this->app->singleton('notifynder.group.category', function ($app) {
209
            return new GroupCategoryRepository(
210
                $app['notifynder.category'],
211
                new NotificationGroup()
212
            );
213
        });
214
    }
215
216
    /**
217
     * Bind Builder.
218
     */
219
    protected function builder()
220
    {
221
        $this->app->singleton('notifynder.builder', function ($app) {
222
            return new NotifynderBuilder(
223
                $app['notifynder.category']
224
            );
225
        });
226
227
        $this->app->resolving(NotifynderBuilder::class, function (NotifynderBuilder $object, $app) {
228
            $object->setConfig($app['config']);
229
        });
230
    }
231
232
    /**
233
     * Contracts of notifynder.
234
     */
235
    protected function contracts()
236
    {
237
        // Notifynder
238
        $this->app->bind(Notifynder::class, 'notifynder');
239
240
        // Repositories
241
        $this->app->bind(CategoryDB::class, 'notifynder.category.repository');
242
        $this->app->bind(NotificationDB::class, 'notifynder.notification.repository');
243
        $this->app->bind(NotifynderGroupDB::class, 'notifynder.group.repository');
244
        $this->app->bind(NotifynderGroupCategoryDB::class, 'notifynder.group.category');
245
246
        // Main Classes
247
        $this->app->bind(NotifynderCategory::class, 'notifynder.category');
248
        $this->app->bind(NotifynderNotification::class, 'notifynder.notification');
249
        $this->app->bind(NotifynderTranslator::class, 'notifynder.translator');
250
        $this->app->bind(NotifynderGroup::class, 'notifynder.group');
251
252
        // Store notifications
253
        $this->app->bind(StoreNotification::class, 'notifynder.store');
254
        $this->app->bind(NotifynderSender::class, 'notifynder.sender');
255
        $this->app->bind(NotifynderDispatcher::class, 'notifynder.dispatcher');
256
    }
257
258
    /**
259
     * Publish config files.
260
     */
261
    protected function config()
262
    {
263
        $this->publishes([
264
            __DIR__.'/../config/notifynder.php' => config_path('notifynder.php'),
265
        ]);
266
267
        $this->mergeConfigFrom(__DIR__.'/../config/notifynder.php', 'notifynder');
268
269
        // Set use strict_extra config option,
270
        // you can toggle it in the configuraiton file
271
        $strictParam = $this->app['config']->get('notifynder.strict_extra', false);
272
        NotifynderParser::setStrictExtra($strictParam);
273
    }
274
275
    /**
276
     * Publish migration files.
277
     */
278
    protected function migration()
279
    {
280
        if (! class_exists('NotificationCategories')) {
281
            $this->publishMigration('2014_02_10_145728_notification_categories');
282
        }
283
        if (! class_exists('CreateNotificationGroupsTable')) {
284
            $this->publishMigration('2014_08_01_210813_create_notification_groups_table');
285
        }
286
        if (! class_exists('CreateNotificationCategoryNotificationGroupTable')) {
287
            $this->publishMigration('2014_08_01_211045_create_notification_category_notification_group_table');
288
        }
289
        if (! class_exists('CreateNotificationsTable')) {
290
            $this->publishMigration('2015_05_05_212549_create_notifications_table');
291
        }
292
        if (! class_exists('AddExpireTimeColumnToNotificationTable')) {
293
            $this->publishMigration('2015_06_06_211555_add_expire_time_column_to_notification_table');
294
        }
295
        if (! class_exists('ChangeTypeToExtraInNotificationsTable')) {
296
            $this->publishMigration('2015_06_06_211555_change_type_to_extra_in_notifications_table');
297
        }
298
        if (! class_exists('AlterCategoryNameToUnique')) {
299
            $this->publishMigration('2015_06_07_211555_alter_category_name_to_unique');
300
        }
301
        if (! class_exists('MakeNotificationUrlNullable')) {
302
            $this->publishMigration('2016_04_19_200827_make_notification_url_nullable');
303
        }
304
    }
305
306
    /**
307
     * @param string $filename
308
     */
309
    protected function publishMigration($filename)
310
    {
311
        $extension = '.php';
312
        $filename = trim($filename, $extension).$extension;
313
        $stub = __DIR__.'/../migrations/'.$filename;
314
        $target = $this->migrationFilepath($filename);
315
        $this->publishes([$stub => $target], 'migrations');
316
    }
317
318
    /**
319
     * @param string $filename
320
     * @return string
321
     */
322
    protected function migrationFilepath($filename)
323
    {
324
        if (function_exists('database_path')) {
325
            return database_path('/migrations/'.$filename);
326
        } else {
327
            return base_path('/database/migrations/'.$filename);
328
        }
329
    }
330
331
    /**
332
     * Register Artisan commands.
333
     */
334
    protected function artisan()
335
    {
336
        // Categories
337
        $this->app->singleton('notifynder.artisan.category-add', function ($app) {
338
            return new CreateCategory(
339
                $app['notifynder.category']
340
            );
341
        });
342
343
        $this->app->singleton('notifynder.artisan.category-delete', function ($app) {
344
            return new DeleteCategory(
345
                $app['notifynder.category']
346
            );
347
        });
348
349
        // Groups
350
        $this->app->singleton('notifynder.artisan.group-add', function ($app) {
351
            return new CreateGroup(
352
                $app['notifynder.group']
353
            );
354
        });
355
356
        $this->app->singleton('notifynder.artisan.group-add-categories', function ($app) {
357
            return new PushCategoryToGroup(
358
                $app['notifynder.group'],
359
                new ArtisanOptionsParser()
360
            );
361
        });
362
363
        // Register commands
364
        $this->commands([
365
            'notifynder.artisan.category-add',
366
            'notifynder.artisan.category-delete',
367
            'notifynder.artisan.group-add',
368
            'notifynder.artisan.group-add-categories',
369
        ]);
370
    }
371
}
372