MiniGameCommandHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 11
c 2
b 0
f 1
lcom 1
cbo 15
dl 0
loc 189
ccs 89
cts 89
cp 1
rs 9.1666

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B handleCreateGameCommand() 0 25 2
A handleStartGameCommand() 0 23 2
A handleJoinGameCommand() 0 23 2
A handleLeaveGameCommand() 0 23 2
A handleGameMoveCommand() 0 23 2
1
<?php
2
3
namespace MiniGameApp\Handler;
4
5
use MiniGameApp\Command\CreateGameCommand;
6
use MiniGameApp\Command\GameMoveCommand;
7
use MiniGameApp\Command\JoinGameCommand;
8
use MiniGameApp\Command\LeaveGameCommand;
9
use MiniGameApp\Command\StartGameCommand;
10
use MiniGameApp\Error\ErrorEventHandler;
11
use MiniGameApp\Event\MiniGameAppErrorEvent;
12
use MiniGameApp\Event\UnableToAddPlayerEvent;
13
use MiniGameApp\Event\UnableToCreateGameEvent;
14
use MiniGameApp\MiniGameFactory;
15
use MiniGameApp\Repository\GameRepository;
16
use Psr\Log\LoggerAwareInterface;
17
use Psr\Log\LoggerAwareTrait;
18
use Psr\Log\NullLogger;
19
use RemiSan\Context\ContextContainer;
20
21
class MiniGameCommandHandler implements LoggerAwareInterface
22
{
23
    use LoggerAwareTrait;
24
25
    /**
26
     * @var MiniGameFactory
27
     */
28
    protected $builder;
29
30
    /**
31
     * @var GameRepository
32
     */
33
    private $gameManager;
34
35
    /**
36
     * @var ErrorEventHandler
37
     */
38
    private $errorHandler;
39
40
    /**
41
     * Constructor
42
     *
43
     * @param MiniGameFactory   $builder
44
     * @param GameRepository    $gameManager
45
     * @param ErrorEventHandler $errorHandler
46
     */
47 33
    public function __construct(
48
        MiniGameFactory $builder,
49
        GameRepository $gameManager,
50
        ErrorEventHandler $errorHandler
51
    ) {
52 33
        $this->builder = $builder;
53 33
        $this->gameManager = $gameManager;
54 33
        $this->errorHandler = $errorHandler;
55 33
        $this->logger = new NullLogger();
56 33
    }
57
58
    /**
59
     * Handles a CreateGameCommand
60
     *
61
     * @param  CreateGameCommand $command
62
     * @return void
63
     */
64 6
    public function handleCreateGameCommand(CreateGameCommand $command)
65
    {
66 6
        ContextContainer::setContext($command->getContext());
67
68
        try {
69 6
            $miniGame = $this->builder->createMiniGame(
70 6
                $command->getGameId(),
71 6
                $command->getPlayerId(),
72 6
                $command->getOptions()
73 4
            );
74
75 3
            $this->gameManager->save($miniGame);
76 5
        } catch (\Exception $e) {
77 3
            $this->errorHandler->handle(
78 3
                new UnableToCreateGameEvent(
79 3
                    $command->getGameId(),
80 3
                    $command->getPlayerId(),
81 3
                    $e->getMessage()
82 2
                ),
83 3
                $command->getContext()
84 2
            );
85
        }
86
87 6
        ContextContainer::reset();
88 6
    }
89
90
    /**
91
     * Handles a StartGameCommand
92
     *
93
     * @param  StartGameCommand $command
94
     * @return void
95
     */
96 6
    public function handleStartGameCommand(StartGameCommand $command)
97
    {
98 6
        ContextContainer::setContext($command->getContext());
99
100
        try {
101 6
            $miniGame = $this->gameManager->load($command->getGameId());
102
103 6
            $miniGame->startGame($command->getPlayerId());
104
105 3
            $this->gameManager->save($miniGame);
106 5
        } catch (\Exception $e) {
107 3
            $this->errorHandler->handle(
108 3
                new MiniGameAppErrorEvent(
109 3
                    $command->getGameId(),
110 3
                    $command->getPlayerId(),
111 3
                    $e->getMessage()
112 2
                ),
113 3
                $command->getContext()
114 2
            );
115
        }
116
117 6
        ContextContainer::reset();
118 6
    }
119
120
    /**
121
     * Handles a JoinGameCommand
122
     *
123
     * @param JoinGameCommand $command
124
     * @return void
125
     */
126 6
    public function handleJoinGameCommand(JoinGameCommand $command)
127
    {
128 6
        ContextContainer::setContext($command->getContext());
129
130
        try {
131 6
            $miniGame = $this->gameManager->load($command->getGameId());
132
133 6
            $miniGame->addPlayerToGame($command->getPlayerOptions());
134
135 3
            $this->gameManager->save($miniGame);
136 5
        } catch (\Exception $e) {
137 3
            $this->errorHandler->handle(
138 3
                new UnableToAddPlayerEvent(
139 3
                    $command->getGameId(),
140 3
                    $command->getPlayerId(),
141 3
                    $e->getMessage()
142 2
                ),
143 3
                $command->getContext()
144 2
            );
145
        }
146
147 6
        ContextContainer::reset();
148 6
    }
149
150
    /**
151
     * Handles a LeaveGameCommand
152
     *
153
     * @param  LeaveGameCommand $command
154
     * @return void
155
     */
156 6
    public function handleLeaveGameCommand(LeaveGameCommand $command)
157
    {
158 6
        ContextContainer::setContext($command->getContext());
159
160
        try {
161 6
            $miniGame = $this->gameManager->load($command->getGameId());
162
163 6
            $miniGame->leaveGame($command->getPlayerId());
164
165 3
            $this->gameManager->save($miniGame);
166 5
        } catch (\Exception $e) {
167 3
            $this->errorHandler->handle(
168 3
                new MiniGameAppErrorEvent(
169 3
                    $command->getGameId(),
170 3
                    $command->getPlayerId(),
171 3
                    $e->getMessage()
172 2
                ),
173 3
                $command->getContext()
174 2
            );
175
        }
176
177 6
        ContextContainer::reset();
178 6
    }
179
180
    /**
181
     * Handles a GameMoveCommand
182
     *
183
     * @param  GameMoveCommand $command
184
     * @return void
185
     */
186 9
    public function handleGameMoveCommand(GameMoveCommand $command)
187
    {
188 9
        ContextContainer::setContext($command->getContext());
189
190
        try {
191 9
            $miniGame = $this->gameManager->load($command->getGameId());
192
193 6
            $miniGame->play($command->getPlayerId(), $command->getMove());
194
195 3
            $this->gameManager->save($miniGame);
196 8
        } catch (\Exception $e) {
197 6
            $this->errorHandler->handle(
198 6
                new MiniGameAppErrorEvent(
199 6
                    $command->getGameId(),
200 6
                    $command->getPlayerId(),
201 6
                    $e->getMessage()
202 4
                ),
203 6
                $command->getContext()
204 4
            );
205
        }
206
207 9
        ContextContainer::reset();
208 9
    }
209
}
210