MapTransformer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 49
ccs 23
cts 23
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSymbolByBiom() 0 11 1
A createStringSequence() 0 10 2
B transformToString() 0 22 4
1
<?php
2
3
namespace Vehsamrak\Terraformator\Service;
4
5
use Vehsamrak\Terraformator\Entity\Location;
6
use Vehsamrak\Terraformator\Entity\Map;
7
use Vehsamrak\Terraformator\Enum\Biom;
8
9
/**
10
 * @author Vehsamrak
11
 */
12
class MapTransformer
13
{
14
15 6
    public function transformToString(Map $map, int $width = 0): string
16
    {
17 6
        $stringMap = '';
18
19
        /** @var Location $location */
20 6
        $mapLocations = $map->toArray();
21
22 6
        if (!count($mapLocations)) {
23 1
            return $stringMap;
24
        }
25
26 5
        if ($width > 0) {
27 3
            foreach (array_chunk($mapLocations, $width) as $locations) {
28 3
                $stringMap .= $this->createStringSequence($locations);
29 3
                $stringMap .= PHP_EOL;
30
            }
31
        } else {
32 2
            $stringMap = $this->createStringSequence($mapLocations);
33
        }
34
35 5
        return $stringMap;
36
    }
37
38 5
    private function getSymbolByBiom(Biom $biom): string
39
    {
40
        $biomSymbolMatrix = [
41 5
            Biom::FOREST => '^',
42 5
            Biom::FIELD  => '_',
43 5
            Biom::DEEP_FOREST => '#',
44 5
            Biom::ROAD => '.',
45
        ];
46
47 5
        return $biomSymbolMatrix[$biom->getValue()];
48
    }
49
50 5
    private function createStringSequence(array $locations): string
51
    {
52 5
        $stringSequence = '';
53 5
        foreach ($locations as $location) {
54 5
            $biom = $location->getBiom();
55 5
            $stringSequence .= $this->getSymbolByBiom($biom);
56
        }
57
58 5
        return $stringSequence;
59
    }
60
}
61