Completed
Push — master ( bbf2f6...0d82ae )
by Jakub
02:15
created

CombatLogger   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 67
ccs 16
cts 21
cp 0.7619
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getRound() 0 2 1
A logText() 0 2 1
A log() 0 2 1
A setRound() 0 2 1
A __construct() 0 3 1
A setTeams() 0 6 2
A count() 0 2 1
A __toString() 0 5 1
A getIterator() 0 2 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
 */
15 1
class CombatLogger implements \Countable, \IteratorAggregate {
16 1
  use \Nette\SmartObject;
17
  
18
  /** @var \Latte\Engine */
19
  protected $latte;
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
  
31
  public function __construct(ILatteFactory $latteFactory, ITranslator $translator) {
32 1
    $this->latte = $latteFactory->create();
33 1
    $this->translator = $translator;
34 1
  }
35
  
36
  /**
37
   * Set teams
38
   */
39
  public function setTeams(Team $team1, Team $team2): void {
40 1
    if(isset($this->team1)) {
41
      throw new ImmutableException("Teams has already been set.");
42
    }
43 1
    $this->team1 = $team1;
44 1
    $this->team2 = $team2;
45 1
  }
46
  
47
  public function getRound(): int {
48 1
    return $this->round;
49
  }
50
  
51
  public function setRound(int $round) {
52 1
    $this->round = $round;
53 1
  }
54
  
55
  /**
56
   * Adds new entry
57
   */
58
  public function log(array $action): void {
59 1
    $this->actions[$this->round][] = new CombatAction($this->translator, $action);
60 1
  }
61
  
62
  /**
63
   * Adds text entry
64
   */
65
  public function logText(string $text, array $params = []): void {
66 1
    $this->actions[$this->round][] = $this->translator->translate($text, 0, $params);
67 1
  }
68
  
69
  public function __toString(): string {
70
    $params = [
71
      "team1" => $this->team1, "team2" => $this->team2, "actions" => $this->actions
72
    ];
73
    return $this->latte->renderToString(__DIR__ . "/CombatLog.latte", $params);
74
  }
75
  
76
  public function count(): int {
77
    return count($this->actions);
78
  }
79
  
80
  public function getIterator(): \ArrayIterator {
81
    return new \ArrayIterator($this->actions);
82
  }
83
}
84
?>