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\Configuration\Configuration; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Check if the config is valid. |
17
|
|
|
* |
18
|
|
|
* @author Andreas Grunwald <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ConfigurationValidationCheck implements CheckInterface |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Configuration object |
25
|
|
|
* |
26
|
|
|
* @var Configuration |
27
|
|
|
*/ |
28
|
|
|
private $configuration = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set of missing configuration keys |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $missingConfiguration = []; |
36
|
|
|
|
37
|
5 |
|
public function __construct(Configuration $configuration) |
38
|
|
|
{ |
39
|
5 |
|
$this->configuration = $configuration; |
40
|
5 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Executes the check itselfs |
44
|
|
|
* |
45
|
|
|
* @return boolean |
46
|
|
|
*/ |
47
|
2 |
|
public function check() |
48
|
|
|
{ |
49
|
2 |
|
$result = true; |
50
|
|
|
|
51
|
|
|
$configurationSettings = [ |
52
|
2 |
|
'Database.Host', |
53
|
2 |
|
'Database.Username', |
54
|
2 |
|
'Database.Password', |
55
|
2 |
|
'Database.Port', |
56
|
2 |
|
'Database.Name', |
57
|
2 |
|
]; |
58
|
|
|
|
59
|
2 |
|
foreach ($configurationSettings as $setting) { |
60
|
2 |
|
if ($this->configuration->hasConfigurationKey($setting) === false) { |
61
|
1 |
|
$this->missingConfiguration[] = $setting; |
62
|
1 |
|
$result = false; |
63
|
1 |
|
} |
64
|
2 |
|
} |
65
|
|
|
|
66
|
2 |
|
return $result; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the message, if the check succeeded |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
1 |
|
public function getSuccessMessage() |
75
|
|
|
{ |
76
|
1 |
|
$message = 'Application is configured properly.'; |
77
|
1 |
|
return $message; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the message, if the check fails |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
public function getFailureMessage() |
86
|
|
|
{ |
87
|
1 |
|
$missingKeys = implode(', ', $this->missingConfiguration); |
88
|
|
|
|
89
|
1 |
|
$message = 'The configuration is not complete. '; |
90
|
1 |
|
$message .= 'Missing keys are %s. Please provide them as command options.'; |
91
|
1 |
|
$message = sprintf($message, $missingKeys); |
92
|
|
|
|
93
|
1 |
|
return $message; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns if this check is optional or required. |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
1 |
|
public function isOptional() |
102
|
|
|
{ |
103
|
1 |
|
return true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|