Passed
Push — master ( d9f7c4...0d8198 )
by Jakub
02:57
created

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