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

Commands   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 189
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A normalizeMessage() 0 12 1
A panel() 0 18 2
A check() 0 29 4
A export() 0 29 2
A publish() 0 26 3
A table() 0 6 2
A info() 0 6 2
A error() 0 6 2
A warn() 0 6 2
1
<?php
2
3
namespace PragmaRX\Health;
4
5
use Illuminate\Console\Command;
6
use PragmaRX\Yaml\Package\Yaml;
7
use PragmaRX\Health\Service as HealthService;
8
9
class Commands
10
{
11
    /**
12
     * @var Service
13
     */
14
    private $healthService;
15
16
    /**
17
     * Commands constructor.
18
     *
19
     * @param Service $healthService
20
     */
21
    public function __construct(HealthService $healthService)
22
    {
23
        $this->healthService = $healthService;
24
    }
25
26
    private function normalizeMessage($message)
27
    {
28
        $message = str_replace("\n", '', $message);
29
        $message = str_replace("\r", '', $message);
30
        $message = str_replace("\t", ' ', $message);
31
        $message = str_replace('<br>', ' ', $message);
32
        $message = str_replace('  ', ' ', $message);
33
34
        $message = wordwrap($message, 60);
35
36
        return $message;
37
    }
38
39
    public function panel(Command $command = null)
40
    {
41
        $columns = ['Resource', 'State', 'Message'];
42
43
        $rows = $this->healthService->health()
44
            ->map(function ($resource) {
45
                return [
46
                    $resource['name'],
47
                    $resource['health']['healthy']
48
                        ? '<info>healthy</info>'
49
                        : '<fg=red>failing</fg=red>',
50
                    $this->normalizeMessage($resource['health']['message']),
51
                ];
52
            })
53
            ->toArray();
54
55
        $this->table($command, $columns, $rows);
56
    }
57
58
    public function check(Command $command = null)
59
    {
60
        $checker = $this->healthService->getSilentChecker();
61
62
        $errors = $checker()
63
            ->where('is_global', false)
64
            ->reduce(function ($carry, $item) {
65
                return $carry + ($item['health']['healthy'] ? 0 : 1);
66
            }, 0);
67
68
        $exitCode = 0;
69
70
        if ($errors) {
71
            $this->error(
72
                $command,
73
                "Application needs attention, $errors " .
74
                    str_plural('resouce', $errors) .
75
                    ' ' .
76
                    ($errors > 1 ? 'are' : 'is') .
77
                    ' currently failing.'
78
            );
79
80
            $exitCode = 255;
81
        } else {
82
            $this->info($command, 'Check completed with no errors.');
83
        }
84
85
        exit($exitCode);
86
    }
87
88
    public function export(Command $command = null)
89
    {
90
        $yaml = new Yaml();
91
92
        collect(config('health.resources'))->each(function (
93
            $resource,
94
            $key
95
        ) use ($command, $yaml) {
96
            $path = config('health.resources.path');
97
98
            $resource['column_size'] = (int) $resource['columnSize'];
99
100
            unset($resource['columnSize']);
101
102
            if (!file_exists($path)) {
103
                mkdir($path, 0660, true);
104
            }
105
106
            $dump = $yaml->dump($resource, 5);
107
108
            file_put_contents(
109
                $file =
110
                    $path . DIRECTORY_SEPARATOR . studly_case($key) . '.yml',
111
                $dump
112
            );
113
114
            $this->info($command, 'Exported ' . $file);
115
        });
116
    }
117
118
    public function publish(Command $command = null)
119
    {
120
        $yaml = new Yaml();
121
122
        $yaml
123
            ->loadFromDirectory(package_resources_dir(), false)
0 ignored issues
show
Unused Code introduced by Antonio Carlos Ribeiro
The call to Yaml::loadFromDirectory() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
124
            ->each(function ($value, $key) use ($command, $yaml) {
125
                if (!file_exists($path = config('health.resources.path'))) {
126
                    mkdir($path, 0755, true);
127
                }
128
129
                if (
130
                    file_exists(
131
                        $file = $path . DIRECTORY_SEPARATOR . $key . '.yaml'
132
                    )
133
                ) {
134
                    $this->warn($command, 'Skipped: ' . $file);
135
136
                    return;
137
                }
138
139
                file_put_contents($file, $yaml->dump($value));
140
141
                $this->info($command, 'Saved: ' . $file);
142
            });
143
    }
144
145
    /**
146
     * Format input to textual table.
147
     *
148
     * @param Command|null $command
149
     * @param $columns
150
     * @param  \Illuminate\Contracts\Support\Arrayable|array $rows
151
     */
152
    private function table($command, $columns, $rows)
153
    {
154
        if ($command) {
155
            $command->table($columns, $rows);
156
        }
157
    }
158
159
    /**
160
     * Write a string as information output.
161
     *
162
     * @param Command|null $command
163
     * @param $string
164
     */
165
    private function info($command, $string)
166
    {
167
        if ($command) {
168
            $command->info($string);
169
        }
170
    }
171
172
    /**
173
     * Write a string as information output.
174
     *
175
     * @param Command|null $command
176
     * @param $string
177
     */
178
    private function error($command, $string)
179
    {
180
        if ($command) {
181
            $command->error($string);
182
        }
183
    }
184
185
    /**
186
     * Write a string as information output.
187
     *
188
     * @param Command|null $command
189
     * @param $string
190
     */
191
    private function warn($command, $string)
192
    {
193
        if ($command) {
194
            $command->warn($string);
195
        }
196
    }
197
}
198