1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Health\Checkers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use PragmaRX\Health\Support\Timer; |
7
|
|
|
use PragmaRX\Health\Support\Result; |
8
|
|
|
|
9
|
|
|
class Database extends Base |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @return Result |
13
|
|
|
*/ |
14
|
1 |
|
public function check() |
15
|
|
|
{ |
16
|
|
|
try { |
17
|
1 |
|
switch ($this->target->type) { |
|
|
|
|
18
|
|
|
case 'find_first_model': |
19
|
1 |
|
return $this->findFirstModel(); |
20
|
|
|
case 'raw_query': |
21
|
1 |
|
return $this->rawQuery(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
throw new \Exception( |
25
|
|
|
"Target type '{$this->target->type}' does not exists" |
26
|
|
|
); |
27
|
1 |
|
} catch (\Exception $exception) { |
28
|
1 |
|
report($exception); |
29
|
|
|
|
30
|
1 |
|
return $this->makeResultFromException($exception); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
protected function findFirstModel() |
35
|
|
|
{ |
36
|
|
|
collect($this->target->models)->each(function ($model) { |
|
|
|
|
37
|
1 |
|
instantiate($model)->first(); |
38
|
1 |
|
}); |
39
|
|
|
|
40
|
|
|
return $this->makeHealthyResult(); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
protected function getConnectionName() |
44
|
|
|
{ |
45
|
1 |
|
return $this->target->connection == 'default' |
|
|
|
|
46
|
1 |
|
? config('database.default') |
47
|
1 |
|
: $this->target->connection; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
protected function rawQuery() |
51
|
|
|
{ |
52
|
1 |
|
Timer::start(); |
53
|
|
|
|
54
|
1 |
|
DB::connection($this->getConnectionName())->select( |
55
|
1 |
|
DB::raw($this->target->query) |
|
|
|
|
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$took = round(Timer::stop(), 5); |
59
|
|
|
$tookHuman = "{$took}s"; |
60
|
|
|
|
61
|
|
|
$this->target->setDisplay($this->target->name." ({$tookHuman})"); |
62
|
|
|
|
63
|
|
|
$result = |
64
|
|
|
$took > $this->target->maximumTime |
|
|
|
|
65
|
|
|
? $this->makeResult( |
66
|
|
|
false, |
67
|
|
|
sprintf( |
68
|
|
|
$this->target->errorMessage, |
|
|
|
|
69
|
|
|
$took, |
70
|
|
|
$this->target->maximumTime |
71
|
|
|
) |
72
|
|
|
) |
73
|
|
|
: $this->makeHealthyResult(); |
74
|
|
|
|
75
|
|
|
$result->setValue($took)->setValueHuman($tookHuman); |
76
|
|
|
|
77
|
|
|
return $result; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.