Passed
Push — main ( a57e19...f52e71 )
by Vedrana
28:10
created

Game21ControllerJson   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 34
ccs 19
cts 19
cp 1
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 2
    #[Route("/api/game", name: "api_game21", methods: ["GET"])]
24
    public function apiGame21(SessionInterface $session): Response
25
    {
26 2
        $game = $session->get('game21');
27
28 2
        if (!$game instanceof Game21) {
29 1
            return new JsonResponse([
30 1
                'error' => 'No active game found in session.'
31 1
            ], Response::HTTP_NOT_FOUND);
32
        }
33
34 1
        $playerHand = $game->getPlayerhand();
35 1
        $bankHand = $game->getBankHand();
36
37 1
        $data = [
38 1
            'player' => $playerHand->toArray(),
39 1
            'playerTotal' => $playerHand->getTotal(),
40 1
            'bank' => $bankHand->toArray(),
41 1
            'bankTotal' => $bankHand->getTotal(),
42 1
        ];
43
44 1
        $response = new JsonResponse($data);
45 1
        $response->setEncodingOptions(
46 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
47 1
        );
48 1
        return $response;
49
    }
50
}
51