Passed
Push — master ( 2a0f07...98f021 )
by Jakub
02:32
created

CharacterEffect   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
wmc 21
eloc 65
dl 0
loc 139
ccs 58
cts 60
cp 0.9667
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 5
A configureOptions() 0 22 3
A getAllowedSources() 0 2 1
A getDuration() 0 2 1
A getAllowedStats() 0 2 1
A getDurations() 0 2 1
A getId() 0 2 1
A setDuration() 0 5 3
A getType() 0 2 1
A getValue() 0 2 1
A getAllowedTypes() 0 2 1
A getStat() 0 2 1
A getSource() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nexendrie\Utils\Constants;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
/**
10
 * Data structure for effect on character
11
 *
12
 * @author Jakub Konečný
13
 * @property-read string $id
14
 * @property-read string $type
15
 * @property-read string $stat
16
 * @property-read int $value
17
 * @property-read string $source
18
 * @property int|string $duration
19
 * @method void onApply(Character $character, CharacterEffect $effect)
20
 * @method void onRemove(Character $character, CharacterEffect $effect)
21
 */
22 1
class CharacterEffect {
23 1
  use \Nette\SmartObject;
24
  
25
  public const SOURCE_PET = "pet";
26
  public const SOURCE_SKILL = "skill";
27
  public const SOURCE_EQUIPMENT = "equipment";
28
  public const DURATION_COMBAT = "combat";
29
  public const DURATION_FOREVER = "forever";
30
  
31
  /** @var string */
32
  protected $id;
33
  /** @var string */
34
  protected $type;
35
  /** @var string */
36
  protected $stat = "";
37
  /** @var int */
38
  protected $value = 0;
39
  /** @var string */
40
  protected $source;
41
  /** @var int|string */
42
  protected $duration;
43
  /** @var callable[] */
44
  public $onApply = [];
45
  /** @var callable[] */
46
  public $onRemove = [];
47
  
48
  public function __construct(array $effect) {
49 1
    $resolver = new OptionsResolver();
50 1
    $this->configureOptions($resolver);
51 1
    $effect = $resolver->resolve($effect);
52 1
    if(!in_array($effect["type"], SkillSpecial::NO_STAT_TYPES, true) AND $effect["stat"] === "") {
53
      throw new \InvalidArgumentException("The option stat with value '' is invalid.");
54
    }
55 1
    $this->id = $effect["id"];
56 1
    $this->type = $effect["type"];
57 1
    $this->stat = $effect["stat"];
58 1
    $this->value = $effect["value"];
59 1
    $this->source = $effect["source"];
60 1
    $this->duration = $effect["duration"];
61 1
    $this->onApply[] = function(Character $character, self $effect) {
62 1
      $character->recalculateStats();
63 1
      if($effect->stat === Character::STAT_MAX_HITPOINTS) {
64 1
        $character->heal($effect->value);
65
      }
66 1
    };
67 1
    $this->onRemove[] = function(Character $character, self $effect) {
68 1
      $character->recalculateStats();
69 1
      if($effect->stat === Character::STAT_MAX_HITPOINTS) {
70 1
        $character->harm($effect->value);
71
      }
72 1
    };
73 1
  }
74
75
  protected function configureOptions(OptionsResolver $resolver): void {
76 1
    $allStats = ["id", "type", "source", "value", "duration", "stat",];
77 1
    $resolver->setRequired($allStats);
78 1
    $resolver->setAllowedTypes("id", "string");
79 1
    $resolver->setAllowedTypes("type", "string");
80 1
    $resolver->setAllowedValues("type", function(string $value) {
81 1
      return in_array($value, $this->getAllowedTypes(), true);
82 1
    });
83 1
    $resolver->setAllowedTypes("stat", "string");
84 1
    $resolver->setDefault("stat", "");
85 1
    $resolver->setAllowedValues("stat", function(string $value) {
86 1
      return $value === "" OR in_array($value, $this->getAllowedStats(), true);
87 1
    });
88 1
    $resolver->setAllowedTypes("source", "string");
89 1
    $resolver->setAllowedValues("source", function(string $value) {
90 1
      return in_array($value, $this->getAllowedSources(), true);
91 1
    });
92 1
    $resolver->setAllowedTypes("value", "integer");
93 1
    $resolver->setDefault("value", 0);
94 1
    $resolver->setAllowedTypes("duration", ["string", "integer"]);
95 1
    $resolver->setAllowedValues("duration", function($value) {
96 1
      return (in_array($value, $this->getDurations(), true)) OR ($value > 0);
97 1
    });
98 1
  }
99
  
100
  protected function getAllowedStats(): array {
101 1
    return Constants::getConstantsValues(Character::class, "STAT_");
102
  }
103
  
104
  /**
105
   * @return string[]
106
   */
107
  protected function getAllowedSources(): array {
108 1
    return Constants::getConstantsValues(static::class, "SOURCE_");
109
  }
110
  
111
  /**
112
   * @return string[]
113
   */
114
  protected function getAllowedTypes(): array {
115 1
    return Constants::getConstantsValues(SkillSpecial::class, "TYPE_");
116
  }
117
  
118
  /**
119
   * @return string[]
120
   */
121
  protected function getDurations(): array {
122 1
    return Constants::getConstantsValues(static::class, "DURATION_");
123
  }
124
  
125
  public function getId(): string {
126 1
    return $this->id;
127
  }
128
  
129
  public function getType(): string {
130 1
    return $this->type;
131
  }
132
  
133
  public function getStat(): string {
134 1
    return $this->stat;
135
  }
136
  
137
  public function getValue(): int {
138 1
    return $this->value;
139
  }
140
  
141
  public function getSource(): string {
142 1
    return $this->source;
143
  }
144
  
145
  /**
146
   * @return int|string
147
   */
148
  public function getDuration() {
149 1
    return $this->duration;
150
  }
151
  
152
  /**
153
   * @param string|int $value
154
   * @throws \InvalidArgumentException
155
   */
156
  public function setDuration($value): void {
157 1
    if(!is_int($value) AND !in_array($value, $this->getDurations(), true)) {
158
      throw new \InvalidArgumentException("Invalid value set to CharacterEffect::\$duration. Expected string or integer.");
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 123 characters
Loading history...
159
    }
160 1
    $this->duration = $value;
161 1
  }
162
}
163
?>