CombatLogger::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nette\Localization\Translator;
7
8
/**
9
 * Combat log
10
 *
11
 * @author Jakub Konečný
12
 */
13 1
final class CombatLogger implements \Countable, \IteratorAggregate, \Stringable
14
{
15
    private Team $team1;
16
    private Team $team2;
17
    private array $actions = [];
18
    public int $round = 0;
19
    public string $title = "";
20
21 1
    public function __construct(private readonly ICombatLogRender $render, private readonly Translator $translator)
22
    {
23 1
    }
24
25
    /**
26
     * Set teams
27
     */
28
    public function setTeams(Team $team1, Team $team2): void
29
    {
30 1
        if (isset($this->team1)) {
31 1
            throw new ImmutableException("Teams has already been set.");
32
        }
33 1
        $this->team1 = $team1;
34 1
        $this->team2 = $team2;
35 1
    }
36
37
    /**
38
     * Adds new entry
39
     */
40
    public function log(array $action): void
41
    {
42 1
        $this->actions[$this->round][] = new CombatLogEntry($action);
43 1
    }
44
45
    /**
46
     * Adds text entry
47
     */
48
    public function logText(string $text, array $params = []): void
49
    {
50 1
        $this->actions[$this->round][] = $this->translator->translate($text, 0, $params);
51 1
    }
52
53
    public function __toString(): string
54
    {
55 1
        $params = [
56 1
            "team1" => $this->team1, "team2" => $this->team2, "actions" => $this->actions, "title" => $this->title,
57
        ];
58 1
        return $this->render->render($params);
59
    }
60
61
    public function count(): int
62
    {
63 1
        return count($this->actions);
64
    }
65
66
    public function getIterator(): \ArrayIterator
67
    {
68 1
        return new \ArrayIterator($this->actions);
69
    }
70
}
71