AbstractRulesSet   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2
Metric Value
wmc 4
cbo 2
dl 0
loc 34
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generateValidAnswer() 0 12 3
loadRules() 0 1 ?
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