SmsProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace HoomanMirghasemi\Sms\Providers;
4
5
use HoomanMirghasemi\Sms\Drivers\Avanak;
6
use HoomanMirghasemi\Sms\Drivers\FakeSmsSender;
7
use HoomanMirghasemi\Sms\Drivers\Kavenegar;
8
use HoomanMirghasemi\Sms\Drivers\Magfa;
9
use HoomanMirghasemi\Sms\Drivers\SmsOnline;
10
use HoomanMirghasemi\Sms\SmsManager;
11
use HoomanMirghasemi\Sms\VoiceCallManager;
12
use Illuminate\Support\ServiceProvider;
13
use Kavenegar\KavenegarApi;
14
15
class SmsProvider extends ServiceProvider
16
{
17
    /**
18
     * Boot the application events.
19
     *
20
     * @return void
21
     */
22
    public function boot(): void
23
    {
24
        $this->loadMigrations()->loadViews();
25
    }
26
27
    /**
28
     * Register the service provider.
29
     *
30
     * @return void
31
     */
32
    public function register(): void
33
    {
34
        $this->mergeConfigFiles();
35
36
        $this->app->register(RouteServiceProvider::class);
37
        $this->app->register(EventServiceProvider::class);
38
39
        /**
40
         * Bind to service container.
41
         */
42
        $this->app->singleton('sms', SmsManager::class);
43
        $this->app->singleton('voiceCall', VoiceCallManager::class);
44
45
        $this->app->bind(FakeSmsSender::class, function () {
46
            $config = config('sms.drivers.fake') ?? [];
47
48
            return new FakeSmsSender($config);
49
        });
50
51
        $this->app->bind(Kavenegar::class, function () {
52
            $config = config('sms.drivers.kavenegar') ?? [];
53
            $kavenegarApi = new KavenegarApi($config['apiKey']);
54
55
            return new Kavenegar($config, $kavenegarApi);
56
        });
57
58
        $this->app->bind(Magfa::class, function () {
59
            $config = config('sms.drivers.magfa') ?? [];
60
61
            return new Magfa($config);
62
        });
63
64
        $this->app->bind(SmsOnline::class, function () {
65
            $config = config('sms.drivers.smsonline') ?? [];
66
67
            return new SmsOnline($config);
68
        });
69
70
        $this->app->bind(Avanak::class, function () {
71
            $config = config('sms.drivers.avanak') ?? [];
72
73
            return new Avanak($config);
74
        });
75
    }
76
77
    private function loadMigrations(): self
78
    {
79
        $this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
80
81
        $this->publishes([
82
            __DIR__.'/../../database/migrations/' => database_path('/migrations'),
83
        ], 'iran-sms-migrations');
84
85
        return $this;
86
    }
87
88
    /**
89
     * Register views.
90
     */
91
    public function loadViews(): self
92
    {
93
        $this->loadViewsFrom(__DIR__.'/../Resources/views', 'sms');
94
95
        $this->publishes([
96
            __DIR__.'/../Resources/views' => base_path('resources/views/vendor/sms'),
97
        ], 'iran-sms-views');
98
99
        return $this;
100
    }
101
102
    private function mergeConfigFiles(): void
103
    {
104
        $this->mergeConfigFrom(
105
            __DIR__.'/../../config/sms.php',
106
            'sms'
107
        );
108
109
        $this->publishes([
110
            __DIR__.'/../../config/sms.php' => config_path('sms.php'),
111
        ], 'iran-sms-config');
112
    }
113
}
114