MapTransformer::getSymbolByBiom()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
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