Skip to content

Commit 49f98ee

Browse files
committed
(fix): address PR review comments - return type safety, read hook init, index default, mirror null handling
1 parent 5fb45e5 commit 49f98ee

4 files changed

Lines changed: 27 additions & 16 deletions

File tree

src/Database/Adapter/Mongo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ protected function syncReadHooks(): void
184184
*/
185185
protected function applyReadFilters(array $filters, string $collection, string $forPermission = 'read'): array
186186
{
187+
$this->syncReadHooks();
187188
foreach ($this->readHooks as $hook) {
188189
$filters = $hook->applyFilters($filters, $collection, $forPermission);
189190
}

src/Database/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function fromDocument(Document $document): self
7474
/** @var string $key */
7575
$key = $document->getAttribute('key', $document->getId());
7676
/** @var string $type */
77-
$type = $document->getAttribute('type', 'index');
77+
$type = $document->getAttribute('type', IndexType::Key->value);
7878
/** @var array<string> $attributes */
7979
$attributes = $document->getAttribute('attributes', []);
8080
/** @var array<int> $lengths */

src/Database/Mirror.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -401,20 +401,22 @@ public function createAttribute(string $collection, Attribute $attribute): bool
401401
$document = $attribute->toDocument();
402402

403403
foreach ($this->writeFilters as $filter) {
404-
$filtered = $filter->beforeCreateAttribute(
404+
$document = $filter->beforeCreateAttribute(
405405
source: $this->source,
406406
destination: $this->destination,
407407
collectionId: $collection,
408408
attributeId: $attribute->key,
409409
attribute: $document,
410410
);
411-
if ($filtered !== null) {
412-
$document = $filtered;
411+
if ($document === null) {
412+
break;
413413
}
414414
}
415415

416-
$filteredAttribute = Attribute::fromDocument($document);
417-
$result = $this->destination->createAttribute($collection, $filteredAttribute);
416+
if ($document !== null) {
417+
$filteredAttribute = Attribute::fromDocument($document);
418+
$result = $this->destination->createAttribute($collection, $filteredAttribute);
419+
}
418420
} catch (Throwable $err) {
419421
$this->logError('createAttribute', $err);
420422
}
@@ -441,25 +443,29 @@ public function createAttributes(string $collection, array $attributes): bool
441443
$document = $attribute->toDocument();
442444

443445
foreach ($this->writeFilters as $filter) {
444-
$filtered = $filter->beforeCreateAttribute(
446+
$document = $filter->beforeCreateAttribute(
445447
source: $this->source,
446448
destination: $this->destination,
447449
collectionId: $collection,
448450
attributeId: $attribute->key,
449451
attribute: $document,
450452
);
451-
if ($filtered !== null) {
452-
$document = $filtered;
453+
if ($document === null) {
454+
break;
453455
}
454456
}
455457

456-
$filteredAttributes[] = Attribute::fromDocument($document);
458+
if ($document !== null) {
459+
$filteredAttributes[] = Attribute::fromDocument($document);
460+
}
457461
}
458462

459-
$result = $this->destination->createAttributes(
460-
$collection,
461-
$filteredAttributes,
462-
);
463+
if ($filteredAttributes !== []) {
464+
$result = $this->destination->createAttributes(
465+
$collection,
466+
$filteredAttributes,
467+
);
468+
}
463469
} catch (Throwable $err) {
464470
$this->logError('createAttributes', $err);
465471
}

src/Database/Query.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public function __construct(Method|string $method, string $attribute = '', array
4444
public static function parse(string $query): static
4545
{
4646
try {
47-
return parent::parse($query);
47+
$parsed = parent::parse($query);
48+
49+
return new static($parsed->getMethod(), $parsed->getAttribute(), $parsed->getValues());
4850
} catch (BaseQueryException $e) {
4951
throw new QueryException($e->getMessage(), $e->getCode(), $e);
5052
}
@@ -58,7 +60,9 @@ public static function parse(string $query): static
5860
public static function parseQuery(array $query): static
5961
{
6062
try {
61-
return parent::parseQuery($query);
63+
$parsed = parent::parseQuery($query);
64+
65+
return new static($parsed->getMethod(), $parsed->getAttribute(), $parsed->getValues());
6266
} catch (BaseQueryException $e) {
6367
throw new QueryException($e->getMessage(), $e->getCode(), $e);
6468
}

0 commit comments

Comments
 (0)