SequencePrinter   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
eloc 30
c 0
b 0
f 0
dl 0
loc 90
ccs 39
cts 39
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setTileSize() 0 7 2
A setEmptyTileToken() 0 3 1
A __construct() 0 4 1
A getPadToken() 0 3 1
A getTile() 0 3 1
A generateEmptyBoard() 0 9 1
A getEmptyTileToken() 0 3 1
A setPadToken() 0 3 1
A getTileSize() 0 3 1
A printSequence() 0 18 3
1
<?php
2
3
namespace JMGQ\AStar\Example\Terrain;
4
5
class SequencePrinter
6
{
7
    private TerrainCost $terrainCost;
8
    /** @var iterable<Position> */
9
    private iterable $sequence;
10
    private string $emptyTileToken = '-';
11
    private int $tileSize = 3;
12
    private string $padToken = ' ';
13
14
    /**
15
     * @param TerrainCost $terrainCost
16
     * @param iterable<Position> $sequence
17
     */
18 29
    public function __construct(TerrainCost $terrainCost, iterable $sequence)
19
    {
20 29
        $this->terrainCost = $terrainCost;
21 29
        $this->sequence = $sequence;
22 29
    }
23
24 13
    public function getEmptyTileToken(): string
25
    {
26 13
        return $this->emptyTileToken;
27
    }
28
29 6
    public function setEmptyTileToken(string $emptyTileToken): void
30
    {
31 6
        $this->emptyTileToken = $emptyTileToken;
32 6
    }
33
34 12
    public function getTileSize(): int
35
    {
36 12
        return $this->tileSize;
37
    }
38
39 7
    public function setTileSize(int $tileSize): void
40
    {
41 7
        if ($tileSize < 1) {
42 2
            throw new \InvalidArgumentException("Invalid tile size: $tileSize");
43
        }
44
45 5
        $this->tileSize = $tileSize;
46 5
    }
47
48 13
    public function getPadToken(): string
49
    {
50 13
        return $this->padToken;
51
    }
52
53 6
    public function setPadToken(string $padToken): void
54
    {
55 6
        $this->padToken = $padToken;
56 6
    }
57
58 7
    public function printSequence(): void
59
    {
60 7
        $board = $this->generateEmptyBoard();
61
62 7
        $step = 1;
63 7
        foreach ($this->sequence as $position) {
64 7
            $board[$position->getRow()][$position->getColumn()] = $this->getTile((string) $step);
65
66 7
            $step++;
67
        }
68
69 7
        $stringBoard = [];
70
71 7
        foreach ($board as $row) {
72 7
            $stringBoard[] = implode('', $row);
73
        }
74
75 7
        echo implode("\n", $stringBoard);
76 7
    }
77
78
    /**
79
     * @return string[][]
80
     */
81 7
    private function generateEmptyBoard(): array
82
    {
83 7
        $emptyTile = $this->getTile($this->getEmptyTileToken());
84
85 7
        $emptyRow = array_fill(0, $this->terrainCost->getTotalColumns(), $emptyTile);
86
87 7
        $board = array_fill(0, $this->terrainCost->getTotalRows(), $emptyRow);
88
89 7
        return $board;
90
    }
91
92 7
    private function getTile(string $value): string
93
    {
94 7
        return str_pad($value, $this->getTileSize(), $this->getPadToken(), STR_PAD_LEFT);
95
    }
96
}
97