Skip to content

Commit

Permalink
Merge pull request #199 from vincentchalamon/master
Browse files Browse the repository at this point in the history
Fix #198: Support mongodb PHP extension
  • Loading branch information
lsmith77 committed Nov 23, 2018
2 parents e8c9bbc + f818541 commit ba75134
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions Check/DoctrineMongoDb.php
Expand Up @@ -3,6 +3,8 @@
namespace Liip\MonitorBundle\Check;

use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\ConnectionException;
use ZendDiagnostics\Check\AbstractCheck;
use ZendDiagnostics\Result\Success;

Expand All @@ -19,18 +21,36 @@ public function __construct(ManagerRegistry $registry, $connectionName = null)

public function check()
{
$connection = $this->manager->getConnection();
$connection->connect();

if ($connection->isConnected()) {
return new Success();
$connection = $this->manager->getConnection($this->connectionName);

// Using "mongo" PHP extension
if (\method_exists($connection, 'connect')) {
$connection->connect();

if ($connection->isConnected()) {
return new Success();
}

return new Failure(
sprintf(
'Connection "%s" is unavailable.',
$this->connectionName
)
);
}

// Using "mongodb" PHP extension
try {
$connection->getManager()->executeCommand('test', new Command(['ping' => 1]));
} catch (ConnectionException $e) {
return new Failure(
sprintf(
'Connection "%s" is unavailable.',
$this->connectionName
)
);
}

return new Failure(
sprintf(
'Connection "%s" is unavailable.',
$this->connectionName
)
);
return new Success();
}
}

0 comments on commit ba75134

Please sign in to comment.