Completed
Push — version-4 ( a26cca...5903bf )
by
unknown
02:38
created

NotifynderServiceProvider::migration()   F

Complexity

Conditions 10
Paths 512

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
cc 10
eloc 19
c 6
b 1
f 1
nc 512
nop 0
dl 0
loc 30
rs 3.2187

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
class NotifynderServiceProvider extends ServiceProvider
17
{
18
    public function register()
19
    {
20
        $this->bindContracts();
21
        $this->bindConfig();
22
        $this->bindSender();
23
        $this->bindNotifynder();
24
25
        $this->registerSenders();
26
    }
27
28
    public function boot()
29
    {
30
        $this->config();
31
        $this->migration();
32
    }
33
34
    /**
35
     * Bind contracts.
36
     */
37
    protected function bindContracts()
38
    {
39
        $this->app->bind(NotifynderManagerContract::class, 'notifynder');
40
        $this->app->bind(SenderManagerContract::class, 'notifynder.sender');
41
        $this->app->bind(ConfigContract::class, 'notifynder.config');
42
    }
43
44
    /**
45
     * Bind notifynder config.
46
     */
47
    protected function bindConfig()
48
    {
49
        $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...
50
            return new Config();
51
        });
52
    }
53
54
    /**
55
     * Bind notifynder config.
56
     */
57
    protected function bindSender()
58
    {
59
        $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...
60
            return new SenderManager();
61
        });
62
    }
63
64
    /**
65
     * Bind notifynder manager.
66
     */
67
    protected function bindNotifynder()
68
    {
69
        $this->app->singleton('notifynder', function ($app) {
70
            return new NotifynderManager(
71
                $app['notifynder.sender']
72
            );
73
        });
74
    }
75
76
    public function registerSenders()
77
    {
78
        app('notifynder')->extend('sendSingle', function (array $notifications) {
79
            return new SingleSender($notifications);
80
        });
81
82
        app('notifynder')->extend('sendMultiple', function (array $notifications) {
83
            return new MultipleSender($notifications);
84
        });
85
86
        app('notifynder')->extend('sendOnce', function (array $notifications) {
87
            return new OnceSender($notifications);
88
        });
89
    }
90
91
    /**
92
     * Publish config file.
93
     */
94
    protected function config()
95
    {
96
        $this->publishes([
97
            __DIR__.'/../config/notifynder.php' => config_path('notifynder.php'),
98
        ]);
99
100
        $this->mergeConfigFrom(__DIR__.'/../config/notifynder.php', 'notifynder');
101
    }
102
103
    /**
104
     * Publish migration files.
105
     */
106
    protected function migration()
107
    {
108
        if (! class_exists('NotificationCategories')) {
109
            $this->publishMigration('2014_02_10_145728_notification_categories');
110
        }
111
        if (! class_exists('CreateNotificationGroupsTable')) {
112
            $this->publishMigration('2014_08_01_210813_create_notification_groups_table');
113
        }
114
        if (! class_exists('CreateNotificationCategoryNotificationGroupTable')) {
115
            $this->publishMigration('2014_08_01_211045_create_notification_category_notification_group_table');
116
        }
117
        if (! class_exists('CreateNotificationsTable')) {
118
            $this->publishMigration('2015_05_05_212549_create_notifications_table');
119
        }
120
        if (! class_exists('AddExpireTimeColumnToNotificationTable')) {
121
            $this->publishMigration('2015_06_06_211555_add_expire_time_column_to_notification_table');
122
        }
123
        if (! class_exists('ChangeTypeToExtraInNotificationsTable')) {
124
            $this->publishMigration('2015_06_06_211555_change_type_to_extra_in_notifications_table');
125
        }
126
        if (! class_exists('AlterCategoryNameToUnique')) {
127
            $this->publishMigration('2015_06_07_211555_alter_category_name_to_unique');
128
        }
129
        if (! class_exists('MakeNotificationUrlNullable')) {
130
            $this->publishMigration('2016_04_19_200827_make_notification_url_nullable');
131
        }
132
        if (! class_exists('AddStackIdToNotifications')) {
133
            $this->publishMigration('2016_05_19_144531_add_stack_id_to_notifications');
134
        }
135
    }
136
137
    /**
138
     * @param string $filename
139
     */
140
    protected function publishMigration($filename)
141
    {
142
        $extension = '.php';
143
        $filename = trim($filename, $extension).$extension;
144
        $stub = __DIR__.'/../migrations/'.$filename;
145
        $target = $this->migrationFilepath($filename);
146
        $this->publishes([$stub => $target], 'migrations');
147
    }
148
149
    /**
150
     * @param string $filename
151
     * @return string
152
     */
153
    protected function migrationFilepath($filename)
154
    {
155
        if (function_exists('database_path')) {
156
            return database_path('/migrations/'.$filename);
157
        } else {
158
            return base_path('/database/migrations/'.$filename);
159
        }
160
    }
161
}
162