Pet::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 7
c 4
b 0
f 0
dl 0
loc 9
rs 10
ccs 7
cts 7
cp 1
cc 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
8
/**
9
 * Pet
10
 *
11
 * @author Jakub Konečný
12
 */
13 1
final class Pet implements ICharacterEffectsProvider
14
{
15
    public readonly int $id;
16
    public bool $deployed;
17
    public readonly string $bonusStat;
18
    public readonly int $bonusValue;
19
20
    public function __construct(array $data)
21
    {
22 1
        $resolver = new OptionsResolver();
23 1
        $this->configureOptions($resolver);
24 1
        $data = $resolver->resolve($data);
25 1
        $this->id = $data["id"];
26 1
        $this->deployed = $data["deployed"];
27 1
        $this->bonusStat = $data["bonusStat"];
28 1
        $this->bonusValue = $data["bonusValue"];
29 1
    }
30
31
    private function configureOptions(OptionsResolver $resolver): void
32
    {
33 1
        $allStats = ["id", "deployed", "bonusStat", "bonusValue",];
34 1
        $resolver->setRequired($allStats);
35 1
        $resolver->setAllowedTypes("id", "integer");
36 1
        $resolver->setAllowedTypes("deployed", "boolean");
37 1
        $resolver->setAllowedTypes("bonusStat", "string");
38 1
        $resolver->setAllowedValues("bonusStat", function (string $value): bool {
39 1
            return in_array($value, $this->getAllowedStats(), true);
40 1
        });
41 1
        $resolver->setAllowedTypes("bonusValue", "integer");
42 1
        $resolver->setAllowedValues("bonusValue", function (int $value): bool {
43 1
            return ($value >= 0);
44 1
        });
45 1
    }
46
47
    protected function getAllowedStats(): array
48
    {
49 1
        return Character::BASE_STATS;
50
    }
51
52
    protected function getDeployParams(): array
53
    {
54
        return [
55 1
            "id" => "pet" . $this->id . "bonusEffect",
56 1
            "type" => SkillSpecial::TYPE_BUFF,
57 1
            "stat" => $this->bonusStat,
58 1
            "value" => $this->bonusValue,
59
            "valueAbsolute" => false,
60 1
            "duration" => CharacterEffect::DURATION_COMBAT,
61
        ];
62
    }
63
64
    public function getCombatEffects(): array
65
    {
66 1
        if (!$this->deployed) {
67 1
            return [];
68
        }
69 1
        return [new CharacterEffect($this->getDeployParams())];
70
    }
71
}
72