Skip to content

Commit f2fa341

Browse files
committed
improve api
1 parent 7d898bb commit f2fa341

8 files changed

Lines changed: 124 additions & 39 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"infection/infection": "^0.31.9",
3030
"mongodb/mongodb": "^2.1",
3131
"patchlevel/coding-standard": "^1.3.0",
32-
"patchlevel/rango": "1.0.0-alpha.2",
32+
"patchlevel/rango": "1.0.0-alpha.3",
3333
"phpat/phpat": "^0.12.0",
3434
"phpbench/phpbench": "^1.2.15",
3535
"phpstan/phpstan": "^2.1.32",

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Metadata/MultipleIdPropertiesFound.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
use RuntimeException;
88

9+
use function sprintf;
10+
911
class MultipleIdPropertiesFound extends RuntimeException
1012
{
1113
public function __construct(string $className)
1214
{
15+
parent::__construct(sprintf('Multiple id properties found in class %s.', $className));
1316
}
1417
}

src/Metadata/NoIdPropertyFound.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
use RuntimeException;
88

9+
use function sprintf;
10+
911
class NoIdPropertyFound extends RuntimeException
1012
{
1113
public function __construct(string $className)
1214
{
15+
parent::__construct(sprintf('No id property found in class %s.', $className));
1316
}
1417
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\ODM\Repository;
6+
7+
use RuntimeException;
8+
9+
use function sprintf;
10+
11+
final class DocumentNotFound extends RuntimeException
12+
{
13+
/** @param class-string $documentClass */
14+
public function __construct(
15+
string $documentClass,
16+
string $id,
17+
string $database,
18+
string $collection,
19+
) {
20+
parent::__construct(sprintf(
21+
'Document "%s" with id "%s" not found in database "%s" and collection "%s".',
22+
$documentClass,
23+
$id,
24+
$database,
25+
$collection,
26+
));
27+
}
28+
}

src/Repository/MongoDBRepository.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
*/
2121
final readonly class MongoDBRepository implements Repository
2222
{
23+
private Collection $collection;
24+
2325
/** @param DocumentMetadata<T> $metadata */
2426
public function __construct(
2527
private Database $database,
2628
private DocumentMetadata $metadata,
2729
private Hydrator $hydrator,
2830
) {
31+
$this->collection = $this->database->selectCollection($this->metadata->collection);
2932
}
3033

3134
/** @param T $object */
@@ -37,13 +40,34 @@ public function persist(object $object): void
3740

3841
$data = $this->hydrator->extract($object);
3942

40-
$this->collection()->insertOne($data);
43+
$this->collection->insertOne($data);
44+
}
45+
46+
/**
47+
* @return T
48+
*
49+
* @throws DocumentNotFound
50+
*/
51+
public function get(string $id): object
52+
{
53+
$object = $this->find($id);
54+
55+
if (!$object) {
56+
throw new DocumentNotFound(
57+
$this->metadata->className,
58+
$id,
59+
$this->collection->getDatabaseName(),
60+
$this->collection->getCollectionName(),
61+
);
62+
}
63+
64+
return $object;
4165
}
4266

4367
/** @return T|null */
4468
public function find(string $id): object|null
4569
{
46-
$data = $this->collection()->findOne(['_id' => $id], [
70+
$data = $this->collection->findOne(['_id' => $id], [
4771
'typeMap' => ['root' => 'array', 'document' => 'array'],
4872
]);
4973

@@ -56,13 +80,13 @@ public function find(string $id): object|null
5680

5781
public function remove(string $id): void
5882
{
59-
$this->collection()->deleteOne(['_id' => $id]);
83+
$this->collection->deleteOne(['_id' => $id]);
6084
}
6185

6286
/** @return iterable<T> */
6387
public function findAll(): iterable
6488
{
65-
$cursor = $this->collection()->find([], [
89+
$cursor = $this->collection->find([], [
6690
'typeMap' => ['root' => 'array', 'document' => 'array'],
6791
]);
6892

@@ -98,7 +122,7 @@ public function findBy(array $filter, array|null $orderBy = null, int|null $limi
98122

99123
$options['typeMap'] = ['root' => 'array', 'document' => 'array'];
100124

101-
$cursor = $this->collection()->find($filter, $options);
125+
$cursor = $this->collection->find($filter, $options);
102126

103127
foreach ($cursor as $document) {
104128
yield $this->hydrator->hydrate($this->metadata->className, $document);
@@ -125,19 +149,19 @@ public function findOneBy(array $filter = [], array|null $orderBy = null): objec
125149
);
126150
}
127151

128-
$data = $this->collection()->findOne($filter, $options);
152+
$data = $this->collection->findOne($filter, $options);
129153

130154
return $data ? $this->hydrator->hydrate($this->metadata->className, $data) : null;
131155
}
132156

133157
public function count(): int
134158
{
135-
return $this->collection()->countDocuments();
159+
return $this->collection->countDocuments();
136160
}
137161

138162
public function has(string $id): bool
139163
{
140-
return $this->collection()->countDocuments(['_id' => $id]) > 0;
164+
return $this->collection->countDocuments(['_id' => $id]) > 0;
141165
}
142166

143167
public function database(): Database
@@ -148,7 +172,7 @@ public function database(): Database
148172
/** @return Collection<array<string, mixed>> */
149173
public function collection(): Collection
150174
{
151-
return $this->database->selectCollection($this->metadata->collection);
175+
return $this->collection;
152176
}
153177

154178
/** @return DocumentMetadata<T> */
@@ -165,15 +189,13 @@ public function createCollection(): void
165189

166190
public function dropCollection(): void
167191
{
168-
$this->collection()->drop();
192+
$this->collection->drop();
169193
}
170194

171195
public function updateIndexes(bool $dropUnknown = false): void
172196
{
173-
$collection = $this->collection();
174-
175197
$existingIndexes = [];
176-
foreach (iterator_to_array($collection->listIndexes()) as $index) {
198+
foreach (iterator_to_array($this->collection->listIndexes()) as $index) {
177199
$existingIndexes[$index['name']] = true;
178200
}
179201

@@ -190,7 +212,7 @@ public function updateIndexes(bool $dropUnknown = false): void
190212
$index->keys,
191213
);
192214

193-
$collection->createIndex($keys, [
215+
$this->collection->createIndex($keys, [
194216
'name' => $index->name,
195217
'unique' => $index->unique,
196218
]);
@@ -202,7 +224,7 @@ public function updateIndexes(bool $dropUnknown = false): void
202224
return;
203225
}
204226

205-
foreach (iterator_to_array($collection->listIndexes()) as $index) {
227+
foreach (iterator_to_array($this->collection->listIndexes()) as $index) {
206228
if (in_array($index['name'], $desiredNames, true)) {
207229
continue;
208230
}
@@ -212,7 +234,7 @@ public function updateIndexes(bool $dropUnknown = false): void
212234
continue;
213235
}
214236

215-
$collection->dropIndex($index['name']);
237+
$this->collection->dropIndex($index['name']);
216238
}
217239
}
218240
}

0 commit comments

Comments
 (0)