Completed
Push — master ( 084e6b...eff98c )
by Guillaume
05:15
created

ListRenderer::render()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 22
rs 8.9197
cc 4
eloc 15
nc 2
nop 1
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
22
/**
23
 * @author Guillaume Cavana <[email protected]>
24
 */
25
class ListRenderer implements RendererInterface
26
{
27
    protected $io;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param IO $io
33
     */
34
    public function __construct(IO $io)
35
    {
36
        $this->io = $io;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function render(ResultCollection $resultCollection)
43
    {
44
        $format = '[%s][%s] %s - %s%s';
45
46
        $errorFormat = ' - <bg=red>%s</>';
47
48
        $statusGuesser = new StatusGuesser();
49
50
        foreach ($resultCollection as $result) {
51
            $errorLog = $result->getHandlerError();
52
            $this->io->write(
53
                sprintf(
54
                    $format,
55
                    $statusGuesser->isFailed($result) ? 'FAIL' : 'OK',
56
                    $result->getStatusCode(),
57
                    $result->getUrl()->getName(),
58
                    $result->getReponseTime(),
59
                    (null !== $errorLog) ? sprintf($errorFormat, $result->getHandlerError()) : ''
60
                )."\n"
61
            );
62
        }
63
    }
64
}
65