Skip to content

Commit

Permalink
Use named constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightwatson committed Oct 15, 2023
1 parent 60d4044 commit b990fe0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 63 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
54 changes: 15 additions & 39 deletions src/FcmMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/FcmResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
20 changes: 8 additions & 12 deletions src/Resources/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion tests/FcmMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
13 changes: 12 additions & 1 deletion tests/Resources/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b990fe0

Please sign in to comment.