Round::createStepResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
namespace FizzBuzzDomain;
4
5
use GameDomain\Player\PlayerInterface;
6
use GameDomain\Round\AbstractRound;
7
use GameDomain\Round\Step\Step;
8
use GameDomain\Round\Step\StepResult;
9
use GameDomain\Round\Step\StepResultCollection;
10
use GameDomain\Rule\AbstractRulesSet;
11
12
/**
13
 * Round
14
 */
15
final class Round extends AbstractRound
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20
    public function play(AbstractRulesSet $gameRules)
21
    {
22
        $roundResult = new StepResultCollection();
23
24
        foreach ($this->players as $player) {
25
            $stepResult = $this->createStepResult($gameRules, $player, $this->createStep());
26
            $roundResult->add($stepResult);
27
28
            if (!$stepResult->isValid()) {
29
                break;
30
            }
31
        }
32
33
        return $roundResult;
34
    }
35
36
    /**
37
     * @return \GameDomain\Round\Step\Step
38
     */
39
    protected function createStep()
40
    {
41
        return new Step($this->players->getPosition() + 1);
42
    }
43
44
    /**
45
     * @param \GameDomain\Rule\AbstractRulesSet  $gameRules
46
     * @param \GameDomain\Player\PlayerInterface $player
47
     * @param \GameDomain\Round\Step\Step        $step
48
     *
49
     * @return \GameDomain\Round\Step\StepResult
50
     */
51
    protected function createStepResult(AbstractRulesSet $gameRules, PlayerInterface $player, Step $step)
52
    {
53
        return new StepResult(
54
            $player,
55
            $player->play($gameRules, $step),
56
            $gameRules->generateValidAnswer($step->getRawValue()),
57
            $step
58
        );
59
    }
60
}
61