CombatLogEntry::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 11
rs 9.9666
ccs 9
cts 9
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
 * Data structure for combat action
10
 *
11
 * @author Jakub Konečný
12
 */
13
final class CombatLogEntry
14
{
15
    /** @internal */
16
    public const ACTION_POISON = "poison";
17
18
    public readonly Character $character1;
19
    public readonly Character $character2;
20
    public readonly string $action;
21
    public readonly string $name;
22
    public readonly bool $result;
23
    public readonly int $amount;
24
25
    public function __construct(array $action)
26
    {
27 1
        $resolver = new OptionsResolver();
28 1
        $this->configureOptions($resolver);
29 1
        $action = $resolver->resolve($action);
30 1
        $this->action = $action["action"];
31 1
        $this->result = $action["result"];
32 1
        $this->amount = $action["amount"];
33 1
        $this->character1 = clone $action["character1"];
34 1
        $this->character2 = clone $action["character2"];
35 1
        $this->name = $action["name"];
36 1
    }
37
38
    private function configureOptions(OptionsResolver $resolver): void
39
    {
40 1
        $requiredStats = ["action", "result", "character1", "character2",];
41 1
        $resolver->setDefined(["amount", "name",]);
42 1
        $resolver->setRequired($requiredStats);
43 1
        $resolver->setAllowedTypes("action", "string");
44 1
        $resolver->setAllowedTypes("result", "bool");
45 1
        $resolver->setAllowedTypes("amount", "integer");
46 1
        $resolver->setDefault("amount", 0);
47 1
        $resolver->setAllowedTypes("name", "string");
48 1
        $resolver->setDefault("name", "");
49 1
        $resolver->setAllowedTypes("character1", Character::class);
50 1
        $resolver->setAllowedTypes("character2", Character::class);
51 1
    }
52
}
53