Completed
Push — version-4 ( 855c86...c55f2f )
by
unknown
09:40
created

NotifynderServiceProvider::migrationFilepath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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