Passed
Push — main ( ee7bfe...8f061d )
by Karl
05:56
created

GameController::game()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
ccs 0
cts 0
cp 0
crap 6
rs 9.8333
1
<?php
2
3
namespace App\Controller;
4
5
# Generic use statements
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Routing\Attribute\Route;
9
use Symfony\Component\HttpFoundation\Request;
10
use App\Observer\SessionSavingObserver;
11
use App\Model\Game;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
14
class GameController extends AbstractController
15
{
16
    #[Route('/game', name: 'app_game')]
17
    public function index(): Response
18
    {
19
        return $this->render('game/index.html.twig');
20
    }
21
22
    #[Route('/game/doc', name: 'app_game_doc')]
23
    public function doc(): Response
24
    {
25
        return $this->render('game/doc.html.twig');
26
    }
27
28
    #[Route('/game/play/post', name: 'app_post_game')]
29
    public function processMove(Request $request): RedirectResponse
30
    {
31
        $session = $request->getSession();
32
        $gameState = $session->get('game');
33
        $game = Game::createFromSavedState((array) $gameState);
34
35
        $game->attach(new SessionSavingObserver($request, 'game'));
36
37
        try {
38
            $game->playRound($request->request->all());
39
        } catch (\Exception $e) {
40
            $this->addFlash('error', $e->getMessage());
41
        }
42
43
        return $this->redirectToRoute('app_play_game');
44
    }
45
46
    #[Route('/game/play', name: 'app_play_game', methods: ['GET'], defaults: ['game_needed' => true ])]
47
    public function play(Request $request): Response
48
    {
49
        $session = $request->getSession();
50
        $gameState = $session->get('game');
51
        $game = Game::createFromSavedState((array) $gameState);
52
53
        return $this->render('game/game.html.twig', [
54
            'game' => $game
55
        ]);
56
    }
57
58
    #[Route('/api/game', name: 'api_game')]
59
    public function game(Request $request): JsonResponse
0 ignored issues
show
Bug introduced by
The type App\Controller\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
60
    {
61
62
        $data = [];
63
        if ($request->getSession()->has('game')) {
64
            $game = Game::createFromSavedState((array) $request->getSession()->get('game'));
65
            $gameState = $game->getGameState();
66
            $data = [
67
                'players' => $gameState['players'],
68
                'currentPlayer' => $gameState['currentPlayer'],
69
                'pot' => $gameState['pot'],
70
                'gameStatus' => $gameState['gameStatus'],
71
                'bank_balance' => $game->getPlayers()['bank']->getBalance(),
72
                'player_balance' => $game->getPlayers()['human']->getBalance()
73
            ];
74
        }
75
        return new JsonResponse($data);
76
    }
77
}
78