Skip to content

Commit

Permalink
Added type to PrePublishMessage (#20)
Browse files Browse the repository at this point in the history
* Added type to `PrePublishMessage`

* minor

* Applied fixes from StyleCI

* Added more tests

* Removed rabbit MQ publisher

* Applied fixes from StyleCI

[ci skip] [skip ci]

* Removed monolog and added dummy logger

* Applied fixes from StyleCI

[ci skip] [skip ci]

* removed comments

* typo

* added comments

* Updated comment
  • Loading branch information
Nyholm committed Oct 6, 2016
1 parent ad16bfc commit 2cdfbe2
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 14 deletions.
11 changes: 11 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 0.2.2

### Added

- A type property was added to `PrePublishMessage`.
- Lots of code style changes

### Fixed

- Bug when configure the Serializer

## 0.2.1

### Added
Expand Down
3 changes: 2 additions & 1 deletion Command/MessageDispatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* A command that wil be called by dispatch-message.php.
* A command that will be called by dispatch-message.php.
* It will give a SimpleBus message envelope to the ConsumerWrapper.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand All @@ -34,6 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$secretKey = $this->getContainer()->getParameter('happyr.mq2php.secret_key');

if (!empty($secretKey)) {
// If we have a secret key we must validate the hash
if (!hash_equals(sha1($secretKey.$data), $hash)) {
throw new \Exception('Hash verification failed');
}
Expand Down
9 changes: 8 additions & 1 deletion DependencyInjection/Compiler/RegisterConsumers.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ public function process(ContainerBuilder $container)
*/
private function replaceArgumentWithReference(ContainerBuilder $container, $serviceId, $referenceId)
{
if (!$container->hasDefinition($serviceId) || !$container->hasDefinition($referenceId)) {
if (!$container->hasDefinition($serviceId)) {
return;
}

// If there is not $referenceId the $service has no use
if (!$container->hasDefinition($referenceId)) {
$container->removeDefinition($serviceId);

return;
}

Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/HappyrMq2phpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function load(array $configs, ContainerBuilder $container)

// Add default headers to the serializer
$def = $container->getDefinition($serializerId);
$def->replaceArgument(1, $config['message_headers']);
$def->replaceArgument(2, $config['message_headers']);

// Add the secret key as parameter
$container->setParameter('happyr.mq2php.secret_key', $config['secret_key']);
Expand Down
2 changes: 1 addition & 1 deletion Event/PreHandleMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symfony\Component\EventDispatcher\Event;

/**
* Before we start to receive a message.
* A message has been pulled from the queue and we are just about to start handling that message.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
Expand Down
23 changes: 21 additions & 2 deletions Event/PrePublishMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,27 @@ class PrePublishMessage extends Event
const NAME = 'happyr.mq2php.pre_publish_message';

/**
* This is the json message before we run json_encode.
*
* @var array
*/
private $message;

/**
* @param array $message
* The class/type from the original message (command/event).
*
* @var string
*/
private $type;

/**
* @param array $message
* @param string $type
*/
public function __construct(array $message)
public function __construct(array $message, $type)
{
$this->message = $message;
$this->type = $type;
}

/**
Expand All @@ -45,4 +56,12 @@ public function setMessage(array $message)

return $this;
}

/**
* @return string
*/
public function getType()
{
return $this->type;
}
}
10 changes: 5 additions & 5 deletions Service/MessageSerializerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public function __construct(
}

/**
* Serialize a Message by wrapping it in an Envelope and serializing the envelope. This will take the
* SimpleBus envelope and add it as body on a HTTP-like message.
* Serialize a Message by wrapping it in an Envelope and serializing the envelope. This decoration will
* take the SimpleBus envelope and add it in a json message.
*
* {@inheritdoc}
*/
public function wrapAndSerialize($message)
public function wrapAndSerialize($originalMessage)
{
$serializedMessage = $this->serializer->wrapAndSerialize($message);
$serializedMessage = $this->serializer->wrapAndSerialize($originalMessage);

$message = [];
foreach ($this->headers as $name => $value) {
Expand All @@ -74,7 +74,7 @@ public function wrapAndSerialize($message)
// Add a hash where the secret key is baked in.
$message['headers'][] = ['key' => 'hash', 'value' => sha1($this->secretKey.$serializedMessage)];

$event = new PrePublishMessage($message);
$event = new PrePublishMessage($message, is_object($originalMessage) ? get_class($originalMessage) : gettype($originalMessage));
$this->eventDispatcher->dispatch(PrePublishMessage::NAME, $event);

return json_encode($event->getMessage());
Expand Down
13 changes: 12 additions & 1 deletion Tests/Functional/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@

namespace Happyr\Mq2phpBundle\Tests\Functional;

use Happyr\Mq2phpBundle\Service\ConsumerWrapper;
use Happyr\Mq2phpBundle\Service\MessageSerializerDecorator;

class BundleInitializationTest extends BaseTestCase
{
public function testRegisterBundle()
{
static::createClient();
static::bootKernel();
$container = static::$kernel->getContainer();
$this->assertTrue($container->has('happyr.mq2php.message_serializer'));
$client = $container->get('happyr.mq2php.message_serializer');
$this->assertInstanceOf(MessageSerializerDecorator::class, $client);

$this->assertTrue($container->has('happyr.mq2php.consumer_wrapper'));
$client = $container->get('happyr.mq2php.consumer_wrapper');
$this->assertInstanceOf(ConsumerWrapper::class, $client);
}
}
4 changes: 4 additions & 0 deletions Tests/Functional/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use JMS\SerializerBundle\JMSSerializerBundle;
use SimpleBus\AsynchronousBundle\SimpleBusAsynchronousBundle;
use SimpleBus\JMSSerializerBundleBridge\SimpleBusJMSSerializerBundleBridgeBundle;
use SimpleBus\SymfonyBridge\SimpleBusCommandBusBundle;
use SimpleBus\SymfonyBridge\SimpleBusEventBusBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Kernel;
Expand Down Expand Up @@ -35,6 +37,8 @@ public function registerBundles()
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new SimpleBusCommandBusBundle(),
new SimpleBusEventBusBundle(),
new SimpleBusAsynchronousBundle(),
new SimpleBusJMSSerializerBundleBridgeBundle(),
new JMSSerializerBundle(),
Expand Down
44 changes: 44 additions & 0 deletions Tests/Functional/app/Service/DummyLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Happyr\Mq2phpBundle\Tests\Functional\app\Service;

use Psr\Log\LoggerInterface;

class DummyLogger implements LoggerInterface
{
public function emergency($message, array $context = [])
{
}

public function alert($message, array $context = [])
{
}

public function critical($message, array $context = [])
{
}

public function error($message, array $context = [])
{
}

public function warning($message, array $context = [])
{
}

public function notice($message, array $context = [])
{
}

public function info($message, array $context = [])
{
}

public function debug($message, array $context = [])
{
}

public function log($level, $message, array $context = [])
{
}
}
12 changes: 12 additions & 0 deletions Tests/Functional/app/Service/DummyPublisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Happyr\Mq2phpBundle\Tests\Functional\app\Service;

use SimpleBus\Asynchronous\Publisher\Publisher;

class DummyPublisher implements Publisher
{
public function publish($message)
{
}
}
7 changes: 7 additions & 0 deletions Tests/Functional/app/config/default.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
imports:
- { resource: framework.yml }
- { resource: services.yml }

happyr_mq2php:
enabled: true

simple_bus_asynchronous:
events:
publisher_service_id: 'happyr.mq2php.dummy_publisher'
commands:
publisher_service_id: 'happyr.mq2php.dummy_publisher'
6 changes: 6 additions & 0 deletions Tests/Functional/app/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
happyr.mq2php.dummy_publisher:
class: Happyr\Mq2phpBundle\Tests\Functional\app\Service\DummyPublisher

logger:
class: Happyr\Mq2phpBundle\Tests\Functional\app\Service\DummyLogger
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<directory suffix=".php">./</directory>
<exclude>
<directory>vendor</directory>
<directory>tests</directory>
<directory>Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
</phpunit>

0 comments on commit 2cdfbe2

Please sign in to comment.