Completed
Push — master ( 93e915...9d3945 )
by
unknown
02:18
created

HtmlReporter::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
nc 2
nop 4
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeReporter\Reporter;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use whm\Smoke\Config\Configuration;
7
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
8
use whm\Smoke\Rules\CheckResult;
9
10
11
class HtmlReporter implements Reporter
12
{
13
    private $results = [];
14
    private $successes = [];
15
    private $failures = [];
16
    private $unknowns = [];
17
18
19
    /**
20
     * @var Configuration
21
     */
22
    private $templateDir;
23
    private $templateFile;
24
    private $resultFile;
25
26
    /**
27
     * @var OutputInterface
28
     */
29
    private $output;
30
31
    public function init(OutputInterface $_output, $resultFile, $templateDir = null, $templateFile = 'index.html.twig')
32
    {
33
        $this->resultFile = $resultFile;
34
35
        if ($templateDir === null) {
36
            $this->templateDir = __DIR__ . '/templates';
0 ignored issues
show
Documentation Bug introduced by
It seems like __DIR__ . '/templates' of type string is incompatible with the declared type object<whm\Smoke\Config\Configuration> of property $templateDir.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        } else {
38
            $this->templateDir = $templateDir;
39
        }
40
41
        $this->templateFile = $templateFile;
42
        $this->output = $_output;
43
    }
44
45
    /**
46
     * @param CheckResult[] $results
47
     */
48
    public function processResults($results)
49
    {
50
        foreach ($results as $result) {
51
            $temp = [
52
                'url' => $result->getResponse()->getUri(),
53
                'rule' => $result->getRuleName(),
54
                'message' => $result->getMessage(),
55
                'status' => $result->getStatus(),
56
            ];
57
58
            switch ($result->getStatus()) {
59
                case CheckResult::STATUS_SUCCESS:
60
                    $this->successes[] = $temp;
61
                    break;
62
63
                case CheckResult::STATUS_FAILURE:
64
                    $this->failures[] = $temp;
65
                break;
66
                default:
67
                    $this->unknowns[] = $temp;
68
            }
69
70
            $this->results[] = $temp;
71
        }
72
    }
73
74
    public function finish()
75
    {
76
        $loader = new \Twig_Loader_Filesystem($this->templateDir);
0 ignored issues
show
Documentation introduced by
$this->templateDir is of type object<whm\Smoke\Config\Configuration>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        $twig = new \Twig_Environment($loader);
78
79
        $html = $twig->render($this->templateFile, [
80
            'results' => $this->results,
81
            'successes' => $this->successes,
82
            'failures' => $this->failures,
83
            'unknowns' => $this->unknowns,
84
            'success' => count($this->successes),
85
            'failure' => count($this->failures),
86
            'unknown' => count($this->unknowns),
87
            'total' => count($this->results),
88
        ]);
89
90
        if (!file_exists(dirname($this->resultFile))) {
91
            mkdir(dirname($this->resultFile));
92
        }
93
94
        if (!file_put_contents($this->resultFile, $html)) {
95
            $this->output->writeln("<error>HTML Reporter extension: Could not write result file to " . $this->resultFile ."</error>");
96
        } else {
97
            $this->output->writeln("<info>HTML Reporter extension:</info> Result file written to " . $this->resultFile);
98
        }
99
    }
100
101
}
102