Test Failed
Pull Request — master (#154)
by Antonio Carlos
04:56 queued 02:28
created

Commands   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 144
ccs 0
cts 60
cp 0
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A info() 0 6 2
A warn() 0 6 2
A __construct() 0 4 1
A getTargetsFomResources() 0 12 3
A normalizeMessage() 0 12 1
A panel() 0 18 2
A check() 0 23 4
A table() 0 6 2
A error() 0 6 2
1
<?php
2
3
namespace PragmaRX\Health;
4
5
use Illuminate\Console\Command;
6
use PragmaRX\Health\Service as HealthService;
7
8
class Commands
9
{
10
    /**
11
     * @var Service
12
     */
13
    private $healthService;
14
15
    /**
16
     * Commands constructor.
17
     *
18
     * @param Service $healthService
19
     */
20
    public function __construct(HealthService $healthService)
21
    {
22
        $this->healthService = $healthService;
23
    }
24
25
    /**
26
     * @param $rows
27
     * @return \Illuminate\Support\Collection|\IlluminateAgnostic\Arr\Support\Collection|\IlluminateAgnostic\Collection\Support\Collection|\IlluminateAgnostic\Str\Support\Collection|\Tightenco\Collect\Support\Collection|\Vanilla\Support\Collection
28
     * @throws \Exception
29
     */
30
    protected function getTargetsFomResources($resources)
31
    {
32
        $targets = collect();
33
34
        foreach ($resources as $resource) {
35
            foreach ($resource->targets as $target) {
36
                $targets->push($target);
37
            }
38
        }
39
40
        return $targets;
41
    }
42
43
    private function normalizeMessage($message)
44
    {
45
        $message = str_replace("\n", '', $message);
46
        $message = str_replace("\r", '', $message);
47
        $message = str_replace("\t", ' ', $message);
48
        $message = str_replace('<br>', ' ', $message);
49
        $message = str_replace('  ', ' ', $message);
50
51
        $message = wordwrap($message, 60);
52
53
        return $message;
54
    }
55
56
    public function panel(Command $command = null)
57
    {
58
        $columns = ['Resource', 'State', 'Message'];
59
60
        $rows = $this->getTargetsFomResources($this->healthService->health())
61
            ->map(function ($target) {
62
                return [
63
                    "{$target->resource->name} ({$target->display})",
64
                    $target->result->healthy
65
                        ? '<info>healthy</info>'
66
                        : '<fg=red>failing</fg=red>',
67
                    $this->normalizeMessage($target->result->errorMessage),
68
                ];
69
            })
70
            ->toArray();
71
72
        $this->table($command, $columns, $rows);
73
    }
74
75
    public function check(Command $command = null)
76
    {
77
        $checker = $this->healthService->getSilentChecker();
78
79
        $errors = $this->getTargetsFomResources($checker()->filter(function ($resource) {
80
            return ! $resource->isGlobal;
81
        }))->reduce(function ($carry, $target) {
82
            return $carry + ($target->result->healthy ? 0 : 1);
83
        }, 0);
84
85
        if ($errors) {
86
            $this->error(
87
                $command,
88
                "Application needs attention, $errors ".
89
                    str_plural('resouce', $errors).
0 ignored issues
show
Deprecated Code introduced by
The function str_plural() has been deprecated with message: Str::plural() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
90
                    ' '.
91
                    ($errors > 1 ? 'are' : 'is').
92
                    ' currently failing.'
93
            );
94
        } else {
95
            $this->info($command, 'Check completed with no errors.');
96
        }
97
    }
98
99
    /**
100
     * Format input to textual table.
101
     *
102
     * @param Command|null $command
103
     * @param $columns
104
     * @param  \Illuminate\Contracts\Support\Arrayable|array $rows
105
     */
106
    private function table($command, $columns, $rows)
107
    {
108
        if ($command) {
109
            $command->table($columns, $rows);
110
        }
111
    }
112
113
    /**
114
     * Write a string as information output.
115
     *
116
     * @param Command|null $command
117
     * @param $string
118
     */
119
    private function info($command, $string)
120
    {
121
        if ($command) {
122
            $command->info($string);
123
        }
124
    }
125
126
    /**
127
     * Write a string as information output.
128
     *
129
     * @param Command|null $command
130
     * @param $string
131
     */
132
    private function error($command, $string)
133
    {
134
        if ($command) {
135
            $command->error($string);
136
        }
137
    }
138
139
    /**
140
     * Write a string as information output.
141
     *
142
     * @param Command|null $command
143
     * @param $string
144
     */
145
    private function warn($command, $string)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
146
    {
147
        if ($command) {
148
            $command->warn($string);
149
        }
150
    }
151
}
152