Completed
Push — version-4 ( 511c26...6005c1 )
by
unknown
02:14
created

NotifynderServiceProvider::bindNotifynder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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