Skip to content

Commit 2164988

Browse files
committed
Use modern syntax: Constructor property promotion, improved types
1 parent 8759f4f commit 2164988

9 files changed

+44
-175
lines changed

config/twilio-notification-channel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22

33
return [
4-
'enabled' => env('TWILIO_ENABLED', true),
4+
'enabled' => (bool) env('TWILIO_ENABLED', true),
55
'username' => env('TWILIO_USERNAME'), // optional when using auth token
66
'password' => env('TWILIO_PASSWORD'), // optional when using auth token
77
'auth_token' => env('TWILIO_AUTH_TOKEN'), // optional when using username and password
88
'account_sid' => env('TWILIO_ACCOUNT_SID'),
99

1010
'from' => env('TWILIO_FROM'), // optional
1111
'alphanumeric_sender' => env('TWILIO_ALPHA_SENDER'),
12-
'shorten_urls' => env('TWILIO_SHORTEN_URLS', false), // optional, enable twilio URL shortener
12+
'shorten_urls' => (bool) env('TWILIO_SHORTEN_URLS', false), // optional, enable twilio URL shortener
1313

1414
/**
1515
* See https://www.twilio.com/docs/sms/services.

src/Twilio.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,18 @@
1010

1111
class Twilio
1212
{
13-
/** @var TwilioService */
14-
protected $twilioService;
15-
16-
/** @var TwilioConfig */
17-
public $config;
18-
19-
public function __construct(TwilioService $twilioService, TwilioConfig $config)
20-
{
21-
$this->twilioService = $twilioService;
22-
$this->config = $config;
23-
}
13+
public function __construct(
14+
protected TwilioService $twilioService,
15+
public TwilioConfig $config
16+
) {}
2417

2518
/**
26-
* Send a TwilioMessage to the a phone number.
19+
* Send a TwilioMessage to a phone number.
2720
*
28-
*
29-
* @return mixed
3021
* @throws TwilioException
3122
* @throws CouldNotSendNotification
3223
*/
33-
public function sendMessage(TwilioMessage $message, ?string $to, bool $useAlphanumericSender = false)
24+
public function sendMessage(TwilioMessage $message, ?string $to, bool $useAlphanumericSender = false): CallInstance|MessageInstance
3425
{
3526
if ($message instanceof TwilioSmsMessage) {
3627
if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
@@ -164,12 +155,7 @@ protected function getAlphanumericSender(): ?string
164155
return $this->config->getAlphanumericSender();
165156
}
166157

167-
/**
168-
* @param array $params
169-
* @param TwilioMessage $message
170-
* @param array $optionalParams
171-
*/
172-
protected function fillOptionalParams(&$params, $message, $optionalParams): self
158+
protected function fillOptionalParams(array &$params, TwilioMessage $message, array $optionalParams): self
173159
{
174160
foreach ($optionalParams as $optionalParam) {
175161
if ($message->$optionalParam) {

src/TwilioCallMessage.php

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,16 @@ class TwilioCallMessage extends TwilioMessage
88

99
public const STATUS_COMPLETED = 'completed';
1010

11-
/**
12-
* @var null|string
13-
*/
14-
public $method;
11+
public ?string $method = null;
1512

16-
/**
17-
* @var null|string
18-
*/
19-
public $status;
13+
public ?string $status = null;
2014

21-
/**
22-
* @var null|string
23-
*/
24-
public $fallbackUrl;
15+
public ?string $fallbackUrl = null;
2516

26-
/**
27-
* @var null|string
28-
*/
29-
public $fallbackMethod;
17+
public ?string $fallbackMethod = null;
3018

3119
/**
3220
* Set the message url.
33-
*
34-
* @return $this
3521
*/
3622
public function url(string $url): self
3723
{
@@ -42,11 +28,8 @@ public function url(string $url): self
4228

4329
/**
4430
* Set the message url request method.
45-
*
46-
* @param string $method
47-
* @return $this
4831
*/
49-
public function method($method): self
32+
public function method(string $method): self
5033
{
5134
$this->method = $method;
5235

@@ -55,8 +38,6 @@ public function method($method): self
5538

5639
/**
5740
* Set the status for the current calls.
58-
*
59-
* @return $this
6041
*/
6142
public function status(string $status): self
6243
{
@@ -67,8 +48,6 @@ public function status(string $status): self
6748

6849
/**
6950
* Set the fallback url.
70-
*
71-
* @return $this
7251
*/
7352
public function fallbackUrl(string $fallbackUrl): self
7453
{
@@ -79,8 +58,6 @@ public function fallbackUrl(string $fallbackUrl): self
7958

8059
/**
8160
* Set the fallback url request method.
82-
*
83-
* @return $this
8461
*/
8562
public function fallbackMethod(string $fallbackMethod): self
8663
{

src/TwilioChannel.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,18 @@
1010

1111
class TwilioChannel
1212
{
13-
/**
14-
* @var Twilio
15-
*/
16-
protected $twilio;
17-
18-
/**
19-
* @var Dispatcher
20-
*/
21-
protected $events;
22-
23-
/**
24-
* TwilioChannel constructor.
25-
*/
26-
public function __construct(Twilio $twilio, Dispatcher $events)
27-
{
28-
$this->twilio = $twilio;
29-
$this->events = $events;
30-
}
13+
public function __construct(
14+
protected Twilio $twilio,
15+
protected Dispatcher $events
16+
) {}
3117

3218
/**
3319
* Send the given notification.
3420
*
35-
* @param mixed $notifiable
36-
*
3721
* @return mixed
3822
* @throws Exception
3923
*/
40-
public function send($notifiable, Notification $notification)
24+
public function send(mixed $notifiable, Notification $notification)
4125
{
4226
if (! $this->isEnabled()) {
4327
return;
@@ -78,27 +62,19 @@ public function send($notifiable, Notification $notification)
7862
/**
7963
* Get the message to send.
8064
*
81-
* @param mixed $notifiable
82-
*
8365
* @return mixed
8466
*/
85-
protected function getMessage($notifiable, Notification $notification)
67+
protected function getMessage(mixed $notifiable, Notification $notification)
8668
{
8769
return $notification->toTwilio($notifiable);
8870
}
8971

9072
/**
9173
* Check if twilio is enabled.
92-
*
93-
* @return bool
9474
*/
95-
protected function isEnabled()
75+
protected function isEnabled(): bool
9676
{
97-
if (is_null($this->twilio->config)) {
98-
return true;
99-
}
100-
101-
return $this->twilio->config->enabled();
77+
return $this->twilio->config->enabled() ?? true;
10278
}
10379

10480
/**

src/TwilioConfig.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
namespace NotificationChannels\Twilio;
44

5-
class TwilioConfig
5+
readonly class TwilioConfig
66
{
7-
/** @var array */
8-
private $config;
7+
public function __construct(
8+
private array $config
9+
) {}
910

10-
public function __construct(array $config)
11-
{
12-
$this->config = $config;
13-
}
14-
15-
public function enabled()
11+
public function enabled(): bool
1612
{
1713
return $this->config['enabled'] ?? true;
1814
}

src/TwilioMessage.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,32 @@
44

55
abstract class TwilioMessage
66
{
7-
/**
8-
* The message content.
9-
*
10-
* @var string
11-
*/
12-
public $content;
13-
147
/**
158
* The phone number the message should be sent from.
16-
*
17-
* @var string
189
*/
19-
public $from;
10+
public ?string $from = null;
2011

21-
/**
22-
* @var null|string
23-
*/
24-
public $statusCallback;
12+
public ?string $statusCallback = null;
13+
14+
public ?string $statusCallbackMethod = null;
2515

2616
/**
27-
* @var null|string
17+
* Create a new message instance.
2818
*/
29-
public $statusCallbackMethod;
19+
public function __construct(
20+
public string $content = ''
21+
) {}
3022

3123
/**
3224
* Create a message object.
33-
* @return static
3425
*/
3526
public static function create(string $content = ''): self
3627
{
3728
return new static($content);
3829
}
3930

40-
/**
41-
* Create a new message instance.
42-
*/
43-
public function __construct(string $content = '')
44-
{
45-
$this->content = $content;
46-
}
47-
4831
/**
4932
* Set the message content.
50-
*
51-
* @return $this
5233
*/
5334
public function content(string $content): self
5435
{
@@ -59,8 +40,6 @@ public function content(string $content): self
5940

6041
/**
6142
* Set the phone number the message should be sent from.
62-
*
63-
* @return $this
6443
*/
6544
public function from(string $from): self
6645
{
@@ -79,8 +58,6 @@ public function getFrom(): ?string
7958

8059
/**
8160
* Set the status callback.
82-
*
83-
* @return $this
8461
*/
8562
public function statusCallback(string $statusCallback): self
8663
{
@@ -91,8 +68,6 @@ public function statusCallback(string $statusCallback): self
9168

9269
/**
9370
* Set the status callback request method.
94-
*
95-
* @return $this
9671
*/
9772
public function statusCallbackMethod(string $statusCallbackMethod): self
9873
{

src/TwilioMmsMessage.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44

55
class TwilioMmsMessage extends TwilioSmsMessage
66
{
7-
/**
8-
* @var string|null
9-
*/
10-
public $mediaUrl;
7+
public ?string $mediaUrl = null;
118

129
/**
1310
* Set the message media url.
14-
*
15-
* @return $this
1611
*/
1712
public function mediaUrl(string $url): self
1813
{

src/TwilioProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function boot() {}
1919
/**
2020
* Register the application services.
2121
*/
22-
public function register()
22+
public function register(): void
2323
{
2424
$this->mergeConfigFrom(__DIR__.'/../config/twilio-notification-channel.php', 'twilio-notification-channel');
2525

0 commit comments

Comments
 (0)