PlayTrait::play()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace MiniGame\Entity;
4
5
use MiniGame\Exceptions\IllegalMoveException;
6
use MiniGame\GameResult;
7
use MiniGame\Move;
8
9
trait PlayTrait
10
{
11
    /**
12
     * Allows the player to play the game
13
     *
14
     * @param  PlayerId $playerId
15
     * @param  Move $move
16
     *
17
     * @throws IllegalMoveException
18
     *
19
     * @return GameResult
20
     */
21 6
    public function play(PlayerId $playerId, Move $move)
22
    {
23 6
        $playMethod = 'play' . $this->getClassName($move);
24
25 6
        if (! method_exists($this, $playMethod)) {
26 3
            throw new IllegalMoveException($move, 'Error: move was not recognized!');
27
        }
28
29 3
        return $this->$playMethod($playerId, $move);
30
    }
31
32
    /**
33
     * @param  Move $move
34
     *
35
     * @return string
36
     */
37 6
    private function getClassName(Move $move)
38
    {
39 6
        $namespaceArray = explode('\\', get_class($move));
40 6
        $underscoreArray = explode('_', end($namespaceArray));
41 6
        return end($underscoreArray);
42
    }
43
}
44