Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/serializer #12

Merged
merged 4 commits into from
Apr 6, 2017
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"prefer-stable": true,
"require": {
"php": "^7.1",
"prooph/snapshot-store" : "^1.0",
"prooph/snapshot-store" : "^1.1"
"mongodb/mongodb": "^1.1.0"
},
"require-dev": {
Expand Down
1 change: 1 addition & 0 deletions docs/interop_factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Sample configuration:
'wtimeout' => 0, // How long to wait (in milliseconds) for secondaries before failing.
'journal' => false, // Wait until mongod has applied the write to the journal.
],
'serializer' => 'My\Serializer' //<-- optional, service name of a custom serializer
],
],
],
Expand Down
8 changes: 7 additions & 1 deletion src/Container/MongoDbSnapshotStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use Interop\Config\RequiresMandatoryOptions;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\WriteConcern;
use Prooph\SnapshotStore\CallbackSerializer;
use Prooph\SnapshotStore\MongoDb\MongoDbSnapshotStore;
use Prooph\SnapshotStore\Serializer;
use Psr\Container\ContainerInterface;

class MongoDbSnapshotStoreFactory implements ProvidesDefaultOptions, RequiresConfigId, RequiresMandatoryOptions
Expand Down Expand Up @@ -71,13 +73,16 @@ public function __invoke(ContainerInterface $container): MongoDbSnapshotStore
$config['write_concern']['journal']
);

$serializer = $config['serializer'] instanceof Serializer ? $config['serializer'] : $container->get($config['serializer']);

return new MongoDbSnapshotStore(
$client,
$config['db_name'],
$config['snapshot_grid_fs_map'],
$config['default_snapshot_grid_fs_name'],
$readConcern,
$writeConcern
$writeConcern,
$serializer
);
}

Expand All @@ -102,6 +107,7 @@ public function defaultOptions(): iterable
'wtimeout' => 0, // How long to wait (in milliseconds) for secondaries before failing.
'journal' => false, // Wait until mongod has applied the write to the journal.
],
'serializer' => new CallbackSerializer(null, null),
];
}

Expand Down
15 changes: 12 additions & 3 deletions src/MongoDbSnapshotStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\WriteConcern;
use MongoDB\GridFS\Exception\FileNotFoundException;
use Prooph\SnapshotStore\CallbackSerializer;
use Prooph\SnapshotStore\Serializer;
use Prooph\SnapshotStore\Snapshot;
use Prooph\SnapshotStore\SnapshotStore;

Expand Down Expand Up @@ -55,13 +57,19 @@ final class MongoDbSnapshotStore implements SnapshotStore
*/
private $writeConcern;

/**
* @var Serializer
*/
private $serializer;

public function __construct(
Client $client,
string $dbName,
array $snapshotGridFsMap = [],
string $defaultSnapshotGridFsName = 'snapshots',
ReadConcern $readConcern = null,
WriteConcern $writeConcern = null
WriteConcern $writeConcern = null,
Serializer $serializer = null
) {
if (null === $readConcern) {
$readConcern = new ReadConcern(ReadConcern::LOCAL);
Expand All @@ -77,6 +85,7 @@ public function __construct(
$this->defaultSnapshotGridFsName = $defaultSnapshotGridFsName;
$this->readConcern = $readConcern;
$this->writeConcern = $writeConcern;
$this->serializer = $serializer ?: new CallbackSerializer(null, null);
}

public function get(string $aggregateType, string $aggregateId): ?Snapshot
Expand All @@ -98,7 +107,7 @@ public function get(string $aggregateType, string $aggregateId): ?Snapshot

$destination = $this->createStream();
stream_copy_to_stream($stream, $destination);
$aggregateRoot = unserialize(stream_get_contents($destination, -1, 0));
$aggregateRoot = $this->serializer->unserialize(stream_get_contents($destination, -1, 0));

return new Snapshot(
$aggregateType,
Expand Down Expand Up @@ -129,7 +138,7 @@ public function save(Snapshot ...$snapshots): void

$bucket->uploadFromStream(
$aggregateId,
$this->createStream(serialize($snapshot->aggregateRoot())),
$this->createStream($this->serializer->serialize($snapshot->aggregateRoot())),
[
'_id' => $aggregateId,
'metadata' => [
Expand Down
26 changes: 26 additions & 0 deletions tests/Container/MongoDbSnapshotStoreFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace ProophTest\SnapshotStore\MongoDb\Container;

use PHPUnit\Framework\TestCase;
use Prooph\SnapshotStore\CallbackSerializer;
use Prooph\SnapshotStore\MongoDb\Container\MongoDbSnapshotStoreFactory;
use Prooph\SnapshotStore\MongoDb\MongoDbSnapshotStore;
use ProophTest\SnapshotStore\MongoDb\TestUtil;
Expand Down Expand Up @@ -53,4 +54,29 @@ public function it_throws_exception_when_invalid_container_given(): void
$eventStoreName = 'custom';
MongoDbSnapshotStoreFactory::$eventStoreName('invalid container');
}

/**
* @test
*/
public function it_gets_serializer_from_container_when_not_instanceof_serializer(): void
{
$config['prooph']['mongodb_snapshot_store']['default'] = [
'mongo_client_service' => 'my_connection',
'db_name' => 'foo',
'serializer' => 'serializer_servicename',
];

$client = TestUtil::getClient();

$container = $this->prophesize(ContainerInterface::class);

$container->get('my_connection')->willReturn($client)->shouldBeCalled();
$container->get('config')->willReturn($config)->shouldBeCalled();
$container->get('serializer_servicename')->willReturn(new CallbackSerializer(function() {}, function() {}))->shouldBeCalled();

$factory = new MongoDbSnapshotStoreFactory();
$snapshotStore = $factory($container->reveal());

$this->assertInstanceOf(MongoDbSnapshotStore::class, $snapshotStore);
}
}