for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Gendoria\CommandQueueBundle\Serializer;
use Exception;
use Gendoria\CommandQueue\Command\CommandInterface;
use Gendoria\CommandQueue\Serializer\Exception\UnserializeErrorException;
use Gendoria\CommandQueue\Serializer\SerializedCommandData;
use Gendoria\CommandQueue\Serializer\SerializerInterface;
use JMS\Serializer\Serializer;
/**
* Serializer using JMS serializer module
*
* @author Tomasz Struczyński <[email protected]>
*/
class JmsSerializer implements SerializerInterface
{
* @var Serializer
private $serializer;
* Serialization format.
* @var string
private $format;
* Class constructor.
* @param Serializer $serializer
* @param string $format Serialization format.
public function __construct(Serializer $serializer, $format = "json")
$this->serializer = $serializer;
$this->format = $format;
}
* {@inheritdoc}
public function serialize(CommandInterface $command)
return new SerializedCommandData($this->serializer->serialize($command, $this->format), get_class($command));
public function unserialize(SerializedCommandData $serializedCommandData)
try {
$command = $this->serializer->deserialize($serializedCommandData->getSerializedCommand(), $serializedCommandData->getCommandClass(), $this->format);
} catch (Exception $e) {
throw new UnserializeErrorException($serializedCommandData, $e->getMessage(), $e->getCode(), $e);
if (!is_object($command) || !$command instanceof CommandInterface) {
throw new UnserializeErrorException($serializedCommandData, "Unserialized command should implement CommandInterface.");
return $command;