Skip to content

Commit 72361db

Browse files
Merge pull request #79 from otsch/clean-up
Some clean up and improvements
2 parents 9c850a2 + b49079a commit 72361db

13 files changed

+195
-204
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
All notable changes to the `Twitter notification channel` will be documented in this file
44

5+
## 6.0.0 - 2022-02-x
6+
### Added
7+
- Abstract parent class `TwitterMessage` for
8+
`TwitterStatusUpdate` and `TwitterDirectMessage`.
9+
10+
### Changed
11+
- Add a lot of missing argument and return types.
12+
- Make use of constructor property promotion.
13+
514
## 2.0.0 - 2018-11-08
615

716
- update dependencies

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ To use this package, you need to create a notification class, like `NewsWasPubli
7171

7272
```php
7373
<?php
74+
75+
use Illuminate\Notifications\Notification;
7476
use NotificationChannels\Twitter\TwitterChannel;
77+
use NotificationChannels\Twitter\TwitterMessage;
7578
use NotificationChannels\Twitter\TwitterStatusUpdate;
7679

7780
class NewsWasPublished extends Notification
7881
{
79-
8082
/**
8183
* Get the notification's delivery channels.
8284
*
@@ -88,7 +90,7 @@ class NewsWasPublished extends Notification
8890
return [TwitterChannel::class];
8991
}
9092

91-
public function toTwitter($notifiable)
93+
public function toTwitter(mixed $notifiable): TwitterMessage
9294
{
9395
return new TwitterStatusUpdate('Laravel notifications are awesome!');
9496
}
@@ -98,15 +100,15 @@ class NewsWasPublished extends Notification
98100
Take a closer look at the `toTwitter` method. Here we define what kind of Twitter message we want to trigger. In this case, it is a status update message, which is just a new message in your timeline.
99101

100102
````php
101-
public function toTwitter($notifiable)
103+
public function toTwitter(mixed $notifiable): TwitterMessage
102104
{
103105
return new TwitterStatusUpdate('Laravel notifications are awesome!');
104106
}
105107
````
106108
### Publish Twitter status update with images
107109
It is possible to publish images with your status update too. You have to pass the image path to the `withImage` method.
108110
````php
109-
public function toTwitter($notifiable)
111+
public function toTwitter(mixed $notifiable): TwitterMessage
110112
{
111113
return (new TwitterStatusUpdate('Laravel notifications are awesome!'))->withImage('marcel.png');
112114
}
@@ -121,7 +123,7 @@ return (new TwitterStatusUpdate('Laravel notifications are awesome!'))->withImag
121123
### Send a direct message
122124
To send a Twitter direct message to a specific user, you will need the `TwitterDirectMessage` class. Provide the Twitter user handler as the first parameter and the the message as the second one.
123125
````php
124-
public function toTwitter($notifiable)
126+
public function toTwitter(mixed $notifiable): TwitterMessage
125127
{
126128
return new TwitterDirectMessage('marcelpociot', 'Hey Marcel, it was nice meeting you at the Laracon.');
127129
}
@@ -130,7 +132,7 @@ public function toTwitter($notifiable)
130132
You can also provide the `user ID` instead of the `screen name`. This would prevent an extra Twitter API call. Make sure to pass it as an integer when you do.
131133

132134
````php
133-
public function toTwitter($notifiable)
135+
public function toTwitter(mixed $notifiable): TwitterMessage
134136
{
135137
return new TwitterDirectMessage(12345, 'Hey Marcel, it was nice meeting you at the Laracon.');
136138
}

src/Exceptions/CouldNotSendNotification.php

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

33
namespace NotificationChannels\Twitter\Exceptions;
44

5-
class CouldNotSendNotification extends \Exception
5+
use Exception;
6+
7+
class CouldNotSendNotification extends Exception
68
{
7-
/**
8-
* @param $response
9-
* @return CouldNotSendNotification
10-
*/
11-
public static function serviceRespondsNotSuccessful($response)
9+
public static function serviceRespondsNotSuccessful(mixed $response): CouldNotSendNotification
1210
{
1311
if (isset($response->error)) {
1412
return new static("Couldn't post notification. Response: ".$response->error);
@@ -19,23 +17,17 @@ public static function serviceRespondsNotSuccessful($response)
1917
return new static("Couldn't post notification. Response: ".$responseBody);
2018
}
2119

22-
/**
23-
* @param $response
24-
* @return CouldNotSendNotification
25-
*/
26-
public static function userWasNotFound($response)
20+
public static function userWasNotFound(mixed $response): CouldNotSendNotification
2721
{
2822
$responseBody = print_r($response->errors[0]->message, true);
2923

3024
return new static("Couldn't send direct message notification. Response: ".$responseBody);
3125
}
3226

33-
/**
34-
* @param $exceededLength
35-
* @return CouldNotSendNotification
36-
*/
37-
public static function statusUpdateTooLong($exceededLength)
27+
public static function statusUpdateTooLong(int $exceededLength): CouldNotSendNotification
3828
{
39-
return new static("Couldn't post notification, because the status message was too long by ".$exceededLength.' character(s).');
29+
return new static(
30+
"Couldn't post notification, because the status message was too long by ${exceededLength} character(s)."
31+
);
4032
}
4133
}

src/TwitterChannel.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,34 @@
33
namespace NotificationChannels\Twitter;
44

55
use Abraham\TwitterOAuth\TwitterOAuth;
6-
use Illuminate\Notifications\Notifiable;
76
use Illuminate\Notifications\Notification;
87
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification;
98

109
class TwitterChannel
1110
{
12-
/** @var TwitterOAuth */
13-
protected $twitter;
14-
15-
/** @param TwitterOAuth $twitter */
16-
public function __construct(TwitterOAuth $twitter)
11+
public function __construct(protected TwitterOAuth $twitter)
1712
{
18-
$this->twitter = $twitter;
1913
}
2014

2115
/**
2216
* Send the given notification.
2317
*
24-
* @param mixed $notifiable
25-
* @param \Illuminate\Notifications\Notification $notification
18+
* @param mixed $notifiable Should be an object that uses the Illuminate\Notifications\Notifiable trait.
19+
*
2620
* @throws CouldNotSendNotification
2721
*/
28-
public function send($notifiable, Notification $notification)
22+
public function send($notifiable, Notification $notification): array|object
2923
{
3024
$this->changeTwitterSettingsIfNeeded($notifiable);
3125

3226
$twitterMessage = $notification->toTwitter($notifiable);
3327
$twitterMessage = $this->addImagesIfGiven($twitterMessage);
3428

35-
$twitterApiResponse = $this->twitter->post($twitterMessage->getApiEndpoint(), $twitterMessage->getRequestBody($this->twitter),
36-
$twitterMessage->isJsonRequest);
29+
$twitterApiResponse = $this->twitter->post(
30+
$twitterMessage->getApiEndpoint(),
31+
$twitterMessage->getRequestBody(),
32+
$twitterMessage->isJsonRequest,
33+
);
3734

3835
if ($this->twitter->getLastHttpCode() !== 200) {
3936
throw CouldNotSendNotification::serviceRespondsNotSuccessful($this->twitter->getLastBody());
@@ -45,23 +42,27 @@ public function send($notifiable, Notification $notification)
4542
/**
4643
* Use per user settings instead of default ones.
4744
*
48-
* @param Notifiable $notifiable
45+
* @param object $notifiable Provide an object that uses the Illuminate\Notifications\Notifiable trait.
4946
*/
50-
private function changeTwitterSettingsIfNeeded($notifiable)
47+
private function changeTwitterSettingsIfNeeded(object $notifiable)
5148
{
52-
if (method_exists($notifiable, 'routeNotificationFor') && $twitterSettings = $notifiable->routeNotificationFor('twitter')) {
53-
$this->twitter = new TwitterOAuth($twitterSettings[0], $twitterSettings[1], $twitterSettings[2],
54-
$twitterSettings[3]);
49+
if (
50+
method_exists($notifiable, 'routeNotificationFor') &&
51+
$twitterSettings = $notifiable->routeNotificationFor('twitter')
52+
) {
53+
$this->twitter = new TwitterOAuth(
54+
$twitterSettings[0],
55+
$twitterSettings[1],
56+
$twitterSettings[2],
57+
$twitterSettings[3],
58+
);
5559
}
5660
}
5761

5862
/**
5963
* If it is a status update message and images are provided, add them.
60-
*
61-
* @param $twitterMessage
62-
* @return mixed
6364
*/
64-
private function addImagesIfGiven($twitterMessage)
65+
private function addImagesIfGiven(TwitterMessage $twitterMessage): object
6566
{
6667
if (is_a($twitterMessage, TwitterStatusUpdate::class) && $twitterMessage->getImages()) {
6768
$this->twitter->setTimeouts(10, 15);

src/TwitterDirectMessage.php

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,28 @@
55
use Abraham\TwitterOAuth\TwitterOAuth;
66
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification;
77

8-
class TwitterDirectMessage
8+
class TwitterDirectMessage extends TwitterMessage
99
{
10-
/** @var string */
11-
private $content;
10+
public bool $isJsonRequest = true;
1211

13-
/** @var string */
14-
private $to;
15-
16-
/** @var bool */
17-
public $isJsonRequest = true;
18-
19-
/** @var string */
20-
private $apiEndpoint = 'direct_messages/events/new';
21-
22-
/**
23-
* TwitterDirectMessage constructor.
24-
*
25-
* @param $to
26-
* @param $content
27-
*/
28-
public function __construct($to, $content)
12+
public function __construct(private string|int $to, string $content)
2913
{
30-
$this->to = $to;
31-
$this->content = $content;
14+
parent::__construct($content);
3215
}
3316

34-
/**
35-
* Get Twitter direct message content.
36-
*
37-
* @return string
38-
*/
39-
public function getContent()
17+
public function getApiEndpoint(): string
4018
{
41-
return $this->content;
19+
return 'direct_messages/events/new';
4220
}
4321

4422
/**
4523
* Get Twitter direct message receiver.
4624
*
47-
* @param TwitterOAuth $twitter
48-
* @return string
25+
* @return string|mixed
26+
*
4927
* @throws CouldNotSendNotification
5028
*/
51-
public function getReceiver(TwitterOAuth $twitter)
29+
public function getReceiver(TwitterOAuth $twitter): mixed
5230
{
5331
if (is_int($this->to)) {
5432
return $this->to;
@@ -67,24 +45,12 @@ public function getReceiver(TwitterOAuth $twitter)
6745
return $user->id;
6846
}
6947

70-
/**
71-
* Return Twitter direct message api endpoint.
72-
*
73-
* @return string
74-
*/
75-
public function getApiEndpoint()
76-
{
77-
return $this->apiEndpoint;
78-
}
79-
8048
/**
8149
* Build Twitter request body.
8250
*
83-
* @param TwitterOAuth $twitter
84-
* @return array
8551
* @throws CouldNotSendNotification
8652
*/
87-
public function getRequestBody(TwitterOAuth $twitter)
53+
public function getRequestBody(TwitterOAuth $twitter): array
8854
{
8955
return [
9056
'event' => [

src/TwitterImage.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,11 @@
44

55
class TwitterImage
66
{
7-
/** @var string */
8-
private $imagePath;
9-
10-
/**
11-
* TwitterImage constructor.
12-
*
13-
* @param $imagePath
14-
*/
15-
public function __construct($imagePath)
7+
public function __construct(private string $imagePath)
168
{
17-
$this->imagePath = $imagePath;
189
}
1910

20-
/**
21-
* Get image path.
22-
*
23-
* @return string
24-
*/
25-
public function getPath()
11+
public function getPath(): string
2612
{
2713
return $this->imagePath;
2814
}

src/TwitterMessage.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace NotificationChannels\Twitter;
4+
5+
abstract class TwitterMessage
6+
{
7+
public bool $isJsonRequest = false;
8+
9+
public function __construct(protected string $content)
10+
{
11+
}
12+
13+
public function getContent(): string
14+
{
15+
return $this->content;
16+
}
17+
18+
abstract public function getApiEndpoint(): string;
19+
}

0 commit comments

Comments
 (0)