Completed
Push — master ( 13c004...92d87a )
by Vladimir
06:13
created

Console   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 9.09%

Importance

Changes 0
Metric Value
wmc 15
eloc 71
dl 0
loc 148
ccs 6
cts 66
cp 0.0909
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataOut() 0 16 4
A tagByCode() 0 11 1
A __construct() 0 8 2
A onAfterRun() 0 31 5
A onStart() 0 5 1
A tagByImportance() 0 11 1
A onFinish() 0 12 1
1
<?php
2
3
/**
4
 * This file is part of the `tvi/monitor-bundle` project.
5
 *
6
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
11
12
namespace Tvi\MonitorBundle\Reporter;
13
14
use Symfony\Component\Console\Output\ConsoleOutput;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Tvi\MonitorBundle\Check\CheckAbstract;
17
use ZendDiagnostics\Check\CheckInterface;
18
use ZendDiagnostics\Result\Collection as ResultsCollection;
19
use ZendDiagnostics\Result\ResultInterface;
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @author Kevin Bond <[email protected]>, Vladimir Turnaev <[email protected]>
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
23
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
24
class Console extends ReporterAbstract
25
{
26
    private const STATUS_TAG_SUCCESS = '<fg=green;options=bold>';
27
    private const STATUS_TAG_WARNING = '<fg=yellow;options=bold>';
28
    private const STATUS_TAG_SKIP = '<fg=blue;options=bold>';
29
    private const STATUS_TAG_FAILURE = '<fg=red;options=bold>';
30
    private const STATUS_TAG_UNKNOWN = '<fg=white;options=bold>';
31
32
    private const IMPORTANCE_TAG_EMERGENCY = '<fg=red;options=bold>';
33
    private const IMPORTANCE_TAG_WARNING = '<fg=yellow;options=bold>';
34
    private const IMPORTANCE_TAG_NOTE = '<fg=green;options=bold>';
35
    private const IMPORTANCE_TAG_INFO = '<fg=blue;options=bold>';
36
    private const IMPORTANCE_TAG_DEFAULT = '<fg=default>';
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
39
     * @var OutputInterface
40
     */
41
    protected $output;
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
     * @var bool
45
     */
46
    protected $withData;
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $withData should have a doc-comment as per coding-style.
Loading history...
49
     * @param OutputInterface $output
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     */
51 100
    public function __construct(OutputInterface $output = null, $withData = false)
52
    {
53 100
        if (null === $output) {
54 100
            $output = new ConsoleOutput();
55
        }
56 100
        $this->output = $output;
57
58 100
        $this->withData = $withData;
59 100
    }
60
61
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $check should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $result should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $checkAlias should have a doc-comment as per coding-style.
Loading history...
62
     * {@inheritdoc}
63
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
64
    public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null)
65
    {
66
        list($status, $code) = $this->getStatusByResul($result);
67
68
        $statusTag = static::tagByCode($code);
69
70
        $groupTag = $check->getGroup();
0 ignored issues
show
Bug introduced by
The method getGroup() does not exist on ZendDiagnostics\Check\CheckInterface. It seems like you code against a sub-type of ZendDiagnostics\Check\CheckInterface such as Tvi\MonitorBundle\Check\CheckAbstract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        /** @scrutinizer ignore-call */ 
71
        $groupTag = $check->getGroup();
Loading history...
71
        $tags = $check->getTags();
0 ignored issues
show
Bug introduced by
The method getTags() does not exist on ZendDiagnostics\Check\CheckInterface. It seems like you code against a sub-type of ZendDiagnostics\Check\CheckInterface such as Tvi\MonitorBundle\Check\CheckAbstract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        /** @scrutinizer ignore-call */ 
72
        $tags = $check->getTags();
Loading history...
72
73
        if ($tags) {
74
            $tags = implode(', ', $tags);
75
            $tags = "[$tags]";
76
            $groupTag .= ' / '.$tags;
77
        } else {
78
            $tags = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $tags is dead and can be removed.
Loading history...
79
        }
80
81
        $message = $result->getMessage();
82
        if ($message) {
83
            $message = ", {$message}";
84
        }
85
86
        $labelTag = static::tagByImportance($check->getImportance());
0 ignored issues
show
Bug introduced by
The method getImportance() does not exist on ZendDiagnostics\Check\CheckInterface. It seems like you code against a sub-type of ZendDiagnostics\Check\CheckInterface such as Tvi\MonitorBundle\Check\CheckAbstract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        $labelTag = static::tagByImportance($check->/** @scrutinizer ignore-call */ getImportance());
Loading history...
87
88
        $this->output->writeln(sprintf('%s%-30s</> %-40s %s%-10s</> %s%s',
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
89
            $labelTag, $check->getId(), $groupTag, $statusTag, $status, $check->getLabel(), $message));
0 ignored issues
show
Bug introduced by
The method getId() does not exist on ZendDiagnostics\Check\CheckInterface. It seems like you code against a sub-type of ZendDiagnostics\Check\CheckInterface such as Tvi\MonitorBundle\Check\CheckAbstract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
            $labelTag, $check->/** @scrutinizer ignore-call */ getId(), $groupTag, $statusTag, $status, $check->getLabel(), $message));
Loading history...
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
90
91
        if ($this->withData) {
92
            $dataOut = $this->getDataOut($result);
93
            if ($dataOut) {
94
                $this->output->writeln(sprintf('%-71s < %s >', ' ', $dataOut));
95
            }
96
        }
97
    }
98
99
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $checks should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $runnerConfig should have a doc-comment as per coding-style.
Loading history...
100
     * {@inheritdoc}
101
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
102
    public function onStart(\ArrayObject $checks, $runnerConfig)
103
    {
104
        parent::onStart($checks, $runnerConfig);
105
106
        $this->output->writeln(sprintf('<fg=white;options=bold>%-30s %-40s %-10s %s, %s</>', 'Check', 'Group / Tag(s)', 'Status', 'Info', 'Message'));
107
    }
108
109
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $results should have a doc-comment as per coding-style.
Loading history...
110
     * {@inheritdoc}
111
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
112
    public function onFinish(ResultsCollection $results)
113
    {
114
        parent::onFinish($results);
115
116
        $this->output->writeln('---------------------------------');
117
        $this->output->writeln(sprintf('%s: %s', 'total', $this->getTotalCount()));
118
        $this->output->writeln('');
119
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SUCCESS, 'SUCCESSES', $this->getSuccessCount()));
120
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_WARNING, 'WARNINGS', $this->getWarningCount()));
121
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SKIP, 'SKIP', $this->getSkipCount()));
122
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_FAILURE, 'FAILURES', $this->getFailureCount()));
123
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_UNKNOWN, 'UNKNOWNS', $this->getUnknownCount()));
124
    }
125
126
    public static function tagByCode($code): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tagByCode()
Loading history...
127
    {
128
        $tags = [
129
            static::STATUS_CODE_SUCCESS => static::STATUS_TAG_SUCCESS,
130
            static::STATUS_CODE_WARNING => static::STATUS_TAG_WARNING,
131
            static::STATUS_CODE_SKIP => static::STATUS_TAG_SKIP,
132
            static::STATUS_CODE_FAILURE => static::STATUS_TAG_FAILURE,
133
            'default' => static::STATUS_TAG_UNKNOWN,
134
        ];
135
136
        return $tags[$code] ?? $tags['default'];
137
    }
138
139
    public static function tagByImportance($importance): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tagByImportance()
Loading history...
140
    {
141
        $tags = [
142
            CheckAbstract::IMPORTANCE_EMERGENCY => static::IMPORTANCE_TAG_EMERGENCY,
143
            CheckAbstract::IMPORTANCE_WARNING => static::IMPORTANCE_TAG_WARNING,
144
            CheckAbstract::IMPORTANCE_NOTE => static::IMPORTANCE_TAG_NOTE,
145
            CheckAbstract::IMPORTANCE_INFO => static::IMPORTANCE_TAG_INFO,
146
            'default' => static::IMPORTANCE_TAG_DEFAULT,
147
        ];
148
149
        return $tags[$importance] ?? $tags['default'];
150
    }
151
152
153
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $result should have a doc-comment as per coding-style.
Loading history...
154
     * @return null|string
155
     */
156
    protected function getDataOut(ResultInterface $result)
157
    {
158
        $dataOut = null;
159
        $message = $result->getMessage();
160
        if ($message) {
161
            $data = $result->getData();
162
            if (null !== $data) {
163
                $dataOut = json_encode($data);
164
165
                if (\mb_strlen($dataOut) > 100) {
166
                    $dataOut .= "\n";
167
                }
168
            }
169
        }
170
171
        return $dataOut;
172
    }
173
}
174