GameAPIController::game()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 21
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 31
ccs 0
cts 23
cp 0
crap 6
rs 9.584
1
<?php
2
3
namespace App\Controller;
4
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Routing\Annotation\Route;
8
use App\Card\Game21;
9
use Exception;
10
use Symfony\Component\HttpFoundation\Session\SessionInterface;
11
12
/**
13
 * CardAPIController - defines the JSON API endpoints for cheings status of the 21 game.
14
 * Requires a session cookie with the ongoing game to be provided if not using the same same browser.
15
 */
16
class GameAPIController
17
{
18
    #[Route("/api/game", name: "api_game", methods: ['GET'])]
19
    public function game(
20
        SessionInterface $session
21
    ): Response {
22
23
        $game = $session->get("game");
24
        if (($game instanceof Game21) === false) {
25
            throw new Exception("Game not found in session");
26
        }
27
28
        $playerHand = $game->getPlayerHand();
29
        $handValue = $game->getHandValue($playerHand);
30
        $bankHand = $game->getBankHand();
31
        $bankHandValue = $game->getHandValue($bankHand);
32
        $turn = $game->getTurn();
33
        $winner = $game->getWinner();
34
35
        $data = [
36
            "player_hand" => $playerHand->getString(),
37
            "player_handValue" => $handValue,
38
            "bank_hand" => $bankHand->getString(),
39
            "bank_handValue" => $bankHandValue,
40
            "turn" => $turn,
41
            "winner" => $winner
42
            ];
43
44
        $response = new JsonResponse($data);
45
        $response->setEncodingOptions(
46
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
47
        );
48
        return $response;
49
    }
50
51
}
52