NotifynderServiceProvider   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
lcom 2
cbo 9
dl 0
loc 183
rs 10
c 1
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 10 1
A boot() 0 5 1
A bindContracts() 0 6 1
A bindConfig() 0 6 1
A bindSender() 0 6 1
A bindResolver() 0 6 1
A bindNotifynder() 0 8 1
A registerSenders() 0 14 1
A config() 0 8 1
A migration() 0 8 3
A publishMigration() 0 8 1
A getMigrationFilepath() 0 8 2
1
<?php
2
3
namespace Fenos\Notifynder;
4
5
use Illuminate\Support\ServiceProvider;
6
use Fenos\Notifynder\Collections\Config;
7
use Fenos\Notifynder\Senders\OnceSender;
8
use Fenos\Notifynder\Senders\SingleSender;
9
use Fenos\Notifynder\Managers\SenderManager;
10
use Fenos\Notifynder\Senders\MultipleSender;
11
use Fenos\Notifynder\Resolvers\ModelResolver;
12
use Fenos\Notifynder\Contracts\ConfigContract;
13
use Fenos\Notifynder\Managers\NotifynderManager;
14
use Fenos\Notifynder\Contracts\SenderManagerContract;
15
use Fenos\Notifynder\Contracts\NotifynderManagerContract;
16
17
/**
18
 * Class NotifynderServiceProvider.
19
 */
20
class NotifynderServiceProvider extends ServiceProvider
21
{
22
    protected $migrations = [
23
        'NotificationCategories' => '2014_02_10_145728_notification_categories',
24
        'CreateNotificationGroupsTable' => '2014_08_01_210813_create_notification_groups_table',
25
        'CreateNotificationCategoryNotificationGroupTable' => '2014_08_01_211045_create_notification_category_notification_group_table',
26
        'CreateNotificationsTable' => '2015_05_05_212549_create_notifications_table',
27
        'AddExpireTimeColumnToNotificationTable' => '2015_06_06_211555_add_expire_time_column_to_notification_table',
28
        'ChangeTypeToExtraInNotificationsTable' => '2015_06_06_211555_change_type_to_extra_in_notifications_table',
29
        'AlterCategoryNameToUnique' => '2015_06_07_211555_alter_category_name_to_unique',
30
        'MakeNotificationUrlNullable' => '2016_04_19_200827_make_notification_url_nullable',
31
        'AddStackIdToNotifications' => '2016_05_19_144531_add_stack_id_to_notifications',
32
        'UpdateVersion4NotificationsTable' => '2016_07_01_153156_update_version4_notifications_table',
33
        'DropVersion4UnusedTables' => '2016_11_02_193415_drop_version4_unused_tables',
34
    ];
35
36
    /**
37
     * Register the service provider.
38
     *
39
     * @return void
40
     */
41
    public function register()
42
    {
43
        $this->bindContracts();
44
        $this->bindConfig();
45
        $this->bindSender();
46
        $this->bindResolver();
47
        $this->bindNotifynder();
48
49
        $this->registerSenders();
50
    }
51
52
    /**
53
     * Boot the service provider.
54
     *
55
     * @return void
56
     */
57
    public function boot()
58
    {
59
        $this->config();
60
        $this->migration();
61
    }
62
63
    /**
64
     * Bind contracts.
65
     *
66
     * @return void
67
     */
68
    protected function bindContracts()
69
    {
70
        $this->app->bind(NotifynderManagerContract::class, 'notifynder');
71
        $this->app->bind(SenderManagerContract::class, 'notifynder.sender');
72
        $this->app->bind(ConfigContract::class, 'notifynder.config');
73
    }
74
75
    /**
76
     * Bind Notifynder config.
77
     *
78
     * @return void
79
     */
80
    protected function bindConfig()
81
    {
82
        $this->app->singleton('notifynder.config', function () {
83
            return new Config();
84
        });
85
    }
86
87
    /**
88
     * Bind Notifynder sender.
89
     *
90
     * @return void
91
     */
92
    protected function bindSender()
93
    {
94
        $this->app->singleton('notifynder.sender', function () {
95
            return new SenderManager();
96
        });
97
    }
98
99
    /**
100
     * Bind Notifynder resolver.
101
     *
102
     * @return void
103
     */
104
    protected function bindResolver()
105
    {
106
        $this->app->singleton('notifynder.resolver.model', function () {
107
            return new ModelResolver();
108
        });
109
    }
110
111
    /**
112
     * Bind Notifynder manager.
113
     *
114
     * @return void
115
     */
116
    protected function bindNotifynder()
117
    {
118
        $this->app->singleton('notifynder', function ($app) {
119
            return new NotifynderManager(
120
                $app['notifynder.sender']
121
            );
122
        });
123
    }
124
125
    /**
126
     * Register the default senders.
127
     *
128
     * @return void
129
     */
130
    public function registerSenders()
131
    {
132
        app('notifynder')->extend('sendSingle', function (array $notifications) {
133
            return new SingleSender($notifications);
134
        });
135
136
        app('notifynder')->extend('sendMultiple', function (array $notifications) {
137
            return new MultipleSender($notifications);
138
        });
139
140
        app('notifynder')->extend('sendOnce', function (array $notifications) {
141
            return new OnceSender($notifications);
142
        });
143
    }
144
145
    /**
146
     * Publish and merge config file.
147
     *
148
     * @return void
149
     */
150
    protected function config()
151
    {
152
        $this->publishes([
153
            __DIR__.'/../config/notifynder.php' => config_path('notifynder.php'),
154
        ]);
155
156
        $this->mergeConfigFrom(__DIR__.'/../config/notifynder.php', 'notifynder');
157
    }
158
159
    /**
160
     * Publish migration files.
161
     *
162
     * @return void
163
     */
164
    protected function migration()
165
    {
166
        foreach ($this->migrations as $class => $file) {
167
            if (! class_exists($class)) {
168
                $this->publishMigration($file);
169
            }
170
        }
171
    }
172
173
    /**
174
     * Publish a single migration file.
175
     *
176
     * @param string $filename
177
     * @return void
178
     */
179
    protected function publishMigration($filename)
180
    {
181
        $extension = '.php';
182
        $filename = trim($filename, $extension).$extension;
183
        $stub = __DIR__.'/../migrations/'.$filename;
184
        $target = $this->getMigrationFilepath($filename);
185
        $this->publishes([$stub => $target], 'migrations');
186
    }
187
188
    /**
189
     * Get the migration file path.
190
     *
191
     * @param string $filename
192
     * @return string
193
     */
194
    protected function getMigrationFilepath($filename)
195
    {
196
        if (function_exists('database_path')) {
197
            return database_path('/migrations/'.$filename);
198
        }
199
200
        return base_path('/database/migrations/'.$filename); // @codeCoverageIgnore
201
    }
202
}
203