Skip to content

Commit

Permalink
Merge pull request #257 from sagikazarmark/rename_default_message
Browse files Browse the repository at this point in the history
Rename DefaultMesssage to PlainMessage
  • Loading branch information
henrikbjorn authored Dec 15, 2016
2 parents 8941ead + 16f5cf1 commit ed1d019
Show file tree
Hide file tree
Showing 23 changed files with 211 additions and 180 deletions.
4 changes: 2 additions & 2 deletions doc/consuming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ earlier, a message's name is used to determine which service object should
receive that message.

A service object can be any object that has a method corresponding to the name of the
message with the first letter lower cased. So ``new DefaultMessage('SendNewsletter')`` will trigger a
message with the first letter lower cased. So ``new PlainMessage('SendNewsletter')`` will trigger a
call to ``$serviceObject->sendNewsletter($message)``. For the system to know which service
object should handle which messages, you are required to register them first.

Expand Down Expand Up @@ -46,7 +46,7 @@ object should handle which messages, you are required to register them first.
Commandline Interface
---------------------

Bernard comes with a ``ConsumeCommand`` which can be used with Symfony Console
Bernard comes with a ``ConsumeCommand`` which can be used with Symfony Console
component.

.. code-block:: php
Expand Down
6 changes: 3 additions & 3 deletions doc/producing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The easiest way is to give it to the producer, as the queue name
is taken from the message object.

To make it easier to send messages and not require every type to be implemented
in a separate class, a ``Bernard\Message\DefaultMessage`` is provided. It can hold
in a separate class, a ``Bernard\Message\PlainMessage`` is provided. It can hold
any number of properties and only needs a name for the message. The queue name
is then generated from that. When generating the queue name it will insert a "_"
before any uppercase letter and then lowercase the name.
Expand All @@ -21,7 +21,7 @@ before any uppercase letter and then lowercase the name.
<?php
use Bernard\Message\DefaultMessage;
use Bernard\Message\PlainMessage;
use Bernard\Producer;
use Bernard\QueueFactory\PersistentFactory;
use Bernard\Serializer;
Expand All @@ -30,7 +30,7 @@ before any uppercase letter and then lowercase the name.
$factory = new PersistentFactory($driver, new Serializer());
$producer = new Producer($factory);
$message = new DefaultMessage('SendNewsletter', array(
$message = new PlainMessage('SendNewsletter', array(
'newsletterId' => 12,
));
Expand Down
8 changes: 4 additions & 4 deletions doc/serializers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ serialize messages as JSON for persistent storage.
Default serializer
------------------

By default Bernard can handle serializing the ``Bernard\Envelope`` and ``Bernard\Message\DefaultMessage`` classes,
By default Bernard can handle serializing the ``Bernard\Envelope`` and ``Bernard\Message\PlainMessage`` classes,
which should be enough when you are just starting out:

.. code-block:: php
Expand All @@ -30,7 +30,7 @@ message contains getters and setters for the properties it needs serializing:
<?php
use Bernard\Normalizer\DefaultMessageNormalizer;
use Bernard\Normalizer\PlainMessageNormalizer;
use Bernard\Normalizer\EnvelopeNormalizer;
use Bernard\Serializer;
use Normalt\Normalizer\AggregateNormalizer;
Expand All @@ -39,7 +39,7 @@ message contains getters and setters for the properties it needs serializing:
$aggregateNormalizer = new AggregateNormalizer([
new EnvelopeNormalizer(),
new GetSetMethodNormalizer(),
new DefaultMessageNormalizer(),
new PlainMessageNormalizer(),
]);
$serializer = new Serializer($aggregateNormalizer);
Expand All @@ -48,7 +48,7 @@ message contains getters and setters for the properties it needs serializing:
The ``AggregateNormalizer`` will check each normalizer passed to its constructor and use the first one that can handle
the object given to it. You should always pass the ``EnvelopeNormalizer`` first. And it's a good idea to add the
``DefaultMessageNormalizer`` last as a fallback when none other match.
``PlainMessageNormalizer`` last as a fallback when none other match.

More normalizers are available from `Symfony <http://symfony.com/doc/current/components/serializer.html#normalizers>`_,
along with the ``DoctrineNormalizer`` and ``RecursiveReflectionNormalizer`` from
Expand Down
4 changes: 2 additions & 2 deletions example/EchoTimeService.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use Bernard\Message\DefaultMessage;
use Bernard\Message\PlainMessage;

class EchoTimeService
{
public function echoTime(DefaultMessage $message)
public function echoTime(PlainMessage $message)
{
if (rand(0, 10) == 7) {
throw new \RuntimeException('I failed because rand was 7');
Expand Down
2 changes: 1 addition & 1 deletion example/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function produce() {
$producer = get_producer();

while (true) {
$producer->produce(new Message\DefaultMessage('EchoTime', array(
$producer->produce(new Message\PlainMessage('EchoTime', array(
'time' => time(),
)));

Expand Down
10 changes: 5 additions & 5 deletions spec/Bernard/Normalizer/EnvelopeNormalizerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function it_is_a_normalizer_and_denormailzer()
function it_normalizes_envelope_and_delegates_message_to_aggregate($envelope, $message, $aggregate)
{
$envelope->getMessage()->willReturn($message);
$envelope->getClass()->willReturn('Bernard\Message\DefaultMessage');
$envelope->getClass()->willReturn('Bernard\Message\PlainMessage');
$envelope->getTimestamp()->willReturn(1337);

$aggregate->normalize($message)->willReturn(array(
Expand All @@ -37,7 +37,7 @@ function it_normalizes_envelope_and_delegates_message_to_aggregate($envelope, $m
$this->setAggregateNormalizer($aggregate);

$this->normalize($envelope)->shouldReturn(array(
'class' => 'Bernard\\Message\\DefaultMessage',
'class' => 'Bernard\\Message\\PlainMessage',
'timestamp' => 1337,
'message' => array('key' => 'value'),
));
Expand All @@ -50,10 +50,10 @@ function it_denormalizes_envelope_and_delegates_message_to_aggregate($message, $
{
$this->setAggregateNormalizer($aggregate);

$aggregate->denormalize(array('key' => 'value'), 'Bernard\\Message\\DefaultMessage', null)->willReturn($message);
$aggregate->denormalize(array('key' => 'value'), 'Bernard\\Message\\PlainMessage', null)->willReturn($message);

$normalized = array(
'class' => 'Bernard\\Message\\DefaultMessage',
'class' => 'Bernard\\Message\\PlainMessage',
'timestamp' => 1337,
'message' => array('key' => 'value'),
);
Expand All @@ -62,6 +62,6 @@ function it_denormalizes_envelope_and_delegates_message_to_aggregate($message, $
$envelope->shouldHaveType('Bernard\\Envelope');
$envelope->getMessage()->shouldReturn($message);
$envelope->getTimestamp()->shouldReturn(1337);
$envelope->getClass()->shouldReturn('Bernard\\Message\\DefaultMessage');
$envelope->getClass()->shouldReturn('Bernard\\Message\\PlainMessage');
}
}
8 changes: 4 additions & 4 deletions spec/Bernard/SerializerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ function let($aggregate)
function it_serializes_normalized_envelope_into_json($envelope, $aggregate)
{
$aggregate->normalize($envelope, null)->willReturn(array(
'class' => 'Bernard\\Message\\DefaultMessage',
'class' => 'Bernard\\Message\\PlainMessage',
'timestamp' => 1337,
'message' => array('name' => 'Import', 'arguments' => array('arg1' => 'value')),
));

$this->serialize($envelope)
->shouldReturn('{"class":"Bernard\\\\Message\\\\DefaultMessage","timestamp":1337,"message":{"name":"Import","arguments":{"arg1":"value"}}}');
->shouldReturn('{"class":"Bernard\\\\Message\\\\PlainMessage","timestamp":1337,"message":{"name":"Import","arguments":{"arg1":"value"}}}');
}

/**
Expand All @@ -33,14 +33,14 @@ function it_serializes_normalized_envelope_into_json($envelope, $aggregate)
function it_unserializes_into_envelope($envelope, $aggregate)
{
$normalized = array(
'class' => 'Bernard\\Message\\DefaultMessage',
'class' => 'Bernard\\Message\\PlainMessage',
'timestamp' => 1337,
'message' => array('name' => 'Import', 'arguments' => array('arg1' => 'value')),
);

$aggregate->denormalize($normalized, 'Bernard\Envelope', null)->willReturn($envelope);

$this->unserialize('{"class":"Bernard\\\\Message\\\\DefaultMessage","timestamp":1337,"message":{"name":"Import","arguments":{"arg1":"value"}}}')
$this->unserialize('{"class":"Bernard\\\\Message\\\\PlainMessage","timestamp":1337,"message":{"name":"Import","arguments":{"arg1":"value"}}}')
->shouldReturn($envelope);
}
}
4 changes: 2 additions & 2 deletions src/Command/ProduceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Bernard\Command;

use Bernard\Producer;
use Bernard\Message\DefaultMessage;
use Bernard\Message\PlainMessage;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -55,6 +55,6 @@ public function execute(InputInterface $input, OutputInterface $output)
}
}

$this->producer->produce(new DefaultMessage($name, $message), $queue);
$this->producer->produce(new PlainMessage($name, $message), $queue);
}
}
87 changes: 3 additions & 84 deletions src/Message/DefaultMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,12 @@

namespace Bernard\Message;

use ArrayAccess;
@trigger_error('The '.__NAMESPACE__.'\DefaultMessage class is deprecated. Use '.__NAMESPACE__.'\PlainMessage instead.', E_USER_DEPRECATED);

/**
* Simple message that gets you started. It has a name an a array of arguments
* It does not enforce any types or properties so be careful on relying them
* being there.
*
* @package Bernard
* @deprecated Use PlainMessage instead
*/
class DefaultMessage extends AbstractMessage implements ArrayAccess
class DefaultMessage extends PlainMessage
{
protected $name;
protected $arguments;

/**
* @param string $name
* @param array $arguments
*/
public function __construct($name, array $arguments = [])
{
$this->name = $name;
$this->arguments = $arguments;
}

/**
* @return array
*/
public function all()
{
return $this->arguments;
}

/**
* @param string $name
*
* @return mixed
*/
public function get($name)
{
return $this->offsetGet($name);
}

/**
* @param string $name
*
* @return bool
*/
public function has($name)
{
return $this->offsetExists($name);
}

public function offsetExists($offset)
{
return array_key_exists($offset, $this->arguments);
}

public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->arguments[$offset] : null;
}

public function offsetSet($offset, $value)
{
throw new \LogicException('Message is immutable');
}

public function offsetUnset($offset)
{
throw new \LogicException('Message is immutable');
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}

public function __get($property)
{
return $this->offsetGet($property);
}

public function __set($property, $value)
{
$this->offsetSet($property, $value);
}
}
94 changes: 94 additions & 0 deletions src/Message/PlainMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Bernard\Message;

use ArrayAccess;

/**
* Simple message that gets you started. It has a name an a array of arguments
* It does not enforce any types or properties so be careful on relying them
* being there.
*
* @package Bernard
*/
class PlainMessage extends AbstractMessage implements ArrayAccess
{
protected $name;
protected $arguments;

/**
* @param string $name
* @param array $arguments
*/
public function __construct($name, array $arguments = [])
{
$this->name = $name;
$this->arguments = $arguments;
}

/**
* @return array
*/
public function all()
{
return $this->arguments;
}

/**
* @param string $name
*
* @return mixed
*/
public function get($name)
{
return $this->offsetGet($name);
}

/**
* @param string $name
*
* @return bool
*/
public function has($name)
{
return $this->offsetExists($name);
}

public function offsetExists($offset)
{
return array_key_exists($offset, $this->arguments);
}

public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->arguments[$offset] : null;
}

public function offsetSet($offset, $value)
{
throw new \LogicException('Message is immutable');
}

public function offsetUnset($offset)
{
throw new \LogicException('Message is immutable');
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}

public function __get($property)
{
return $this->offsetGet($property);
}

public function __set($property, $value)
{
$this->offsetSet($property, $value);
}
}
Loading

0 comments on commit ed1d019

Please sign in to comment.