Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Messaging/FQCNMessageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the prooph/common.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 7/26/15 - 12:11 AM
*/
namespace Prooph\Common\Messaging;
use Assert\Assertion;

/**
* Class FQCNMessageFactory
*
* @package Prooph\Common\Messaging
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class FQCNMessageFactory implements MessageFactory
{
/**
* @param string $messageName
* @param array $messageData
* @throws \UnexpectedValueException
* @return DomainMessage
*/
public function createMessageFromArray($messageName, array $messageData)
{
if (! class_exists($messageName)) {
throw new \UnexpectedValueException('Given message name is not a valid class: ' . (string)$messageName);
}

$ref = new \ReflectionClass($messageName);

if (!$ref->isSubclassOf(DomainMessage::class)) {
throw new \UnexpectedValueException(sprintf(
'Message class %s is not a sub class of %s',
$messageName,
DomainMessage::class
));
}

$messageData['name'] = $messageName;

return $messageName::fromArray($messageData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 5/9/15 - 9:57 PM
* Date: 7/26/15 - 12:08 AM
*/

namespace Prooph\Common\ServiceLocator;
namespace Prooph\Common\Messaging;

/**
* Interface ServiceInitializer
* Interface MessageFactory
*
* @package Prooph\Common\ServiceLocator
* @package Prooph\Common\Messaging
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
interface ServiceInitializer
interface MessageFactory
{
/**
* @param mixed $service
* @param ServiceLocator $serviceLocator
* @param string $messageName
* @param array $messageData
* @return DomainMessage
*/
public function initialize($service, ServiceLocator $serviceLocator);
public function createMessageFromArray($messageName, array $messageData);
}
51 changes: 0 additions & 51 deletions src/ServiceLocator/ServiceLocator.php

This file was deleted.

69 changes: 0 additions & 69 deletions src/ServiceLocator/ZF2/Zf2InitializerProxy.php

This file was deleted.

125 changes: 0 additions & 125 deletions src/ServiceLocator/ZF2/Zf2ServiceManagerProxy.php

This file was deleted.

58 changes: 58 additions & 0 deletions tests/Messaging/FQCNMessageFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/*
* This file is part of the prooph/common.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 7/26/15 - 12:14 AM
*/
namespace ProophTest\Common\Messaging;
use Prooph\Common\Messaging\FQCNMessageFactory;
use ProophTest\Common\Mock\DoSomething;
use Rhumsaa\Uuid\Uuid;

/**
* Class FQCNMessageFactoryTest
*
* @package ProophTest\Common\Messaging
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
final class FQCNMessageFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var FQCNMessageFactory
*/
private $messageFactory;

protected function setUp()
{
$this->messageFactory = new FQCNMessageFactory();
}

/**
* @test
*/
function it_creates_a_new_message_from_array_and_fqcn()
{
$uuid = Uuid::uuid4();
$createdAt = new \DateTimeImmutable();


$command = $this->messageFactory->createMessageFromArray(DoSomething::class, [
'uuid' => $uuid->toString(),
'version' => 1,
'payload' => ['command' => 'payload'],
'metadata' => ['command' => 'metadata'],
'created_at' => $createdAt->format(\DateTime::ISO8601),
]);

$this->assertEquals(DoSomething::class, $command->messageName());
$this->assertEquals($uuid->toString(), $command->uuid()->toString());
$this->assertEquals($createdAt->format(\DateTime::ISO8601), $command->createdAt()->format(\DateTime::ISO8601));
$this->assertEquals(1, $command->version());
$this->assertEquals(['command' => 'payload'], $command->payload());
$this->assertEquals(['command' => 'metadata'], $command->metadata());
}
}
Loading