Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
add new publishBatch method to PubSubAdapterInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgoslett committed May 16, 2017
1 parent 54f26ac commit f6ee715
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ $adapter->subscribe('my_channel', function ($message) {

// publish messages
$adapter->publish('my_channel', 'Hello World!');

// publish multiple messages
$messages = [
'message 1',
'message 2',
];
$adapter->publishBatch('my_channel', $messages);
```

## Writing an Adapter
Expand All @@ -66,6 +73,14 @@ public function subscribe($channel, callable $handler);
* @param mixed $message
*/
public function publish($channel, $message);

/**
* Publish multiple messages to a channel.
*
* @param string $channel
* @param array $messages
*/
public function publishBatch($channel, array $messages);
```

## Examples
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.0.0 - 2017-05-16

* Implement new publishBatch method on PubSubAdapterInterface
* Change message serialization to only json encode and decode

## 1.0.0 - 2016-09-02
Expand Down
11 changes: 11 additions & 0 deletions src/Adapters/DevNullPubSubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ public function publish($channel, $message)
{
// your message is going to /dev/null
}

/**
* Publish multiple messages to a channel.
*
* @param string $channel
* @param array $messages
*/
public function publishBatch($channel, array $messages)
{
// your messages are going to /dev/null
}
}
13 changes: 13 additions & 0 deletions src/Adapters/LocalPubSubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ public function publish($channel, $message)
}
}

/**
* Publish multiple messages to a channel.
*
* @param string $channel
* @param array $messages
*/
public function publishBatch($channel, array $messages)
{
foreach ($messages as $message) {
$this->publish($channel, $message);
}
}

/**
* Return all subscribers on the given channel.
*
Expand Down
8 changes: 8 additions & 0 deletions src/PubSubAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public function subscribe($channel, callable $handler);
* @param mixed $message
*/
public function publish($channel, $message);

/**
* Publish multiple messages to a channel.
*
* @param string $channel
* @param array $messages
*/
public function publishBatch($channel, array $messages);
}
29 changes: 29 additions & 0 deletions tests/LocalPubSubAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,33 @@ public function testPublish()

$adapter->publish('test_channel', 'This is a message sent to handler1 & handler2');
}

public function testPublishBatch()
{
$adapter = new LocalPubSubAdapter();

$handler1 = Mockery::mock(\stdClass::class);
$handler1->shouldReceive('handle')
->with('This is a message sent to handler1 & handler2')
->once();
$handler1->shouldReceive('handle')
->with('This is another message!')
->once();
$adapter->subscribe('test_channel', [$handler1, 'handle']);

$handler2 = Mockery::mock(\stdClass::class);
$handler2->shouldReceive('handle')
->with('This is a message sent to handler1 & handler2')
->once();
$handler2->shouldReceive('handle')
->with('This is another message!')
->once();
$adapter->subscribe('test_channel', [$handler2, 'handle']);

$messages = [
'This is a message sent to handler1 & handler2',
'This is another message!',
];
$adapter->publishBatch('test_channel', $messages);
}
}

0 comments on commit f6ee715

Please sign in to comment.