CardGameControllerJson::drawMoreCards()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 20
rs 9.7998
ccs 0
cts 14
cp 0
crap 2
1
<?php
2
3
namespace App\Controller;
4
5
use App\Card\Deck;
6
use App\Card\CardJoker;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Routing\Annotation\Route;
10
use Symfony\Component\HttpFoundation\Session\SessionInterface;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
13
/**
14
 * Controller Card Game JSON API.
15
 */
16
class CardGameControllerJson extends AbstractController
17
{
18
    /**
19
     * Get the current deck of cards as a JSON response.
20
     *
21
     * @param SessionInterface $session
22
     * @return JsonResponse JSON array representation of the deck.
23
     */
24
    #[Route("/api/deck", name: "api_sorted", methods: ["GET"])]
25
    public function getDeck(SessionInterface $session): JsonResponse
26
    {
27
        $deck = $this->getOrInitDeck($session);
28
29
        return new JsonResponse($deck->getCardsAsArray());
30
    }
31
32
    /**
33
     * Shuffle the deck and update the session.
34
     *
35
     * @param SessionInterface $session
36
     * @return JsonResponse JSON array representation of the shuffled deck.
37
     */
38
    #[Route("/api/deck/shuffle", name: "api_shuffle", methods: ["POST"])]
39
    public function shuffleDeck(SessionInterface $session): JsonResponse
40
    {
41
        $deck = $this->getOrInitDeck($session);
42
43
        $deck->shuffle();
44
45
        $session->set('deck', $deck);
46
47
        return new JsonResponse($deck->getCardsAsArray());
48
    }
49
50
    /**
51
     * Draw a single card from the deck.
52
     *
53
     * @param SessionInterface $session
54
     * @return JsonResponse JSON with drawn card, remaining cards count, and deck array.
55
     */
56
    #[Route("/api/deck/draw", name: "api_draw_card", methods: ["POST"])]
57
    public function drawCard(SessionInterface $session): JsonResponse
58
    {
59
        $deck = $this->getOrInitDeck($session);
60
61
        $drawnCard = $deck->drawCard();
62
63
        if ($drawnCard === null) {
64
            return new JsonResponse(['error' => 'The deck is empty.'], Response::HTTP_BAD_REQUEST);
65
        }
66
        $session->set('deck', $deck);
67
        $remainingCardsCount = $deck->remainingCards();
68
        $session->set('remainingCardsCount', $remainingCardsCount);
69
        $session->set('remainingCards', $deck->getCardsAsArray());
70
71
        // Return the drawn card, remaining cards count and deck as a JSON response
72
        return new JsonResponse([
73
            'drawnCard' => $drawnCard->toArray(),
74
            'remainingCardsCount' => $remainingCardsCount,
75
            'deck' => $deck->getCardsAsArray()
76
        ]);
77
    }
78
79
    /**
80
     * Draw multiple cards from the deck.
81
     *
82
     * @param int $number Number of cards to draw.
83
     * @param SessionInterface $session
84
     * @return JsonResponse JSON with drawn cards array, remaining cards count, and deck array.
85
     */
86
    #[Route("/api/deck/draw/{number}", name: "api_draw_cards", methods: ["POST"])]
87
    public function drawMoreCards(int $number, SessionInterface $session): JsonResponse
88
    {
89
        $deck = $this->getOrInitDeck($session);
90
91
        $drawnCards = $deck->drawMoreCards($number);
92
93
        $session->set('deck', $deck);
94
        $remainingCardsCount = $deck->remainingCards();
95
        $session->set('remainingCardsCount', $remainingCardsCount);
96
        $session->set('remainingCards', $deck->getCards());
97
98
        $drawnCardsArray = array_map(function ($card) {
99
            return $card->toArray();
100
        }, $drawnCards);
101
102
        return new JsonResponse([
103
            'drawnCard' => $drawnCardsArray,
104
            'remainingCardsCount' => $remainingCardsCount,
105
            'deck' => $deck->getCardsAsArray()
106
        ]);
107
    }
108
109
    /** Helper method to get or init the deck */
110
    private function getOrInitDeck(SessionInterface $session): CardJoker
111
    {
112
        $deck = $session->get('deck');
113
        if (!$deck instanceof CardJoker) {
114
            $deck = new CardJoker();
115
            $session->set('deck', $deck);
116
        }
117
        return $deck;
118
    }
119
}
120