Completed
Push — master ( 8667c0...414083 )
by Hilmi Erdem
07:00
created

JetSmsServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 12
dl 0
loc 97
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 5 1
B registerJetSmsClient() 0 26 3
B registerJetSmsService() 0 30 1
A provides() 0 7 1
1
<?php
2
3
namespace NotificationChannels\JetSms;
4
5
use GuzzleHttp\Client;
6
use UnexpectedValueException;
7
use Erdemkeren\JetSms\ShortMessage;
8
use Erdemkeren\JetSms\Http\Clients;
9
use Erdemkeren\JetSms\JetSmsService;
10
use Illuminate\Support\ServiceProvider;
11
use NotificationChannels\JetSms\Events;
12
use Erdemkeren\JetSms\ShortMessageFactory;
13
use Erdemkeren\JetSms\ShortMessageCollection;
14
use Erdemkeren\JetSms\ShortMessageCollectionFactory;
15
use Erdemkeren\JetSms\Http\Responses\JetSmsResponseInterface;
16
17
/**
18
 * Class JetSmsServiceProvider.
19
 */
20
class JetSmsServiceProvider extends ServiceProvider
21
{
22
    /**
23
     * Indicates if loading of the provider is deferred.
24
     *
25
     * @var bool
26
     */
27
    protected $defer = true;
28
29
    /**
30
     * Bootstrap the application services.
31
     */
32
    public function boot()
33
    {
34
        $this->registerJetSmsClient();
35
        $this->registerJetSmsService();
36
    }
37
38
    /**
39
     * Register the JetSms Client binding with the container.
40
     *
41
     * @return void
42
     */
43
    private function registerJetSmsClient()
44
    {
45
        $this->app->bind(Clients\JetSmsClientInterface::class, function () {
46
            $client = null;
0 ignored issues
show
Unused Code introduced by
$client is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
            $username = config('services.JetSms.username');
48
            $password = config('services.JetSms.password');
49
            $originator = config('services.JetSms.originator');
50
51
            switch (config('services.JetSms.client', 'http')) {
52
                case 'http':
53
                    $timeout = config('services.JetSms.timeout');
54
                    $endpoint = config('services.JetSms.http.endpoint');
55
                    $client = new Clients\JetSmsHttpClient(
56
                        new Client(['timeout' => $timeout]), $endpoint, $username, $password, $originator);
57
                    break;
58
                case 'xml':
59
                    $endpoint = config('services.JetSms.xml.endpoint');
60
                    $client = new Clients\JetSmsXmlClient($endpoint, $username, $password, $originator);
61
                    break;
62
                default:
63
                    throw new UnexpectedValueException('Unknown JetSms API client has been provided.');
64
            }
65
66
            return $client;
67
        });
68
    }
69
70
    /**
71
     * Register the jet-sms service.
72
     */
73
    private function registerJetSmsService()
74
    {
75
        $beforeSingle = function (ShortMessage $shortMessage) {
76
            event(new Events\SendingMessage($shortMessage));
77
        };
78
79
        $afterSingle = function (JetSmsResponseInterface $response, ShortMessage $shortMessage) {
80
            event(new Events\MessageWasSent($shortMessage, $response));
81
        };
82
83
        $beforeMany = function (ShortMessageCollection $shortMessages) {
84
            event(new Events\SendingMessages($shortMessages));
85
        };
86
87
        $afterMany = function (JetSmsResponseInterface $response, ShortMessageCollection $shortMessages) {
88
            event(new Events\MessagesWereSent($shortMessages, $response));
89
        };
90
91
        $this->app->singleton('jet-sms', function ($app) use ($beforeSingle, $afterSingle, $beforeMany, $afterMany) {
92
            return new JetSmsService(
93
                $app->make(Clients\JetSmsClientInterface::class),
94
                new ShortMessageFactory(),
95
                new ShortMessageCollectionFactory(),
96
                $beforeSingle,
97
                $afterSingle,
98
                $beforeMany,
99
                $afterMany
100
            );
101
        });
102
    }
103
104
    /**
105
     * Get the services provided by the provider.
106
     *
107
     * @return array
108
     */
109
    public function provides()
110
    {
111
        return [
112
            'jet-sms',
113
            Clients\JetSmsClientInterface::class,
114
        ];
115
    }
116
}
117