Completed
Pull Request — master (#34)
by Alessandro
03:03 queued 40s
created

SingleResultFormatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 44
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 11
    public function __construct(JSONLogParser $logParser)
22
    {
23 11
        $this->addToMap($logParser);
24
25 11
        foreach ($logParser->getParsersForPrinting() as $parser) {
26 10
            $this->addToMap($parser);
27 11
        }
28 11
    }
29
30
    /**
31
     * @param $singleResult
32
     * @return string
33
     */
34 11
    public function formatSingleResult($singleResult)
35
    {
36 11
        if (array_key_exists($singleResult, $this->tagMap)) {
37 10
            $tag = $this->tagMap[$singleResult];
38
39 10
            return sprintf('<%s>%s</%s>', $tag, $singleResult, $tag);
40
        }
41
42 1
        return $singleResult;
43
    }
44
45
    /**
46
     * @param $parser
47
     */
48 11
    private function addToMap($parser)
49
    {
50 11
        if ($parser instanceof OutputContainerBearerInterface) {
51 11
            $container = $parser->getOutputContainer();
52 11
            $this->tagMap[$container->getSingleResultMarker()] = $container->getTag();
53 11
        }
54 11
    }
55
}
56