Completed
Push — master ( 5eb6e2...5eaa75 )
by Jakub
05:29
created

CombatLogger::setRound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 1
cts 1
cp 1
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nette\Localization\ITranslator;
7
8
/**
9
 * Combat log
10
 * 
11
 * @author Jakub Konečný
12
 * @property int $round Current round
13
 * @property string $title
14
 */
15 1
final class CombatLogger implements \Countable, \IteratorAggregate {
16 1
  use \Nette\SmartObject;
17
18
  /** @var ICombatLogRender */
19
  protected $render;
20
  /** @var ITranslator */
21
  protected $translator;
22
  /** @var Team First team */
23
  protected $team1;
24
  /** @var Team Second team */
25
  protected $team2;
26
  /** @var array */
27
  protected $actions = [];
28
  /** @var int */
29
  protected $round;
30
  /** @var string */
31
  protected $title = "";
32
  
33
  public function __construct(ICombatLogRender $render, ITranslator $translator) {
34 1
    $this->render = $render;
35 1
    $this->translator = $translator;
36 1
  }
37
  
38
  /**
39
   * Set teams
40
   */
41
  public function setTeams(Team $team1, Team $team2): void {
42 1
    if(isset($this->team1)) {
43 1
      throw new ImmutableException("Teams has already been set.");
44
    }
45 1
    $this->team1 = $team1;
46 1
    $this->team2 = $team2;
47 1
  }
48
  
49
  public function getRound(): int {
50 1
    return $this->round;
51
  }
52
  
53
  public function setRound(int $round): void {
54 1
    $this->round = $round;
55 1
  }
56
  
57
  public function getTitle(): string {
58 1
    return $this->title;
59
  }
60
  
61
  public function setTitle(string $title): void {
62 1
    $this->title = $title;
63 1
  }
64
65
  /**
66
   * Adds new entry
67
   */
68
  public function log(array $action): void {
69 1
    $this->actions[$this->round][] = new CombatAction($this->translator, $action);
70 1
  }
71
  
72
  /**
73
   * Adds text entry
74
   */
75
  public function logText(string $text, array $params = []): void {
76 1
    $this->actions[$this->round][] = $this->translator->translate($text, 0, $params);
77 1
  }
78
  
79
  public function __toString(): string {
80
    $params = [
81 1
      "team1" => $this->team1, "team2" => $this->team2, "actions" => $this->actions, "title" => $this->title,
82
    ];
83 1
    return $this->render->render($params);
84
  }
85
  
86
  public function count(): int {
87 1
    return count($this->actions);
88
  }
89
  
90
  public function getIterator(): \ArrayIterator {
91 1
    return new \ArrayIterator($this->actions);
92
  }
93
}
94
?>