Passed
Push — master ( dd3471...ff1e5a )
by Jakub
02:42
created

TextCombatLogRender   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 81.08%

Importance

Changes 0
Metric Value
wmc 19
eloc 38
dl 0
loc 64
ccs 30
cts 37
cp 0.8108
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setTemplate() 0 5 2
A render() 0 3 1
C renderItem() 0 31 15
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nette\Bridges\ApplicationLatte\ILatteFactory;
7
use Nette\Localization\ITranslator;
8
9
/**
10
 * TextCombatLogRender
11
 *
12
 * @property string $template
13
 */
14 1
final class TextCombatLogRender implements ICombatLogRender {
15 1
  use \Nette\SmartObject;
16
17
  /** @var \Latte\Engine */
18
  protected $latte;
19
  /** @var ITranslator */
20
  protected $translator;
21
  /** @var string */
22
  protected $template = __DIR__ . "/CombatLog.latte";
23
24
  public function __construct(ILatteFactory $latteFactory, ITranslator $translator) {
25 1
    $this->latte = $latteFactory->create();
26 1
    $this->translator = $translator;
27 1
  }
28
29
  /**
30
   * @throws \RuntimeException
31
   */
32
  public function setTemplate(string $template): void {
33
    if(!is_file($template)) {
34
      throw new \RuntimeException("File $template does not exist.");
35
    }
36
    $this->template = $template;
37
  }
38
39
  public function render(array $params): string {
40 1
    $params["render"] = $this;
41 1
    return $this->latte->renderToString($this->template, $params);
42
  }
43
44
  /**
45
   * @param CombatLogEntry|string $item
46
   */
47
  public function renderItem($item): string {
48 1
    if(!$item instanceof CombatLogEntry) {
49 1
      return $item;
50
    }
51 1
    $character1 = $item->character1->name;
52 1
    $character2 = $item->character2->name;
53 1
    switch($item->action) {
54 1
      case CombatLogEntry::ACTION_ATTACK:
55 1
        $message = ($item->result) ? "combat.log.attackHits" : "combat.log.attackFails";
56 1
        $text = $this->translator->translate($message, $item->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 130 characters
Loading history...
57 1
        if($item->result AND $item->character2->hitpoints < 1) {
58
          $text .= $this->translator->translate("combat.log.characterFalls");
59
        }
60 1
        return $text;
61 1
      case CombatLogEntry::ACTION_SKILL_ATTACK:
62 1
        $message = ($item->result) ? "combat.log.specialAttackHits" : "combat.log.specialAttackFails";
63 1
        $text = $this->translator->translate($message, $item->amount, ["character1" => $character1, "character2" => $character2, "name" => $item->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 153 characters
Loading history...
64 1
        if($item->result AND $item->character2->hitpoints < 1) {
65
          $text .= $this->translator->translate("combat.log.characterFalls");
66
        }
67 1
        return $text;
68 1
      case CombatLogEntry::ACTION_SKILL_SPECIAL:
69 1
        $message = ($item->result) ? "combat.log.specialSkillSuccess" : "combat.log.specialSKillFailure";
70 1
        return $this->translator->translate($message, 0, ["character1" => $character1, "character2" => $character2, "name" => $item->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 140 characters
Loading history...
71 1
      case CombatLogEntry::ACTION_HEALING:
72 1
        $message = ($item->result) ? "combat.log.healingSuccess" : "combat.log.healingFailure";
73 1
        return $this->translator->translate($message, $item->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 129 characters
Loading history...
74 1
      case CombatLogEntry::ACTION_POISON:
75 1
        return $this->translator->translate("combat.log.poison", $item->amount, ["character1" => $character1]);
76
    }
77
    return "";
78
  }
79
}
80
?>