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

CharacterEffect   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 18
dl 0
loc 121
ccs 46
cts 50
cp 0.92
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedSources() 0 2 1
A getDuration() 0 2 1
A getAllowedStats() 0 4 1
A getDurations() 0 2 1
A getId() 0 2 1
A setDuration() 0 5 3
B __construct() 0 34 5
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
    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
 */
20 1
class CharacterEffect {
21 1
  use \Nette\SmartObject;
22
  
23
  public const SOURCE_PET = "pet";
24
  public const SOURCE_SKILL = "skill";
25
  public const SOURCE_EQUIPMENT = "equipment";
26
  public const DURATION_COMBAT = "combat";
27
  public const DURATION_FOREVER = "forever";
28
  
29
  /** @var string */
30
  protected $id;
31
  /** @var string */
32
  protected $type;
33
  /** @var string */
34
  protected $stat = "";
35
  /** @var int */
36
  protected $value = 0;
37
  /** @var string */
38
  protected $source;
39
  /** @var int|string */
40
  protected $duration;
41
  
42
  public function __construct(array $effect) {
43 1
    $allStats = ["id", "type", "source", "value", "duration", "stat",];
44 1
    $resolver = new OptionsResolver();
45 1
    $resolver->setRequired($allStats);
46 1
    $resolver->setAllowedTypes("id", "string");
47 1
    $resolver->setAllowedTypes("type", "string");
48 1
    $resolver->setAllowedValues("type", function(string $value) {
49 1
      return in_array($value, $this->getAllowedTypes(), true);
50 1
    });
51 1
    $resolver->setAllowedTypes("stat", "string");
52 1
    $resolver->setDefault("stat", "");
53 1
    $resolver->setAllowedValues("stat", function(string $value) {
54 1
      return $value === "" OR in_array($value, $this->getAllowedStats(), true);
55 1
    });
56 1
    $resolver->setAllowedTypes("source", "string");
57 1
    $resolver->setAllowedValues("source", function(string $value) {
58 1
      return in_array($value, $this->getAllowedSources(), true);
59 1
    });
60 1
    $resolver->setAllowedTypes("value", "integer");
61 1
    $resolver->setDefault("value", 0);
62 1
    $resolver->setAllowedTypes("duration", ["string", "integer"]);
63 1
    $resolver->setAllowedValues("duration", function($value) {
64 1
      return (in_array($value, $this->getDurations(), true)) OR ($value > 0);
65 1
    });
66 1
    $effect = $resolver->resolve($effect);
67 1
    if(!in_array($effect["type"], SkillSpecial::NO_STAT_TYPES, true) AND $effect["stat"] === "") {
68
      throw new \InvalidArgumentException("The option stat with value '' is invalid.");
69
    }
70 1
    $this->id = $effect["id"];
71 1
    $this->type = $effect["type"];
72 1
    $this->stat = $effect["stat"];
73 1
    $this->value = $effect["value"];
74 1
    $this->source = $effect["source"];
75 1
    $this->duration = $effect["duration"];
76 1
  }
77
  
78
  protected function getAllowedStats(): array {
79 1
    $stats = Constants::getConstantsValues(SkillSpecial::class, "STAT_");
80 1
    $stats = array_merge($stats, ["strength", "dexterity", "constitution", "intelligence", "charisma",]);
81 1
    return $stats;
82
  }
83
  
84
  /**
85
   * @return string[]
86
   */
87
  protected function getAllowedSources(): array {
88 1
    return Constants::getConstantsValues(static::class, "SOURCE_");
89
  }
90
  
91
  /**
92
   * @return string[]
93
   */
94
  protected function getAllowedTypes(): array {
95 1
    return Constants::getConstantsValues(SkillSpecial::class, "TYPE_");
96
  }
97
  
98
  /**
99
   * @return string[]
100
   */
101
  protected function getDurations(): array {
102 1
    return Constants::getConstantsValues(static::class, "DURATION_");
103
  }
104
  
105
  public function getId(): string {
106 1
    return $this->id;
107
  }
108
  
109
  public function getType(): string {
110 1
    return $this->type;
111
  }
112
  
113
  public function getStat(): string {
114 1
    return $this->stat;
115
  }
116
  
117
  public function getValue(): int {
118 1
    return $this->value;
119
  }
120
  
121
  public function getSource(): string {
122 1
    return $this->source;
123
  }
124
  
125
  /**
126
   * @return int|string
127
   */
128
  public function getDuration() {
129 1
    return $this->duration;
130
  }
131
  
132
  /**
133
   * @param string|int $value
134
   * @throws \InvalidArgumentException
135
   */
136
  public function setDuration($value) {
137
    if(!is_int($value) AND !in_array($value, $this->getDurations(), true)) {
138
      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...
139
    }
140
    $this->duration = $value;
141
  }
142
}
143
?>