Completed
Pull Request — master (#106)
by Fabrizio
02:14
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
    }
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
        // Inject configs when model is resolved
127
        $this->app->resolving(Notification::class, function (Notification $object, $app) {
128
            $fillable = $app['config']->get('notifynder.additional_fields.fillable');
129
            $object->fillable(array_merge($object->getFillable(),$fillable));
130
        });
131
132
        // Default store notification
133
        $this->app->bind('notifynder.store', 'notifynder.notification.repository');
134
    }
135
136
    /**
137
     * Bind Translator
138
     */
139
    protected function translator()
140
    {
141
        $this->app->singleton('notifynder.translator', function ($app) {
142
            return new TranslatorManager(
143
                $app['notifynder.translator.compiler'],
144
                $app['config']
145
            );
146
        });
147
148
        $this->app->singleton('notifynder.translator.compiler', function ($app) {
149
            return new Compiler(
150
                $app['filesystem.disk']
151
            );
152
        });
153
    }
154
155
    /**
156
     * Bind Senders
157
     */
158
    protected function senders()
159
    {
160
        $this->app->singleton('notifynder.sender', function ($app) {
161
             return new SenderManager(
162
                 $app['notifynder.sender.factory'],
163
                 $app['notifynder.store'],
164
                 $app[Container::class]
165
             );
166
        });
167
168
        $this->app->singleton('notifynder.sender.factory', function ($app) {
169
            return new SenderFactory(
170
                $app['notifynder.group'],
171
                $app['notifynder.category']
172
            );
173
        });
174
    }
175
176
    /**
177
     * Bind Dispatcher
178
     */
179
    protected function events()
180
    {
181
        $this->app->singleton('notifynder.dispatcher', function ($app) {
182
            return new Dispatcher(
183
                $app['events']
184
            );
185
        });
186
    }
187
188
    /**
189
     * Bind Groups
190
     */
191
    protected function groups()
192
    {
193
        $this->app->singleton('notifynder.group', function ($app) {
194
            return new GroupManager(
195
                $app['notifynder.group.repository'],
196
                $app['notifynder.group.category']
197
            );
198
        });
199
200
        $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...
201
            return new GroupRepository(
202
                new NotificationGroup()
203
            );
204
        });
205
206
        $this->app->singleton('notifynder.group.category', function ($app) {
207
            return new GroupCategoryRepository(
208
                $app['notifynder.category'],
209
                new NotificationGroup()
210
            );
211
        });
212
    }
213
214
    /**
215
     * Bind Builder
216
     */
217
    protected function builder()
218
    {
219
        $this->app->singleton('notifynder.builder', function ($app) {
220
            return new NotifynderBuilder(
221
                $app['notifynder.category']
222
            );
223
        });
224
225
        $this->app->resolving(NotifynderBuilder::class, function (NotifynderBuilder $object, $app) {
226
            $object->setConfig($app['config']);
227
        });
228
    }
229
230
    /**
231
     * Contracts of notifynder
232
     */
233
    protected function contracts()
234
    {
235
        // Notifynder
236
        $this->app->bind(Notifynder::class, 'notifynder');
237
238
        // Repositories
239
        $this->app->bind(CategoryDB::class, 'notifynder.category.repository');
240
        $this->app->bind(NotificationDB::class, 'notifynder.notification.repository');
241
        $this->app->bind(NotifynderGroupDB::class, 'notifynder.group.repository');
242
        $this->app->bind(NotifynderGroupCategoryDB::class, 'notifynder.group.category');
243
244
        // Main Classes
245
        $this->app->bind(NotifynderCategory::class, 'notifynder.category');
246
        $this->app->bind(NotifynderNotification::class, 'notifynder.notification');
247
        $this->app->bind(NotifynderTranslator::class, 'notifynder.translator');
248
        $this->app->bind(NotifynderGroup::class, 'notifynder.group');
249
250
        // Store notifications
251
        $this->app->bind(StoreNotification::class, 'notifynder.store');
252
        $this->app->bind(NotifynderSender::class, 'notifynder.sender');
253
        $this->app->bind(NotifynderDispatcher::class, 'notifynder.dispatcher');
254
    }
255
256
    /**
257
     * Publish config files
258
     */
259
    protected function config()
260
    {
261
        $this->publishes([
262
            __DIR__.'/../config/notifynder.php' => config_path('notifynder.php'),
263
            __DIR__.'/../migrations/' => base_path('/database/migrations'),
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
     * Register Artisan commands
276
     */
277
    protected function artisan()
278
    {
279
        // Categories
280
        $this->app->singleton('notifynder.artisan.category-add', function ($app) {
281
            return new CreateCategory(
282
                $app['notifynder.category']
283
            );
284
        });
285
286
        $this->app->singleton('notifynder.artisan.category-delete', function ($app) {
287
            return new DeleteCategory(
288
                $app['notifynder.category']
289
            );
290
        });
291
292
        // Groups
293
        $this->app->singleton('notifynder.artisan.group-add', function ($app) {
294
            return new CreateGroup(
295
                $app['notifynder.group']
296
            );
297
        });
298
299
        $this->app->singleton('notifynder.artisan.group-add-categories', function ($app) {
300
            return new PushCategoryToGroup(
301
                $app['notifynder.group'],
302
                new ArtisanOptionsParser()
303
            );
304
        });
305
306
        // Register commands
307
        $this->commands([
308
            'notifynder.artisan.category-add',
309
            'notifynder.artisan.category-delete',
310
            'notifynder.artisan.group-add',
311
            'notifynder.artisan.group-add-categories',
312
        ]);
313
    }
314
}
315