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

CardGameController::drawCard()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

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 21
ccs 14
cts 14
cp 1
crap 2
rs 9.8333
1
<?php
2
3
namespace App\Controller;
4
5
use App\Card\Deck;
6
use Symfony\Component\HttpFoundation\Session\SessionInterface;
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Routing\Annotation\Route;
10
11
/**
12
 * Controller to handle card game.
13
 */
14
class CardGameController extends AbstractController
15
{
16
    /**
17
     * Render the home page of the card game.
18
     *
19
     * @return Response
20
     */
21 2
    #[Route("/card", name: "card_start")]
22
    public function home(): Response
23
    {
24 2
        return $this->render('card/home.html.twig');
25
    }
26
27
    /**
28
     * Show current session data.
29
     *
30
     * @param SessionInterface $session
31
     * @return Response
32
     */
33 2
    #[Route("/card/session", name: "show_session")]
34
    public function showSession(SessionInterface $session): Response
35
    {
36 2
        $sessionData = $session->all();
37
38 2
        return $this->render('card/session.html.twig', [
39 2
            'sessionData' => $sessionData,
40 2
        ]);
41
    }
42
43
    /**
44
     * Clear session data and redirect.
45
     *
46
     * @param SessionInterface $session
47
     * @return Response
48
     */
49 1
    #[Route("/card/session/delete", name: "delete_session")]
50
    public function deleteSession(SessionInterface $session): Response
51
    {
52 1
        $session->clear(); // Clear all session data
53
54
        // Add a flash message
55 1
        $this->addFlash('success', 'Session data has been deleted.');
56
57 1
        return $this->redirectToRoute('show_session');
58
    }
59
60
    /**
61
     * Display the deck in sorted order.
62
     *
63
     * @param SessionInterface $session
64
     * @return Response
65
     */
66 1
    #[Route("/card/deck/", name: "sorted_deck")]
67
    public function sorted(SessionInterface $session): Response
68
    {
69 1
        $deck = $this->getOrInitDeck($session);
70
71 1
        return $this->render('card/deck/sorted.html.twig', [
72 1
            'deck' => $deck->getCards()
73 1
        ]);
74
    }
75
76
    /**
77
     * Shuffle the deck.
78
     *
79
     * @param SessionInterface $session
80
     * @return Response
81
     */
82 5
    #[Route("/card/deck/shuffle", name: "shuffle_deck")]
83
    public function shuffle(SessionInterface $session): Response
84
    {
85 5
        $deck = $this->getOrInitDeck($session);
86 5
        $deck->shuffle();
87 5
        $session->set('deck', $deck);
88
89 5
        return $this->render('card/deck/shuffle.html.twig', [
90 5
            'deck' => $deck->getCards()
91 5
        ]);
92
    }
93
94
    /**
95
     * Draw a single card from the deck.
96
     *
97
     * @param SessionInterface $session
98
     * @return Response
99
     */
100 2
    #[Route("/card/deck/draw", name: "draw_card")]
101
    public function drawCard(SessionInterface $session): Response
102
    {
103 2
        $deck = $this->getOrInitDeck($session);
104 2
        $drawnCard = $deck->drawCard();
105 2
        if ($drawnCard !== null) {
106 1
            $remainingCardsCount = $deck->remainingCards();
107
108 1
            $session->set('drawnCard', $drawnCard);
109 1
            $session->set('remainingCardsCount', $remainingCardsCount);
110 1
            $session->set('remainingCards', $deck->getCards());
111
112 1
            return $this->render('card/deck/draw.html.twig', [
113 1
                'drawnCard' => $drawnCard,
114 1
                'remainingCardsCount' => $remainingCardsCount,
115 1
                'remainingCards' =>  $deck->getCards()
116 1
            ]);
117
        }
118
        // If no more cards in the deck
119
        // Redirect to empty deck page
120 1
        return $this->redirectToRoute('empty_deck');
121
    }
122
123
    /**
124
     * Draw multiple cards from the deck.
125
     *
126
     * @param int $num Number of cards to draw
127
     * @param SessionInterface $session
128
     * @return Response
129
     */
130 3
    #[Route("/card/deck/draw/{num<\d+>}", name: "draw_cards")]
131
    public function drawCards(int $num, SessionInterface $session): Response
132
    {
133 3
        $deck = $this->getOrInitDeck($session);
134
        // Draw the specified number of cards from the deck
135 3
        $drawnCards = [];
136 3
        for ($i = 0; $i < $num; $i++) {
137 3
            $drawnCard = $deck->drawCard();
138 3
            if ($drawnCard !== null) {
139 3
                $drawnCards[] = $drawnCard;
140 3
                continue;
141
            }
142 1
            return $this->redirectToRoute('empty_deck');
143
        }
144
145 3
        $remainingCardsCount = $deck->remainingCards();
146
147 3
        $session->set('drawnCards', $drawnCards);
148 3
        $session->set('remainingCardsCount', $remainingCardsCount);
149 3
        $session->set('remainingCards', $deck->getCards());
150
151 3
        $session->set('deck', $deck);
152
153 3
        return $this->render('card/deck/draw_few.html.twig', [
154 3
            'drawnCards' => $drawnCards,
155 3
            'remainingCards' =>  $deck->getCards(),
156 3
            'remainingCardsCount' => $deck->remainingCards()
157 3
        ]);
158
    }
159
160
    /**
161
     * Render a page that the deck is empty.
162
     *
163
     * @return Response
164
     */
165 1
    #[Route("/card/deck/empty", name: "empty_deck")]
166
    public function emptyDeck(): Response
167
    {
168 1
        return $this->render('card/deck/empty_deck.html.twig');
169
    }
170
171
    /** Helper method to get or init the deck */
172 6
    private function getOrInitDeck(SessionInterface $session): Deck
173
    {
174 6
        $deck = $session->get('deck');
175
176 6
        if (!$deck instanceof Deck) {
177 6
            $deck = new Deck();
178 6
            $session->set('deck', $deck);
179
        }
180
181 6
        return $deck;
182
    }
183
}
184