Passed
Push — master ( 1a50e1...432b63 )
by Jakub
03:33
created

CombatLogger::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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
 * @property string $template
16
 */
17 1
final class CombatLogger implements \Countable, \IteratorAggregate {
18 1
  use \Nette\SmartObject;
19
  
20
  /** @var \Latte\Engine */
21
  protected $latte;
22
  /** @var ITranslator */
23
  protected $translator;
24
  /** @var Team First team */
25
  protected $team1;
26
  /** @var Team Second team */
27
  protected $team2;
28
  /** @var array */
29
  protected $actions = [];
30
  /** @var int */
31
  protected $round;
32
  /** @var string */
33
  protected $title = "";
34
  /** @var string */
35
  protected $template = __DIR__ . "/CombatLog.latte";
36
  
37
  public function __construct(ILatteFactory $latteFactory, ITranslator $translator) {
38 1
    $this->latte = $latteFactory->create();
39 1
    $this->translator = $translator;
40 1
  }
41
  
42
  /**
43
   * Set teams
44
   */
45
  public function setTeams(Team $team1, Team $team2): void {
46 1
    if(isset($this->team1)) {
47 1
      throw new ImmutableException("Teams has already been set.");
48
    }
49 1
    $this->team1 = $team1;
50 1
    $this->team2 = $team2;
51 1
  }
52
  
53
  public function getRound(): int {
54 1
    return $this->round;
55
  }
56
  
57
  public function setRound(int $round) {
58 1
    $this->round = $round;
59 1
  }
60
  
61
  public function getTitle(): string {
62 1
    return $this->title;
63
  }
64
  
65
  public function setTitle(string $title): void {
66 1
    $this->title = $title;
67 1
  }
68
  
69
  public function getTemplate(): string {
70
    return $this->template;
71
  }
72
  
73
  /**
74
   * @throws \RuntimeException
75
   */
76
  public function setTemplate(string $template): void {
77
    if(!is_file($template)) {
78
      throw new \RuntimeException("File $template does not exist.");
79
    }
80
    $this->template = $template;
81
  }
82
  
83
  /**
84
   * Adds new entry
85
   */
86
  public function log(array $action): void {
87 1
    $this->actions[$this->round][] = new CombatAction($this->translator, $action);
88 1
  }
89
  
90
  /**
91
   * Adds text entry
92
   */
93
  public function logText(string $text, array $params = []): void {
94 1
    $this->actions[$this->round][] = $this->translator->translate($text, 0, $params);
95 1
  }
96
  
97
  public function __toString(): string {
98
    $params = [
99 1
      "team1" => $this->team1, "team2" => $this->team2, "actions" => $this->actions, "title" => $this->title,
100
    ];
101 1
    return $this->latte->renderToString($this->template, $params);
102
  }
103
  
104
  public function count(): int {
105 1
    return count($this->actions);
106
  }
107
  
108
  public function getIterator(): \ArrayIterator {
109 1
    return new \ArrayIterator($this->actions);
110
  }
111
}
112
?>