ListRenderer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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%s';
45
46
        $errorFormat = ' - <bg=red>%s</>';
47
48
        $statusGuesser = new StatusGuesser();
49
50
        foreach ($resultCollection as $result) {
51
            $errorLog = $result->getHandlerError();
52
            $validatorErrorLog = $result->getValidatorError();
53
54
            $this->io->write(
55
                sprintf(
56
                    $format,
57
                    $statusGuesser->isFailed($result) ? 'FAIL' : 'OK',
58
                    $result->getStatusCode(),
59
                    $result->getUrl()->getName(),
60
                    $result->getReponseTime(),
61
                    (null !== $errorLog) ? sprintf($errorFormat, $errorLog) : '',
62
                    (null !== $validatorErrorLog) ? sprintf($errorFormat, $validatorErrorLog) : ''
63
                )."\n"
64
            );
65
        }
66
    }
67
}
68