Completed
Push — output_parsers_refactor ( d2cb12...a7c18a )
by Alessandro
03:46
created

SingleResultFormatter::addToMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Paraunit\Printer;
4
5
use Paraunit\Parser\JSONLogParser;
6
use Paraunit\Parser\OutputContainerBearerInterface;
7
8
/**
9
 * Class SingleResultFormatter
10
 * @package Paraunit\Printer
11
 */
12
class SingleResultFormatter
13
{
14
    /** @var  array */
15
    private $tagMap;
16
17
    /**
18
     * SingleResultFormatter constructor.
19
     * @param JSONLogParser $logParser
20
     */
21 10
    public function __construct(JSONLogParser $logParser)
22
    {
23 10
        $this->addToMap($logParser);
24
25 10
        foreach ($logParser->getParsersForPrinting() as $parser) {
26 9
            $this->addToMap($parser);
27 10
        }
28 10
    }
29
30
    /**
31
     * @param $singleResult
32
     * @return string
33
     */
34 10
    public function formatSingleResult($singleResult)
35
    {
36 10
        if (array_key_exists($singleResult, $this->tagMap)) {
37 9
            $tag = $this->tagMap[$singleResult];
38
39 9
            return sprintf('<%s>%s</%s>', $tag, $singleResult, $tag);
40
        }
41
42 1
        return $singleResult;
43
    }
44
45 10
    private function addToMap($parser)
46
    {
47 10
        if ($parser instanceof OutputContainerBearerInterface) {
48 10
            $container = $parser->getOutputContainer();
49 10
            $this->tagMap[$container->getSingleResultMarker()] = $container->getTag();
50 10
        }
51 10
    }
52
}
53