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

Commit a8a3b58

Browse files
committed
Merge pull request #58 from easybiblabs/t/couchdb-and-mysql-check
T/couchdb and mysql check
2 parents 91b9b17 + 7e36bff commit a8a3b58

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* @license http://framework.zend.com/license/new-bsd New BSD License
4+
*/
5+
6+
namespace ZendDiagnostics\Check;
7+
8+
use ZendDiagnostics\Result;
9+
10+
/**
11+
* Ensures a connection to CouchDB is possible.
12+
*/
13+
class CouchDBCheck extends GuzzleHttpService
14+
{
15+
/**
16+
* @param array $couchDbSettings
17+
*
18+
* @return self
19+
*/
20+
public function __construct(array $couchDbSettings)
21+
{
22+
if (false === array_key_exists('url', $couchDbSettings)) {
23+
$couchDbUrl = $this->createUrlFromParameters($couchDbSettings);
24+
} else {
25+
$couchDbUrl = $couchDbSettings['url'];
26+
}
27+
28+
parent::__construct($couchDbUrl);
29+
}
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
public function check()
35+
{
36+
$result = parent::check();
37+
if ($result instanceof Result\Success) {
38+
return $result;
39+
}
40+
41+
$msg = $result->getMessage();
42+
$msg = preg_replace('=\/\/(.+):{1}(.+)(\@){1}=i', '//', $msg);
43+
44+
$failure = new Result\Failure($msg, $result->getData());
45+
46+
return $failure;
47+
}
48+
49+
/**
50+
* Assumes CouchDB defaults. Port 80 or 5984 is non-SSL, SSL otherwise.
51+
* Override with 'protocol' if you run something else.
52+
*
53+
* Requires/Supports the following keys in the array:
54+
*
55+
* - dbname
56+
* - host
57+
* - port
58+
* - protocol (optional)
59+
* - username (optional)
60+
* - password (optional)
61+
*
62+
* @param array $couchDbSettings
63+
*
64+
* @return string
65+
*/
66+
private function createUrlFromParameters(array $couchDbSettings)
67+
{
68+
$couchDbUrl = '';
69+
70+
if (array_key_exists('protocol', $couchDbSettings)) {
71+
$couchDbUrl .= $couchDbSettings['protocol'] . '://';
72+
} else {
73+
if ($couchDbSettings['port'] === '5984' || $couchDbSettings['port'] === '80') {
74+
$couchDbUrl .= 'http://';
75+
} else {
76+
$couchDbUrl .= 'https://';
77+
}
78+
}
79+
80+
if ($couchDbSettings['username'] && $couchDbSettings['password']) {
81+
$couchDbUrl .= sprintf(
82+
'%s:%s@',
83+
$couchDbSettings['username'],
84+
$couchDbSettings['password']
85+
);
86+
}
87+
88+
$couchDbUrl .= sprintf(
89+
'%s:%s/%s',
90+
$couchDbSettings['host'],
91+
$couchDbSettings['port'],
92+
$couchDbSettings['dbname']
93+
);
94+
95+
return $couchDbUrl;
96+
}
97+
}
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* @license http://framework.zend.com/license/new-bsd New BSD License
4+
*/
5+
6+
namespace ZendDiagnostics\Check;
7+
8+
use PDO;
9+
use ZendDiagnostics\Result;
10+
11+
/**
12+
* Ensures a connection to the MySQL server/database is possible.
13+
*/
14+
class PDOCheck implements CheckInterface
15+
{
16+
private $dsn;
17+
private $password;
18+
private $username;
19+
private $timeout;
20+
21+
/**
22+
* @param string $dsn
23+
* @param string $username
24+
* @param string $password
25+
* @param int $timeout
26+
*
27+
* @return self
28+
*/
29+
public function __construct($dsn, $username, $password, $timeout = 1)
30+
{
31+
$this->dsn = $dsn;
32+
$this->username = $username;
33+
$this->password = $password;
34+
$this->timeout = $timeout;
35+
}
36+
37+
/**
38+
* @return Result\Failure|Result\Success
39+
*/
40+
public function check()
41+
{
42+
$msg = 'Could not talk to database server';
43+
44+
try {
45+
$pdo = new PDO($this->dsn, $this->username, $this->password);
46+
47+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
48+
$pdo->setAttribute(PDO::ATTR_TIMEOUT, $this->timeout);
49+
50+
$status = $pdo->getAttribute(PDO::ATTR_CONNECTION_STATUS);
51+
if (null !== $status) {
52+
return new Result\Success('Connection to database server was successful.');
53+
}
54+
} catch (\PDOException $e) {
55+
// skip to failure
56+
$msg .= ', e: ' . $e->getCode();
57+
}
58+
59+
return new Result\Failure($msg);
60+
}
61+
62+
/**
63+
* @return string
64+
*/
65+
public function getLabel()
66+
{
67+
return 'Check if the database server can be reached';
68+
}
69+
}

tests/ZendDiagnosticsTest/GuzzleHttpServiceTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@
99
use Guzzle\Plugin\Mock\MockPlugin;
1010
use GuzzleHttp\Stream\Stream;
1111
use GuzzleHttp\Subscriber\Mock;
12+
use ZendDiagnostics\Check\CouchDBCheck;
1213
use ZendDiagnostics\Check\GuzzleHttpService;
1314

1415
class GuzzleHttpServiceTest extends \PHPUnit_Framework_TestCase
1516
{
17+
/**
18+
* @param array $params
19+
*
20+
* @dataProvider couchDbProvider
21+
*/
22+
public function testCouchDbCheck(array $params)
23+
{
24+
$check = new CouchDBCheck($params);
25+
$this->assertInstanceOf('ZendDiagnostics\Check\CouchDbCheck', $check);
26+
}
27+
1628
/**
1729
* @dataProvider checkProvider
1830
*/
@@ -84,6 +96,14 @@ public function checkProvider()
8496
);
8597
}
8698

99+
public function couchDbProvider()
100+
{
101+
return array(
102+
array(array('url' => 'http://root:party@localhost/hello')),
103+
array(array('host' => '127.0.0.1', 'port' => '443', 'username' => 'test', 'password' => 'test', 'dbname' => 'database')),
104+
);
105+
}
106+
87107
private function getMockGuzzle3Client($statusCode = 200, $content = null)
88108
{
89109
$plugin = new MockPlugin();

0 commit comments

Comments
 (0)