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

SingleResultFormatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 41
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A formatSingleResult() 0 10 2
A addToMap() 0 7 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