TableRenderer::render()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 4
eloc 22
nc 3
nop 1
dl 0
loc 32
rs 8.5806
c 4
b 0
f 2
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Renderer;
17
18
use Hogosha\Monitor\Guesser\StatusGuesser;
19
use Hogosha\Monitor\Model\ResultCollection;
20
use Webmozart\Console\Api\IO\IO;
21
use Webmozart\Console\UI\Component\Table;
22
23
/**
24
 * @author Guillaume Cavana <[email protected]>
25
 */
26
class TableRenderer implements RendererInterface
27
{
28
    protected $io;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param IO $io
34
     */
35
    public function __construct(IO $io)
36
    {
37
        $this->io = $io;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function render(ResultCollection $resultCollection)
44
    {
45
        $table = new Table();
46
        $table
47
            ->setHeaderRow(['Global Status', 'Status Code', 'Name', 'Response Time', 'Http Error Log', 'Validator Error Log']);
48
49
        $statusGuesser = new StatusGuesser();
50
51
        foreach ($resultCollection as $result) {
52
            $color = $statusGuesser->isFailed($result) ? 'red' : 'green';
53
            $table->addRow([
54
                $statusGuesser->isFailed($result) ? 'FAIL' : 'OK',
55
                sprintf(
56
                    '<bg=%s>%s</>',
57
                    $color,
58
                    $result->getStatusCode()
59
                ),
60
                $result->getUrl()->getName(),
61
                $result->getReponseTime(),
62
                sprintf(
63
                    '<bg=red>%s</>',
64
                    $result->getHandlerError()
65
                ),
66
                sprintf(
67
                    '<bg=red>%s</>',
68
                    $result->getValidatorError()
69
                ),
70
            ]);
71
        }
72
73
        $table->render($this->io);
74
    }
75
}
76