1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Smsapi; |
4
|
|
|
|
5
|
|
|
use SMSApi\Client; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
|
8
|
|
|
class SmsapiServiceProvider extends ServiceProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Bootstrap the application services. |
12
|
|
|
*/ |
13
|
|
|
public function boot() |
14
|
|
|
{ |
15
|
|
|
$this->app->when(SmsapiChannel::class) |
16
|
|
|
->needs(SmsapiClient::class) |
17
|
|
|
->give(function () { |
18
|
|
|
$config = config('smsapi'); |
19
|
|
|
$auth = $config['auth']; |
20
|
|
|
if ($auth['method'] === 'token') { |
21
|
|
|
$client = Client::createFromToken($auth['credentials']['token']); |
22
|
|
|
} elseif ($auth['method'] === 'password') { |
23
|
|
|
$client = new Client($auth['credentials']['username']); |
24
|
|
|
$client->setPasswordHash($auth['credentials']['password']); |
25
|
|
|
} |
26
|
|
|
$defaults = $config['defaults'] + ['sms' => [], 'mms' => [], 'vms' => []]; |
27
|
|
|
if (! empty($defaults['common'])) { |
28
|
|
|
$defaults['common'] = array_only($defaults['common'], [ |
29
|
|
|
'notify_url', 'partner', 'test', |
30
|
|
|
]); |
31
|
|
|
$defaults['sms'] = array_only($defaults['sms'] + $defaults['common'], [ |
32
|
|
|
'from', 'fast', 'flash', 'encoding', 'normalize', 'nounicode', 'single', |
33
|
|
|
]); |
34
|
|
|
$defaults['mms'] = array_only($defaults['mms'] + $defaults['common'], [ |
35
|
|
|
]); |
36
|
|
|
$defaults['vms'] = array_only($defaults['vms'] + $defaults['common'], [ |
37
|
|
|
'from', 'tries', 'interval', 'tts_lector', 'skip_gsm', |
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
$defaults = array_only($defaults, ['sms', 'mms', 'vms']); |
41
|
|
|
$defaults = array_map(function (array $defaults) { |
42
|
|
|
return array_filter($defaults, function ($value) { |
43
|
|
|
return $value !== null; |
44
|
|
|
}); |
45
|
|
|
}, $defaults); |
46
|
|
|
|
47
|
|
|
return new SmsapiClient($client, $defaults); |
|
|
|
|
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
if ($this->app->runningInConsole()) { |
51
|
|
|
$this->publishes([ |
52
|
|
|
__DIR__.'/../config/smsapi.php' => config_path('smsapi.php'), |
53
|
|
|
], 'config'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register the application services. |
59
|
|
|
*/ |
60
|
|
|
public function register() |
61
|
|
|
{ |
62
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/smsapi.php', 'smsapi'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: