2020 */
2121final 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