|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Gerrie package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Gerrie\Check; |
|
12
|
|
|
|
|
13
|
|
|
use Gerrie\Component\Database\Database; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Checks if the database connection works. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Andreas Grunwald <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class DatabaseConnectionCheck implements CheckInterface |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Database object |
|
25
|
|
|
* |
|
26
|
|
|
* @var Database |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $database; |
|
29
|
|
|
|
|
30
|
5 |
|
public function __construct(Database $database) |
|
31
|
|
|
{ |
|
32
|
5 |
|
$this->database = $database; |
|
33
|
5 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Executes the check itselfs |
|
37
|
|
|
* |
|
38
|
|
|
* @return boolean |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function check() |
|
41
|
|
|
{ |
|
42
|
3 |
|
$result = true; |
|
43
|
|
|
|
|
44
|
|
|
// TODO This check tries only to connect to the database |
|
45
|
|
|
// But it would make sense to check |
|
46
|
|
|
// a) insert, select, update, delete, etc. access |
|
47
|
|
|
// a) a) Which access rights do i need? Topic hardening? |
|
48
|
|
|
try { |
|
49
|
3 |
|
$this->database->connect($this->database->getConfig()); |
|
50
|
3 |
|
} catch (\Exception $e) { |
|
51
|
1 |
|
$result = false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
return $result; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the message, if the check succeeded |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function getSuccessMessage() |
|
63
|
|
|
{ |
|
64
|
1 |
|
$databaseConfig = $this->database->getConfig(); |
|
65
|
1 |
|
$message = 'Database connection to host "%s" works as expected.'; |
|
66
|
1 |
|
$message = sprintf($message, $databaseConfig['Host']); |
|
67
|
|
|
|
|
68
|
1 |
|
return $message; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns the message, if the check fails |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getFailureMessage() |
|
77
|
|
|
{ |
|
78
|
1 |
|
$databaseConfig = $this->database->getConfig(); |
|
79
|
1 |
|
$message = 'Database connection to host "%s" works not as expected. Please check your credentials or setup.'; |
|
80
|
1 |
|
$message = sprintf($message, $databaseConfig['Host']); |
|
81
|
|
|
|
|
82
|
1 |
|
return $message; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns if this check is optional or required. |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function isOptional() |
|
91
|
|
|
{ |
|
92
|
1 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|