Completed
Push — master ( ce79d3...3d8890 )
by Jakub
02:31
created

Pet::setDeployed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Symfony\Component\OptionsResolver\OptionsResolver,
7
    Nexendrie\Utils\Constants;
8
9
/**
10
 * Pet
11
 *
12
 * @author Jakub Konečný
13
 * @property-read int $id
14
 * @property bool $deployed
15
 * @property-read string $bonusStat
16
 * @property-read int $bonusValue
17
 */
18 1
class Pet implements ICharacterEffectProvider {
19 1
  use \Nette\SmartObject;
20
  
21
  public const STAT_STRENGTH = "strength";
22
  public const STAT_DEXTERITY = "dexterity";
23
  public const STAT_CONSTITUTION = "constitution";
24
  public const STAT_INTELLIGENCE = "intelligence";
25
  public const STAT_CHARISMA = "charisma";
26
  
27
  /** @var int */
28
  protected $id;
29
  /** @var bool */
30
  protected $deployed;
31
  /** @var string */
32
  protected $bonusStat;
33
  /** @var int */
34
  protected $bonusValue;
35
  
36
  public function __construct(array $data) {
37 1
    $allStats = ["id", "deployed", "bonusStat", "bonusValue",];
38 1
    $resolver = new OptionsResolver();
39 1
    $resolver->setRequired($allStats);
40 1
    $resolver->setAllowedTypes("id", "integer");
41 1
    $resolver->setAllowedTypes("deployed", "boolean");
42 1
    $resolver->setAllowedTypes("bonusStat", "string");
43 1
    $resolver->setAllowedValues("bonusStat", function(string $value) {
44 1
      return in_array($value, $this->getAllowedStats(), true);
45 1
    });
46 1
    $resolver->setAllowedTypes("bonusValue", "integer");
47 1
    $resolver->setAllowedValues("bonusValue", function(int $value) {
48 1
      return ($value >= 0);
49 1
    });
50 1
    $data = $resolver->resolve($data);
51 1
    $this->id = $data["id"];
52 1
    $this->deployed = $data["deployed"];
53 1
    $this->bonusStat = $data["bonusStat"];
54 1
    $this->bonusValue = $data["bonusValue"];
55 1
  }
56
  
57
  protected function getAllowedStats(): array {
58 1
    return Constants::getConstantsValues(static::class, "STAT_");
59
  }
60
  
61
  public function getId(): int {
62 1
    return $this->id;
63
  }
64
  
65
  public function isDeployed(): bool {
66 1
    return $this->deployed;
67
  }
68
  
69
  public function setDeployed(bool $deployed): void {
70 1
    $this->deployed = $deployed;
71 1
  }
72
  
73
  public function getBonusStat(): string {
74
    return $this->bonusStat;
75
  }
76
  
77
  public function getBonusValue(): int {
78
    return $this->bonusValue;
79
  }
80
  
81
  protected function getDeployParams(): array {
82
    return [
83 1
      "id" => "pet" . $this->id . "bonusEffect",
84 1
      "type" => "buff",
85 1
      "stat" => $this->bonusStat,
86 1
      "value" => $this->bonusValue,
87 1
      "source" => CharacterEffect::SOURCE_PET,
88 1
      "duration" => CharacterEffect::DURATION_COMBAT,
89
    ];
90
  }
91
  
92
  public function toCombatEffect(): ?CharacterEffect {
93 1
    if(!$this->deployed) {
94 1
      return NULL;
95
    }
96 1
    return new CharacterEffect($this->getDeployParams());
97
  }
98
}
99
?>