Passed
Branch benchmark (b81757)
by Jose
02:27
created

ResultPrinter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JMGQ\AStar\Benchmark\Result;
4
5
use Symfony\Component\Console\Style\StyleInterface;
6
7
class ResultPrinter
8
{
9
    private $output;
10
11 7
    public function __construct(StyleInterface $output)
12
    {
13 7
        $this->output = $output;
14 7
    }
15
16
    /**
17
     * @param AggregatedResult[] $results
18
     */
19 7
    public function display(array $results)
20
    {
21 7
        $tableRows = array();
22
23 7
        $orderedResults = $this->orderResults($results);
24
25 7
        foreach ($orderedResults as $result) {
26 6
            $size = $result->getSize() . 'x' . $result->getSize();
27 6
            $averageDuration = $result->getAverageDuration() . 'ms';
28 6
            $minimumDuration = $result->getMinimumDuration() . 'ms';
29 6
            $maximumDuration = $result->getMaximumDuration() . 'ms';
30 6
            $solutionFound = $this->formatSolutionFound($result);
31
32 6
            $tableRows[] = array($size, $averageDuration, $minimumDuration, $maximumDuration, $solutionFound);
33
        }
34
35 7
        $tableHeaders = array('Size', 'Avg Duration', 'Min Duration', 'Max Duration', 'Solved?');
36
37 7
        $this->output->table($tableHeaders, $tableRows);
38 7
    }
39
40
    /**
41
     * @param AggregatedResult[] $results
42
     * @return AggregatedResult[]
43
     */
44
    private function orderResults(array $results)
45
    {
46 7
        usort($results, function ($a, $b) {
47 1
            return $a->getSize() > $b->getSize();
48 7
        });
49
50 7
        return $results;
51
    }
52
53
    /**
54
     * @param AggregatedResult $result
55
     * @return string
56
     */
57 6
    private function formatSolutionFound($result)
58
    {
59 6
        $allResultsAreSolved = $result->getNumberOfSolutions() === $result->getNumberOfTerrains();
60 6
        if ($allResultsAreSolved) {
61 4
            return 'Yes';
62
        }
63
64 2
        $allResultsAreUnsolved = $result->getNumberOfSolutions() === 0;
65 2
        if ($allResultsAreUnsolved) {
66 1
            return 'No';
67
        }
68
69 1
        return 'Sometimes';
70
    }
71
}
72