Skip to content

Commit

Permalink
MongoDb storage driver: not rely on default typeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeygeno committed Sep 12, 2023
1 parent 49aa3ae commit 1044f1f
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Storage/MongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use MongoDB\Client;
use MongoDB\Collection;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;

/**
* MongoDb storage implementation
Expand All @@ -12,6 +14,15 @@ class MongoDb implements I
{
protected Client $client;

/**
* @see https://www.php.net/manual/en/mongodb-driver-cursor.settypemap.php
*/
private const TYPE_MAP = [
'array' => BSONArray::class,
'document' => BSONDocument::class,
'root' => BSONDocument::class,
];

/**
* @var array<mixed>
*/
Expand Down Expand Up @@ -85,17 +96,17 @@ public function sessionUp(string $sessionId, int $otp, int $sessionExpSecs, int
];

$this->collection($this->config['collection_session'])
->updateOne(['id' => $sessionId], ['$set' => $sessionSet], ['upsert' => true/*, 'session'=> $session*/]);
->updateOne(['id' => $sessionId], ['$set' => $sessionSet], ['upsert' => true]);

$sessionCounterSetOnInsert = [
// New datetime on creation, no changes on update!
'created' => new \MongoDb\BSON\UTCDateTime()
];

$this->collection($this->config['collection_session_counter'])
->updateOne(['id' => $sessionId], ['$setOnInsert' => $sessionCounterSetOnInsert], ['upsert' => true, /*'session'=> $session*/]);
->updateOne(['id' => $sessionId], ['$setOnInsert' => $sessionCounterSetOnInsert], ['upsert' => true]);
$this->collection($this->config['collection_session_counter'])
->updateOne(['id' => $sessionId], ['$inc' => ['count' => 1]], [/*'session'=> $session*/]);
->updateOne(['id' => $sessionId], ['$inc' => ['count' => 1]]);

// Indexes
if ($this->config['indexes']) {
Expand All @@ -120,7 +131,7 @@ public function sessionDown(string $sessionId): self
public function sessionCounter(string $sessionId): int
{
$sessionCounter = $this->collection($this->config['collection_session_counter'])
->findOne(['id' => $sessionId], ['projection' => ['count' => 1]]);
->findOne(['id' => $sessionId], ['projection' => ['count' => 1], 'typeMap' => self::TYPE_MAP]);
return ($sessionCounter and !empty($sessionCounter->count)) ? $sessionCounter->count : 0;
}

Expand All @@ -130,7 +141,7 @@ public function sessionCounter(string $sessionId): int
public function otp(string $sessionId): int
{
$session = $this->collection($this->config['collection_session'])
->findOne(['id' => $sessionId], ['projection' => ['otp' => 1]]);
->findOne(['id' => $sessionId], ['projection' => ['otp' => 1], 'typeMap' => self::TYPE_MAP]);
return ($session and !empty($session->otp)) ? $session->otp : 0;
}

Expand All @@ -150,7 +161,7 @@ public function otpCheckIncrement(string $sessionId): self
public function otpCheckCounter(string $sessionId): int
{
$session = $this->collection($this->config['collection_session'])
->findOne(['id' => $sessionId], ['projection' => ['otp_check_count' => 1]]);
->findOne(['id' => $sessionId], ['projection' => ['otp_check_count' => 1], 'typeMap' => self::TYPE_MAP]);
return ($session and !empty($session->otp_check_count)) ? $session->otp_check_count : 0;
}
}

0 comments on commit 1044f1f

Please # to comment.