Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 74e09d5

Browse files
authored
Merge pull request #78 from tkachuk/feature/mongodb-check
Added checker for MongoDB
2 parents 831f6ad + f64fa6a commit 74e09d5

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ It currently ships with the following Diagnostic Checks:
2222
* [GuzzleHttpService](#guzzlehttpservice) - check if given http host is responding using Guzzle,
2323
* [HttpService](#httpservice) - check if given http host is responding,
2424
* [Memcache](#memcache) - check if memcache extension is loaded and given server is reachable,
25+
* [Mongo](#mongodb) - check if connection to MongoDb is possible,
2526
* [OpCacheMemory](#opcachememory) - check if the OpCache memory usage is below warning/critical thresholds,
2627
* [PDOCheck](#pdocheck) - check if connection is possible,
2728
* [PhpVersion](#phpversion) - make sure that PHP version matches constraint,
@@ -487,6 +488,18 @@ $checkLocal = new Memcache('127.0.0.1'); // default port
487488
$checkBackup = new Memcache('10.0.30.40', 11212);
488489
````
489490

491+
### MongoDb
492+
Check if connection to MongoDb is possible
493+
494+
````php
495+
<?php
496+
use ZendDiagnostics\Check\Mongo;
497+
498+
$mongoCheck = new Mongo('mongodb://127.0.0.1:27017');
499+
// and with user/password
500+
$mongoCheck = new Mongo('mongodb://user:[email protected]:27017');
501+
````
502+
490503
### PhpVersion
491504

492505
Check if current PHP version matches the given requirement. The test accepts 2 parameters - baseline version and

src/ZendDiagnostics/Check/Mongo.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace ZendDiagnostics\Check;
3+
4+
use ZendDiagnostics\Result\Success;
5+
use ZendDiagnostics\Result\Failure;
6+
7+
class Mongo extends AbstractCheck
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $connectionUri;
13+
14+
/**
15+
* @param string $connectionUri
16+
*/
17+
public function __construct($connectionUri)
18+
{
19+
$this->connectionUri = $connectionUri;
20+
}
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function check()
26+
{
27+
try {
28+
$this->getListDBs();
29+
} catch (\Exception $e) {
30+
return new Failure(sprintf('Failed to connect to MongoDB server. Reason: `%s`', $e->getMessage()));
31+
}
32+
33+
return new Success();
34+
}
35+
36+
/**
37+
* @return array|\Iterator
38+
*
39+
* @throws \RuntimeException
40+
* @throws \MongoDB\Driver\Exception
41+
* @throws \MongoConnectionException
42+
*/
43+
private function getListDBs()
44+
{
45+
if (class_exists('\MongoDB\Client')) {
46+
return (new \MongoDB\Client($this->connectionUri))->listDatabases();
47+
} elseif (class_exists('\MongoClient')) {
48+
return (new \MongoClient($this->server))->listDBs();
49+
}
50+
51+
throw new \RuntimeException('Neither the mongo extension or mongodb are installed');
52+
}
53+
}

0 commit comments

Comments
 (0)