Passed
Push — master ( e32a81...12baf4 )
by Antonio Carlos
02:26
created

src/Support/Traits/Database.php (1 issue)

Upgrade to new PHP Analysis Engine

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
    public function __load()
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;
0 ignored issues
show
The property target does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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
        $model = config('health.database.model', HealthCheck::class);
53
54
        $model::create([
55
            'resource_name' => $resource = $target->resource->name,
56
            'resource_slug' => $target->resource->slug,
57
            'target_name' => $target->name,
58
            'target_slug' => str_slug($target->name),
59
            'target_display' => $target->display,
60
            'healthy' => $result->healthy,
61
            'error_message' => $result->errorMessage,
62
            'runtime' => $result->elapsedTime,
63
            'value' => $result->value,
64
            'value_human' => $result->valueHuman,
65
        ]);
66
67
        return $model::where([
68
            'resource_slug' => $target->resource->slug,
69
            'target_name' => $target->name,
70
        ])
71
            ->orderBy('created_at', 'desc')
72
            ->take(config('health.database.max_records'))
73
            ->get();
74
    }
75
}
76