Console::onAfterRun()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 31
ccs 0
cts 20
cp 0
rs 9.2888
c 0
b 0
f 0
cc 5
nc 12
nop 3
crap 30
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
    const STATUS_TAG_SUCCESS = '<fg=green;options=bold>';
27
    const STATUS_TAG_WARNING = '<fg=yellow;options=bold>';
28
    const STATUS_TAG_SKIP = '<fg=blue;options=bold>';
29
    const STATUS_TAG_FAILURE = '<fg=red;options=bold>';
30
    const STATUS_TAG_UNKNOWN = '<fg=white;options=bold>';
31
32
    const IMPORTANCE_TAG_EMERGENCY = '<fg=red;options=bold>';
33
    const IMPORTANCE_TAG_WARNING = '<fg=yellow;options=bold>';
34
    const IMPORTANCE_TAG_NOTE = '<fg=green;options=bold>';
35
    const IMPORTANCE_TAG_INFO = '<fg=blue;options=bold>';
36
    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 99
    public function __construct(OutputInterface $output = null, $withData = false)
52
    {
53 99
        if (null === $output) {
54 99
            $output = new ConsoleOutput();
55
        }
56 99
        $this->output = $output;
57
58 99
        $this->withData = $withData;
59 99
    }
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%-60s</> %-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
100
    /**
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...
101
     * {@inheritdoc}
102
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
103
    public function onStart(\ArrayObject $checks, $runnerConfig)
104
    {
105
        parent::onStart($checks, $runnerConfig);
106
107
        $this->output->writeln(sprintf('<fg=white;options=bold>%-60s %-40s %-10s %s, %s</>', 'Check', 'Group / Tag(s)', 'Status', 'Info', 'Message'));
108
    }
109
110
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $results should have a doc-comment as per coding-style.
Loading history...
111
     * {@inheritdoc}
112
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
113
    public function onFinish(ResultsCollection $results)
114
    {
115
        parent::onFinish($results);
116
117
        $this->output->writeln('---------------------------------');
118
        $this->output->writeln(sprintf('%s: %s', 'total', $this->getTotalCount()));
119
        $this->output->writeln('');
120
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SUCCESS, 'SUCCESSES', $this->getSuccessCount()));
121
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_WARNING, 'WARNINGS', $this->getWarningCount()));
122
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SKIP, 'SKIP', $this->getSkipCount()));
123
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_FAILURE, 'FAILURES', $this->getFailureCount()));
124
        $this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_UNKNOWN, 'UNKNOWNS', $this->getUnknownCount()));
125
    }
126
127
    public static function tagByCode($code): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tagByCode()
Loading history...
128
    {
129
        $tags = [
130
            static::STATUS_CODE_SUCCESS => static::STATUS_TAG_SUCCESS,
131
            static::STATUS_CODE_WARNING => static::STATUS_TAG_WARNING,
132
            static::STATUS_CODE_SKIP => static::STATUS_TAG_SKIP,
133
            static::STATUS_CODE_FAILURE => static::STATUS_TAG_FAILURE,
134
            'default' => static::STATUS_TAG_UNKNOWN,
135
        ];
136
137
        return $tags[$code] ?? $tags['default'];
138
    }
139
140
    public static function tagByImportance($importance): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tagByImportance()
Loading history...
141
    {
142
        $tags = [
143
            CheckAbstract::IMPORTANCE_EMERGENCY => static::IMPORTANCE_TAG_EMERGENCY,
144
            CheckAbstract::IMPORTANCE_WARNING => static::IMPORTANCE_TAG_WARNING,
145
            CheckAbstract::IMPORTANCE_NOTE => static::IMPORTANCE_TAG_NOTE,
146
            CheckAbstract::IMPORTANCE_INFO => static::IMPORTANCE_TAG_INFO,
147
            'default' => static::IMPORTANCE_TAG_DEFAULT,
148
        ];
149
150
        return $tags[$importance] ?? $tags['default'];
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