-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mongo.php
60 lines (52 loc) · 1.34 KB
/
Mongo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Knp\Event\Store;
use Knp\Event\Store;
use Knp\Event\Event;
use \MongoDB;
use \MongoCollection;
use \MongoBinData;
use Knp\Event\Serializer;
use Knp\Event\Store\NoResult;
class Mongo implements Store
{
private $events;
private $serializer;
public function __construct(MongoDB $events, Serializer $serializer)
{
$this->events = $events;
$this->serializer = $serializer;
}
public function add(Event $event)
{
$this->events->selectCollection($event->getProviderClass())->insert(
$this->serializer->serialize($event)
);
}
public function byProvider($class, $id)
{
$documents = $this->events->selectCollection($class)->find([
'provider_id' => (string)$id,
]);
if (0 === $documents->count()) {
throw new NoResult;
}
return new CursorIterator($documents, $this->serializer);
}
}
/**
* TODO tmp
* see https://jira.mongodb.org/browse/PHP-820
* see https://jira.mongodb.org/browse/PHP-977
**/
class CursorIterator extends \IteratorIterator
{
public function __construct(\Traversable $t, $serializer)
{
parent::__construct($t);
$this->serializer = $serializer;
}
public function current()
{
return $this->serializer->unserialize(parent::current());
}
}