AbstractRulesSet::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace GameDomain\Rule;
4
5
use GameDomain\Exceptions\IrrelevantRuleException;
6
use Utils\Collections\ArrayCollection;
7
8
/**
9
 * Rules Set
10
 *
11
 * @method AbstractRule[] toArray()
12
 */
13
abstract class AbstractRulesSet extends ArrayCollection
14
{
15
    /**
16
     * Constructor
17
     */
18
    final public function __construct()
19
    {
20
        $this->loadRules();
21
    }
22
23
    /**
24
     * @param int $number
25
     *
26
     * @return \GameDomain\Round\Step\Answer
27
     * @throws \DomainException
28
     */
29
    final public function generateValidAnswer($number)
30
    {
31
        foreach ($this->toArray() as $gameRule) {
32
            try {
33
                return $gameRule->generateValidAnswer($number);
34
            } catch (IrrelevantRuleException $exception) {
35
                continue;
36
            }
37
        }
38
39
        throw new \DomainException('No valid answer can be generated from the current rules set.');
40
    }
41
42
    /**
43
     * Loads the rules
44
     */
45
    abstract protected function loadRules();
46
}
47