HangmanOptions::checkOptions()   C
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 6.6037
c 0
b 0
f 0
cc 8
eloc 13
nc 4
nop 0
crap 8
1
<?php
2
3
namespace Hangman\Options;
4
5
use MiniGame\Exceptions\IllegalOptionException;
6
use MiniGame\GameOptions;
7
use MiniGame\Options\AbstractGameOptions;
8
use MiniGame\PlayerOptions;
9
use WordSelector\Entity\Word;
10
11
class HangmanOptions extends AbstractGameOptions implements GameOptions
12
{
13
    /**
14
     * @var int
15
     */
16
    private $lives;
17
18
    /**
19
     * @var int
20
     */
21
    private $length;
22
23
    /**
24
     * @var int
25
     */
26
    private $level;
27
28
    /**
29
     * @var Word
30
     */
31
    private $word;
32
33
    /**
34
     * @var string
35
     */
36
    private $language;
37
38
    /**
39
     * Constructor
40
     */
41 21
    public function __construct()
42
    {
43 21
    }
44
45
    /**
46
     * @return int
47
     */
48 9
    public function getLives()
49
    {
50 9
        return $this->lives;
51
    }
52
53
    /**
54
     * @return int
55
     */
56 9
    public function getLength()
57
    {
58 9
        return $this->length;
59
    }
60
61
    /**
62
     * @return int
63
     */
64 9
    public function getLevel()
65
    {
66 9
        return $this->level;
67
    }
68
69
    /**
70
     * @return string
71
     */
72 9
    public function getWord()
73
    {
74 9
        return $this->word;
75
    }
76
77
    /**
78
     * @return string
79
     */
80 3
    public function getLanguage()
81
    {
82 3
        return $this->language;
83
    }
84
85
    /**
86
     * Check the options
87
     *
88
     * @throws IllegalOptionException
89
     */
90 21
    private function checkOptions()
91
    {
92 21
        if ($this->length !== null && $this->word) {
93 6
            throw new IllegalOptionException(
94 6
                "You can't set the length if the word is already chosen!",
95 6
                'length',
96 6
                $this->length
97 4
            );
98
        }
99
100 15
        if ($this->level !== null && $this->word) {
101 3
            throw new IllegalOptionException(
102 3
                "You can't set the level if the word is already chosen!",
103 3
                'level',
104 3
                $this->level
105 2
            );
106
        }
107
108 12
        if ($this->word === null && $this->level === null && $this->length === null) {
109 3
            throw new \InvalidArgumentException('You have to provide at least one option (word/length/level)!');
110
        }
111 9
    }
112
113
    /**
114
     * Static Constructor.
115
     *
116
     * @param  Word            $word
117
     * @param  string          $language
118
     * @param  int             $length
119
     * @param  int             $level
120
     * @param  int             $lives
121
     * @param  PlayerOptions[] $players
122
     *
123
     * @throws IllegalOptionException
124
     *
125
     * @return HangmanOptions
126
     */
127 21
    public static function create(
128
        Word $word = null,
129
        $language = 'en',
130
        $length = null,
131
        $level = null,
132
        $lives = 6,
133
        array $players = []
134
    ) {
135 21
        $obj = new self();
136
137 21
        $obj->init($players);
138 21
        $obj->lives = $lives;
139 21
        $obj->word = $word;
140 21
        $obj->language = $language;
141 21
        $obj->length = $length;
142 21
        $obj->level = $level;
143
144 21
        $obj->checkOptions();
145
146 9
        return $obj;
147
    }
148
}
149