From b990fe0252dea4ccff721daaeb5d12460304ba6b Mon Sep 17 00:00:00 2001 From: Dwight Watson Date: Mon, 16 Oct 2023 08:31:40 +1100 Subject: [PATCH] Use named constructors --- README.md | 14 ++++---- src/FcmMessage.php | 54 ++++++++-------------------- src/Resources/FcmResource.php | 4 +-- src/Resources/Notification.php | 20 +++++------ tests/FcmMessageTest.php | 14 +++++++- tests/Resources/NotificationTest.php | 13 ++++++- 6 files changed, 56 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 2ac9084..e54c302 100644 --- a/README.md +++ b/README.md @@ -61,14 +61,12 @@ class AccountActivated extends Notification public function toFcm($notifiable) { - return FcmMessage::create() - ->notification( - FcmNotification::create() - ->title('Account Activated') - ->body('Your account has been activated.') - ->image('http://example.com/url-to-image-here.png') - ) - ->setData(['data1' => 'value', 'data2' => 'value2']) + return new FcmMessage(notification: new FcmNotification( + title: 'Account Activated', + body: 'Your account has been activated.', + image: 'http://example.com/url-to-image-here.png' + )) + ->data(['data1' => 'value', 'data2' => 'value2']) ->custom([ 'android' => [ 'notification' => [ diff --git a/src/FcmMessage.php b/src/FcmMessage.php index a69c131..b0d65ef 100644 --- a/src/FcmMessage.php +++ b/src/FcmMessage.php @@ -12,51 +12,27 @@ class FcmMessage implements Message use Macroable; /** - * The message name. - */ - public ?string $name = null; - - /** - * The message token. - */ - public ?string $token = null; - - /** - * The message topic. - */ - public ?string $topic = null; - - /** - * The message condition. - */ - public ?string $condition = null; - - /** - * The message data. - */ - public ?array $data = []; - - /** - * The custom message data. - */ - public array $custom = []; - - /** - * The message notification. - */ - public ?Notification $notification = null; - - /** - * The custom messaging client. + * Create a new message instance. */ - public ?Messaging $client = null; + public function __construct( + public ?string $name = null, + public ?string $token = null, + public ?string $topic = null, + public ?string $condition = null, + public ?array $data = [], + public array $custom = [], + public ?Notification $notification = null, + public ?Messaging $client = null, + ) { + // + } /** * Create a new message instance. */ - public static function create(): self + public static function create(...$args): static { - return new self; + return new static(...$args); } /** diff --git a/src/Resources/FcmResource.php b/src/Resources/FcmResource.php index d03e4fb..3c9b9e5 100644 --- a/src/Resources/FcmResource.php +++ b/src/Resources/FcmResource.php @@ -7,9 +7,9 @@ abstract class FcmResource /** * @return static */ - public static function create(): static + public static function create(...$args): static { - return new static; + return new static(...$args); } /** diff --git a/src/Resources/Notification.php b/src/Resources/Notification.php index 3a8de8b..60f7d73 100644 --- a/src/Resources/Notification.php +++ b/src/Resources/Notification.php @@ -5,19 +5,15 @@ class Notification extends FcmResource { /** - * The notification title. + * Create a new notification instance. */ - public ?string $title = null; - - /** - * The notification body. - */ - public ?string $body = null; - - /** - * The notification image. - */ - public ?string $image = null; + public function __construct( + public ?string $title = null, + public ?string $body = null, + public ?string $image = null + ) { + // + } /** * Set the notification title. diff --git a/tests/FcmMessageTest.php b/tests/FcmMessageTest.php index d050b47..9718db2 100644 --- a/tests/FcmMessageTest.php +++ b/tests/FcmMessageTest.php @@ -12,11 +12,23 @@ class FcmMessageTest extends TestCase { public function test_it_can_be_instantiated() { - $message = FcmMessage::create(); + $message = new FcmMessage(name: 'name'); $this->assertInstanceOf(FcmMessage::class, $message); + + $this->assertEquals('name', $message->name); + } + + public function test_it_can_be_created() + { + $message = FcmMessage::create(name: 'name'); + + $this->assertInstanceOf(FcmMessage::class, $message); + + $this->assertEquals('name', $message->name); } + public function test_it_can_set_name() { $message = FcmMessage::create()->name('name'); diff --git a/tests/Resources/NotificationTest.php b/tests/Resources/NotificationTest.php index 0ef0ded..237f00a 100644 --- a/tests/Resources/NotificationTest.php +++ b/tests/Resources/NotificationTest.php @@ -9,9 +9,20 @@ class NotificationTest extends TestCase { public function test_it_can_be_instantiated() { - $message = Notification::create(); + $message = new Notification(title: 'title'); $this->assertInstanceOf(Notification::class, $message); + + $this->assertEquals('title', $message->title); + } + + public function test_it_can_be_created() + { + $message = Notification::create(title: 'title'); + + $this->assertInstanceOf(Notification::class, $message); + + $this->assertEquals('title', $message->title); } public function test_it_can_set_title()