Passed
Branch main (02626f)
by Vedrana
05:25
created

Game21ControllerJson   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 19
cp 0
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A apiGame21() 0 26 2
1
<?php
2
3
namespace App\Controller;
4
5
use App\Card\Game21;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Routing\Annotation\Route;
9
use Symfony\Component\HttpFoundation\Session\SessionInterface;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
12
/**
13
 * API Controller for Game21.
14
 */
15
class Game21ControllerJson extends AbstractController
16
{
17
    /**
18
     * Returns the current state of the Game21 session.
19
     *
20
     * @param SessionInterface $session
21
     * @return JsonResponse The game state.
22
     */
23
    #[Route("/api/game", name: "api_game21", methods: ["GET"])]
24
    public function apiGame21(SessionInterface $session): Response
25
    {
26
        $game = $session->get('game21');
27
28
        if (!$game instanceof Game21) {
29
            return new JsonResponse([
30
                'error' => 'No active game found in session.'
31
            ], Response::HTTP_NOT_FOUND);
32
        }
33
34
        $playerHand = $game->getPlayerhand();
35
        $bankHand = $game->getBankHand();
36
37
        $data = [
38
            'player' => $playerHand->toArray(),
39
            'playerTotal' => $playerHand->getTotal(),
40
            'bank' => $bankHand->toArray(),
41
            'bankTotal' => $bankHand->getTotal(),
42
        ];
43
44
        $response = new JsonResponse($data);
45
        $response->setEncodingOptions(
46
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
47
        );
48
        return $response;
49
    }
50
}
51