These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PragmaRX\Health\Support\Traits; |
||
4 | |||
5 | use PragmaRX\Health\Data\Models\HealthCheck; |
||
6 | |||
7 | trait Database |
||
8 | { |
||
9 | protected $database; |
||
10 | |||
11 | 1 | public function loadDatabase() |
|
12 | { |
||
13 | 1 | $this->database = $this->__load(); |
|
14 | 1 | } |
|
15 | |||
16 | /** |
||
17 | * Load cache. |
||
18 | * |
||
19 | * @return \Illuminate\Support\Collection |
||
20 | */ |
||
21 | 1 | View Code Duplication | public function __load() |
0 ignored issues
–
show
|
|||
22 | { |
||
23 | 1 | if (! file_exists($file = $this->getDatabaseFileName())) { |
|
24 | 1 | return collect(); |
|
25 | } |
||
26 | |||
27 | return collect(json_decode(file_get_contents($file), true)); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Get cache filename. |
||
32 | * |
||
33 | * @return string|null |
||
34 | */ |
||
35 | 1 | protected function getDatabaseFileName() |
|
36 | { |
||
37 | 1 | return $this->target->saveTo; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * Check if database is enabled. |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | 1 | protected function databaseEnabled() |
|
46 | { |
||
47 | 1 | return config('health.database.enabled'); |
|
48 | } |
||
49 | |||
50 | protected function saveResultsToDatabase($target, $result) |
||
51 | { |
||
52 | HealthCheck::create([ |
||
53 | 'resource_name' => $resource = $target->resource->name, |
||
54 | 'resource_slug' => $target->resource->slug, |
||
55 | 'target_name' => $target->name, |
||
56 | 'target_slug' => str_slug($target->name), |
||
57 | 'target_display' => $target->display, |
||
58 | 'healthy' => $result->healthy, |
||
59 | 'error_message' => $result->errorMessage, |
||
60 | 'runtime' => $result->elapsedTime, |
||
61 | 'value' => $result->value, |
||
62 | 'value_human' => $result->valueHuman, |
||
63 | ]); |
||
64 | |||
65 | return HealthCheck::where([ |
||
66 | 'resource_slug' => $target->resource->slug, |
||
67 | 'target_name' => $target->name, |
||
68 | ]) |
||
69 | ->orderBy('created_at', 'desc') |
||
70 | ->take(config('health.database.max_records')) |
||
71 | ->get(); |
||
72 | } |
||
73 | } |
||
74 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.