Completed
Push — version-4 ( 91d123...7efc98 )
by
unknown
02:30
created

NotifynderServiceProvider::migration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 2 Features 4
Metric Value
cc 3
eloc 4
c 10
b 2
f 4
nc 3
nop 0
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
 */
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
    ];
33
34
    /**
35
     * Register the service provider.
36
     *
37
     * @return void
38
     */
39
    public function register()
40
    {
41
        $this->bindContracts();
42
        $this->bindConfig();
43
        $this->bindSender();
44
        $this->bindNotifynder();
45
46
        $this->registerSenders();
47
    }
48
49
    /**
50
     * Boot the service provider.
51
     *
52
     * @return void
53
     */
54
    public function boot()
55
    {
56
        $this->config();
57
        $this->migration();
58
    }
59
60
    /**
61
     * Bind contracts.
62
     *
63
     * @return void
64
     */
65
    protected function bindContracts()
66
    {
67
        $this->app->bind(NotifynderManagerContract::class, 'notifynder');
68
        $this->app->bind(SenderManagerContract::class, 'notifynder.sender');
69
        $this->app->bind(ConfigContract::class, 'notifynder.config');
70
    }
71
72
    /**
73
     * Bind Notifynder config.
74
     *
75
     * @return void
76
     */
77
    protected function bindConfig()
78
    {
79
        $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...
80
            return new Config();
81
        });
82
    }
83
84
    /**
85
     * Bind Notifynder sender.
86
     *
87
     * @return void
88
     */
89
    protected function bindSender()
90
    {
91
        $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...
92
            return new SenderManager();
93
        });
94
    }
95
96
    /**
97
     * Bind Notifynder manager.
98
     *
99
     * @return void
100
     */
101
    protected function bindNotifynder()
102
    {
103
        $this->app->singleton('notifynder', function ($app) {
104
            return new NotifynderManager(
105
                $app['notifynder.sender']
106
            );
107
        });
108
    }
109
110
    /**
111
     * Register the default senders.
112
     *
113
     * @return void
114
     */
115
    public function registerSenders()
116
    {
117
        app('notifynder')->extend('sendSingle', function (array $notifications) {
118
            return new SingleSender($notifications);
119
        });
120
121
        app('notifynder')->extend('sendMultiple', function (array $notifications) {
122
            return new MultipleSender($notifications);
123
        });
124
125
        app('notifynder')->extend('sendOnce', function (array $notifications) {
126
            return new OnceSender($notifications);
127
        });
128
    }
129
130
    /**
131
     * Publish and merge config file.
132
     *
133
     * @return void
134
     */
135
    protected function config()
136
    {
137
        $this->publishes([
138
            __DIR__.'/../config/notifynder.php' => config_path('notifynder.php'),
139
        ]);
140
141
        $this->mergeConfigFrom(__DIR__.'/../config/notifynder.php', 'notifynder');
142
    }
143
144
    /**
145
     * Publish migration files.
146
     *
147
     * @return void
148
     */
149
    protected function migration()
150
    {
151
        foreach($this->migrations as $class => $file) {
152
            if (! class_exists($class)) {
153
                $this->publishMigration($file);
154
            }
155
        }
156
    }
157
158
    /**
159
     * Publish a single migration file.
160
     *
161
     * @param string $filename
162
     * @return void
163
     */
164
    protected function publishMigration($filename)
165
    {
166
        $extension = '.php';
167
        $filename = trim($filename, $extension).$extension;
168
        $stub = __DIR__.'/../migrations/'.$filename;
169
        $target = $this->getMigrationFilepath($filename);
170
        $this->publishes([$stub => $target], 'migrations');
171
    }
172
173
    /**
174
     * Get the migration file path.
175
     *
176
     * @param string $filename
177
     * @return string
178
     */
179
    protected function getMigrationFilepath($filename)
180
    {
181
        if (function_exists('database_path')) {
182
            return database_path('/migrations/'.$filename);
183
        } else {
184
            return base_path('/database/migrations/'.$filename);
185
        }
186
    }
187
}
188