Test Failed
Push — master ( bfe357...41d8e8 )
by Antonio Carlos
25:24
created

src/Checkers/Base.php (5 issues)

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\Checkers;
4
5
use SebastianBergmann\Timer\Timer;
6
use PragmaRX\Health\Support\Result;
7
use PragmaRX\Health\Support\Target;
8
use PragmaRX\Health\Support\Traits\Database;
0 ignored issues
show
This use statement conflicts with another class in this namespace, PragmaRX\Health\Checkers\Database.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
10
abstract class Base implements Contract
11
{
12
    use Database;
13
14
    /**
15
     * @var Target
16
     */
17
    protected $target;
18
19
    /**
20
     * Check target and store elapsed time.
21
     */
22
    protected function checkAndStoreTime()
23
    {
24
        Timer::start();
25
26
        $result = $this->check();
27
28
        $result->elapsedTime = Timer::stop();
29
30
        return $result;
31
    }
32
33
    /**
34
     * Create base directory for files.
35
     *
36
     * @param $fileName
37
     */
38
    protected function makeDir($fileName)
39
    {
40
        $dir = dirname($fileName);
41
42
        if (!file_exists($dir)) {
43
            mkdir($dir, 0775, true);
44
        }
45
    }
46
47
    /**
48
     * Make a result.
49
     *
50
     * @param bool $healthy
51
     * @param null $errorMessage
52
     * @return Result
53
     */
54
    public function makeResult($healthy = true, $errorMessage = null)
55
    {
56
        return new Result($healthy, $errorMessage);
57
    }
58
59
    /**
60
     * Make a healthy result.
61
     *
62
     * @return Result
63
     */
64
    protected function makeHealthyResult()
65
    {
66
        return $this->makeResult();
67
    }
68
69
    /**
70
     * Make a result from an exception.
71
     *
72
     * @param $exception
73
     * @return Result
74
     */
75
    protected function makeResultFromException($exception)
76
    {
77
        return $this->makeResult(false, $exception->getMessage());
78
    }
79
80
    /**
81
     * @param $resources
82
     * @return mixed
83
     */
84
    public function healthy($resources)
85
    {
86
        return $this->healthy;
0 ignored issues
show
The property healthy 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...
87
    }
88
89
    /**
90
     * @param $resources
91
     * @return mixed
92
     */
93
    public function message($resources)
94
    {
95
        return $this->message;
0 ignored issues
show
The property message 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...
96
    }
97
98
    /**
99
     * Save result to database.
100
     *
101
     * @param $result
102
     * @return
103
     */
104
    protected function saveToDatabase($result)
105
    {
106
        if ($this->databaseEnabled()) {
107
            return $this->saveResultsToDatabase($this->target, $result);
108
        }
109
    }
110
111
    /**
112
     * @param $healthy
113
     */
114
    public function setHealthy($healthy)
115
    {
116
        $this->healthy = $healthy;
117
    }
118
119
    /**
120
     * @param $message
121
     */
122
    public function setMessage($message)
123
    {
124
        $this->message = $message;
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    public function healthArray()
131
    {
132
        return [
133
            'healthy' => $this->healthy,
134
135
            'message' => $this->message,
136
        ];
137
    }
138
139
    /**
140
     * Load cache.
141
     *
142
     * @return \Illuminate\Support\Collection
143
     */
144 View Code Duplication
    public function load()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
145
    {
146
        if (!file_exists($file = $this->getFileName())) {
147
            return collect();
148
        }
149
150
        return collect(json_decode(file_get_contents($file), true));
151
    }
152
153
    /**
154
     * Persist to database cache file.
155
     *
156
     * @param $data
157
     */
158
    public function persist($data = null)
159
    {
160
        if (is_null($data)) {
161
            $data = $this->database->toArray();
162
        }
163
164
        if (!is_array($data)) {
165
            $data = $data->toArray();
166
        }
167
168
        $this->makeDir($this->getFileName());
169
170
        file_put_contents($this->getFileName(), json_encode($data));
171
    }
172
173
    /**
174
     * Get cache filename.
175
     *
176
     * @return string
177
     */
178
    protected function getFileName()
179
    {
180
        return $this->target->saveTo ?? '';
0 ignored issues
show
The property saveTo does not seem to exist in PragmaRX\Health\Support\Target.

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.

Loading history...
181
    }
182
183
    /**
184
     * Target setter.
185
     *
186
     * @param $target
187
     * @return $this
188
     */
189
    public function setTarget($target)
190
    {
191
        $this->target = $target;
192
193
        return $this;
194
    }
195
196
    public function checkTarget()
197
    {
198
        $result = $this->checkAndStoreTime();
199
200
        $result->checks = $this->saveToDatabase($result);
201
202
        return $result;
203
    }
204
}
205