-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRepository.php
64 lines (53 loc) · 1.52 KB
/
Repository.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
61
62
63
64
<?php declare(strict_types=1);
namespace Igni\Storage\Driver\MongoDB;
use Igni\Storage\Driver\GenericRepository;
use Igni\Storage\Exception\RepositoryException;
use Igni\Storage\Storable;
abstract class Repository extends GenericRepository
{
public function get($id): Storable
{
$cursor = $this->connection->find(
$this->metaData->getSource(),
['_id' => $id],
['limit' => 1]
);
$cursor->hydrateWith($this->hydrator);
$entity = $cursor->current();
$cursor->close();
if (!$entity instanceof Storable) {
throw RepositoryException::forNotFound($id);
}
return $entity;
}
public function create(Storable $entity): Storable
{
// Support id auto-generation.
$entity->getId();
$data = $this->hydrator->extract($entity);
if (isset($data['id'])) {
$data['_id'] = $data['id'];
unset($data['id']);
}
$this->connection->insert(
$this->metaData->getSource(),
$data
);
return $entity;
}
public function remove(Storable $entity): Storable
{
$this->connection->remove(
$this->metaData->getSource(),
$entity->getId()->getValue()
);
return $entity;
}
public function update(Storable $entity): Storable
{
$this->connection->update(
$this->metaData->getSource(),
$this->hydrator->extract($entity)
);
}
}