Completed
Pull Request — master (#106)
by Fabrizio
04:32 queued 02:13
created

NotifynderServiceProvider::notifications()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 27
rs 8.8571
cc 1
eloc 13
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
            $object->setConfig($app['config']);
129
        });
130
131
        // Default store notification
132
        $this->app->bind('notifynder.store', 'notifynder.notification.repository');
133
    }
134
135
    /**
136
     * Bind Translator
137
     */
138
    protected function translator()
139
    {
140
        $this->app->singleton('notifynder.translator', function ($app) {
141
            return new TranslatorManager(
142
                $app['notifynder.translator.compiler'],
143
                $app['config']
144
            );
145
        });
146
147
        $this->app->singleton('notifynder.translator.compiler', function ($app) {
148
            return new Compiler(
149
                $app['filesystem.disk']
150
            );
151
        });
152
    }
153
154
    /**
155
     * Bind Senders
156
     */
157
    protected function senders()
158
    {
159
        $this->app->singleton('notifynder.sender', function ($app) {
160
             return new SenderManager(
161
                 $app['notifynder.sender.factory'],
162
                 $app['notifynder.store'],
163
                 $app[Container::class]
164
             );
165
        });
166
167
        $this->app->singleton('notifynder.sender.factory', function ($app) {
168
            return new SenderFactory(
169
                $app['notifynder.group'],
170
                $app['notifynder.category']
171
            );
172
        });
173
    }
174
175
    /**
176
     * Bind Dispatcher
177
     */
178
    protected function events()
179
    {
180
        $this->app->singleton('notifynder.dispatcher', function ($app) {
181
            return new Dispatcher(
182
                $app['events']
183
            );
184
        });
185
    }
186
187
    /**
188
     * Bind Groups
189
     */
190
    protected function groups()
191
    {
192
        $this->app->singleton('notifynder.group', function ($app) {
193
            return new GroupManager(
194
                $app['notifynder.group.repository'],
195
                $app['notifynder.group.category']
196
            );
197
        });
198
199
        $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...
200
            return new GroupRepository(
201
                new NotificationGroup()
202
            );
203
        });
204
205
        $this->app->singleton('notifynder.group.category', function ($app) {
206
            return new GroupCategoryRepository(
207
                $app['notifynder.category'],
208
                new NotificationGroup()
209
            );
210
        });
211
    }
212
213
    /**
214
     * Bind Builder
215
     */
216
    protected function builder()
217
    {
218
        $this->app->singleton('notifynder.builder', function ($app) {
219
            return new NotifynderBuilder(
220
                $app['notifynder.category']
221
            );
222
        });
223
224
        $this->app->resolving(NotifynderBuilder::class, function (NotifynderBuilder $object, $app) {
225
            $object->setConfig($app['config']);
226
        });
227
    }
228
229
    /**
230
     * Contracts of notifynder
231
     */
232
    protected function contracts()
233
    {
234
        // Notifynder
235
        $this->app->bind(Notifynder::class, 'notifynder');
236
237
        // Repositories
238
        $this->app->bind(CategoryDB::class, 'notifynder.category.repository');
239
        $this->app->bind(NotificationDB::class, 'notifynder.notification.repository');
240
        $this->app->bind(NotifynderGroupDB::class, 'notifynder.group.repository');
241
        $this->app->bind(NotifynderGroupCategoryDB::class, 'notifynder.group.category');
242
243
        // Main Classes
244
        $this->app->bind(NotifynderCategory::class, 'notifynder.category');
245
        $this->app->bind(NotifynderNotification::class, 'notifynder.notification');
246
        $this->app->bind(NotifynderTranslator::class, 'notifynder.translator');
247
        $this->app->bind(NotifynderGroup::class, 'notifynder.group');
248
249
        // Store notifications
250
        $this->app->bind(StoreNotification::class, 'notifynder.store');
251
        $this->app->bind(NotifynderSender::class, 'notifynder.sender');
252
        $this->app->bind(NotifynderDispatcher::class, 'notifynder.dispatcher');
253
    }
254
255
    /**
256
     * Publish config files
257
     */
258
    protected function config()
259
    {
260
        $this->publishes([
261
            __DIR__.'/../config/notifynder.php' => config_path('notifynder.php'),
262
            __DIR__.'/../migrations/' => base_path('/database/migrations'),
263
        ]);
264
265
        $this->mergeConfigFrom(__DIR__.'/../config/notifynder.php', 'notifynder');
266
267
        // Set use strict_extra config option,
268
        // you can toggle it in the configuraiton file
269
        $strictParam = $this->app['config']->get('notifynder.strict_extra',false);
270
        NotifynderParser::setStrictExtra($strictParam);
271
    }
272
273
    /**
274
     * Register Artisan commands
275
     */
276
    protected function artisan()
277
    {
278
        // Categories
279
        $this->app->singleton('notifynder.artisan.category-add', function ($app) {
280
            return new CreateCategory(
281
                $app['notifynder.category']
282
            );
283
        });
284
285
        $this->app->singleton('notifynder.artisan.category-delete', function ($app) {
286
            return new DeleteCategory(
287
                $app['notifynder.category']
288
            );
289
        });
290
291
        // Groups
292
        $this->app->singleton('notifynder.artisan.group-add', function ($app) {
293
            return new CreateGroup(
294
                $app['notifynder.group']
295
            );
296
        });
297
298
        $this->app->singleton('notifynder.artisan.group-add-categories', function ($app) {
299
            return new PushCategoryToGroup(
300
                $app['notifynder.group'],
301
                new ArtisanOptionsParser()
302
            );
303
        });
304
305
        // Register commands
306
        $this->commands([
307
            'notifynder.artisan.category-add',
308
            'notifynder.artisan.category-delete',
309
            'notifynder.artisan.group-add',
310
            'notifynder.artisan.group-add-categories',
311
        ]);
312
    }
313
}
314