|
1
|
|
|
<?php |
|
2
|
|
|
namespace BretRZaun\StatusPage\Check; |
|
3
|
|
|
use MongoDB\Client; |
|
4
|
|
|
|
|
5
|
|
|
use BretRZaun\StatusPage\Result; |
|
6
|
|
|
|
|
7
|
|
|
class MongoDbCheck extends AbstractCheck |
|
8
|
|
|
{ |
|
9
|
|
|
private $client; |
|
10
|
|
|
private $databases = []; |
|
11
|
|
|
private $collections = []; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(string $label, Client $client) |
|
14
|
|
|
{ |
|
15
|
|
|
parent::__construct($label); |
|
16
|
|
|
$this->client = $client; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function ensureDatabaseExists(string $database) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->databases[] = $database; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function ensureDatabaseHasCollection(string $database, string $collection) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->ensureDatabaseExists($database); |
|
27
|
|
|
if (!isset($this->collections[$database])) { |
|
28
|
|
|
$this->collections[$database] = []; |
|
29
|
|
|
} |
|
30
|
|
|
$this->collections[$database][] = $collection; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function checkStatus(): Result |
|
34
|
|
|
{ |
|
35
|
|
|
$result = new Result($this->label); |
|
36
|
|
|
try { |
|
37
|
|
|
$databases = $this->listDatabaseNames(); |
|
38
|
|
|
if (count($this->databases) > 0) { |
|
39
|
|
|
$this->checkDatabases($databases); |
|
40
|
|
|
} |
|
41
|
|
|
if (count($this->collections) > 0) { |
|
42
|
|
|
$this->checkCollections($databases); |
|
43
|
|
|
} |
|
44
|
|
|
} catch(\Exception $e) { |
|
45
|
|
|
$result->setError($e->getMessage()); |
|
46
|
|
|
} |
|
47
|
|
|
return $result; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function listDatabaseNames(): array |
|
51
|
|
|
{ |
|
52
|
|
|
$databaseNames = []; |
|
53
|
|
|
$iterator = $this->client->listDatabases(); |
|
54
|
|
|
foreach ($iterator as $dbInfo) { |
|
55
|
|
|
$databaseNames[] = $dbInfo->getName(); |
|
56
|
|
|
} |
|
57
|
|
|
return $databaseNames; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function listCollectionNames(string $databaseName): array |
|
61
|
|
|
{ |
|
62
|
|
|
$database = $this->client->selectDatabase($databaseName); |
|
63
|
|
|
$collectionNames = []; |
|
64
|
|
|
$iterator = $database->listCollections(); |
|
65
|
|
|
foreach ($iterator as $collectionInfo) { |
|
66
|
|
|
$collectionNames[] = $collectionInfo->getName(); |
|
67
|
|
|
} |
|
68
|
|
|
return $collectionNames; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function checkDatabases($databases) |
|
72
|
|
|
{ |
|
73
|
|
|
foreach($this->databases as $database) { |
|
74
|
|
|
if (!in_array($database, $databases)) { |
|
75
|
|
|
throw new \RuntimeException('Database '.$database.' does not exist'); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function checkCollections(array $databases) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($this->collections as $databaseName => $collections) { |
|
83
|
|
|
$collectionNames = $this->listCollectionNames($databaseName); |
|
84
|
|
|
foreach ($collections as $collection) { |
|
85
|
|
|
if (!in_array($collection, $collectionNames)) { |
|
86
|
|
|
throw new \RuntimeException('Collection '.$collection.' does not exist in database '.$databaseName); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.