Completed
Push — main ( dc0cbe...02ba7b )
by mohsen
14s queued 13s
created

FileDriver::record()   C

Complexity

Conditions 15
Paths 192

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 20
c 0
b 0
f 0
nc 192
nop 5
dl 0
loc 35
rs 5.15

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MohsenAbrishami\Stethoscope\LogRecord\Drivers;
4
5
use Illuminate\Support\Facades\Storage;
6
use MohsenAbrishami\Stethoscope\LogRecord\Contracts\LogRecordInterface;
7
use MohsenAbrishami\Stethoscope\Traits\MessageCreatorTrait;
8
9
class FileDriver implements LogRecordInterface
10
{
11
    use MessageCreatorTrait;
12
13
    public function record($cpuUsage, $memoryUsage, $networkStatus, $webServerStatuses, $hardDiskFreeSpace)
14
    {
15
        $file = config('stethoscope.log_file_storage.path') . now()->format('Y-m-d');
16
17
        $this->storage = Storage::disk(config('stethoscope.log_file_storage.driver'));
0 ignored issues
show
Bug Best Practice introduced by
The property storage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
19
        $log = '';
20
21
        if ($cpuUsage > config(('stethoscope.thresholds.cpu')) && config('stethoscope.monitorable_resources.cpu'))
22
            $log .= $this->cpuMessage($cpuUsage) . "\n";
23
24
        if ($memoryUsage > config(('stethoscope.thresholds.memory')) && config('stethoscope.monitorable_resources.memory'))
25
            $log .= $this->memoryMessage($memoryUsage) . "\n";
26
27
        if (!$networkStatus && config('stethoscope.monitorable_resources.network'))
28
            $log .= $this->networkMessage($networkStatus) . "\n";
29
30
        if (($webServerStatuses['nginx'] != 'active' && config('stethoscope.monitorable_resources.web_server'))) {
31
            $log .= $this->webServerMessage('nginx', $webServerStatuses['nginx']) . "\n";
32
        }
33
34
        if (($webServerStatuses['apache'] != 'active' && config('stethoscope.monitorable_resources.web_server'))) {
35
            $log .= $this->webServerMessage('apache', $webServerStatuses['apache']) . "\n";
36
        }
37
38
        if ($hardDiskFreeSpace < config(('stethoscope.thresholds.hard_disk')) && config('stethoscope.monitorable_resources.hard_disk'))
39
            $log .= $this->hardDiskMessage($hardDiskFreeSpace) . "\n";
40
41
        if ($log != '') {
42
            $log = $this->timeMessage() . "\n" . $log;
43
44
            if ($this->storage->exists($file))
45
                $log = $this->storage->get($file) . "\n \n" . $log;
46
47
            $this->storage->put($file, $log);
48
        }
49
    }
50
}
51