Completed
Push — master ( 2f25c9...4c0020 )
by
unknown
06:10
created

NotifynderServiceProvider   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 2
cbo 8
dl 0
loc 170
rs 10

11 Methods

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