Completed
Push — master ( 938b6e...587974 )
by
unknown
02:26
created

NotifynderServiceProvider::migrationFilepath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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