CardGameController::deckShuffle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Cards\CardHand;
6
use App\Cards\DeckOfCards;
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\Session\SessionInterface;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
class CardGameController extends AbstractController
14
{
15 1
    #[Route('/game/card', name: 'card_start')]
16
    public function cardHome(
17
        SessionInterface $session,
18
    ): Response {
19
        /** @var DeckOfCards $deck */
20 1
        $deck = $session->get('cards_deck') ?? new DeckOfCards();
21
22 1
        $session->set('cards_deck', $deck);
23
24 1
        return $this->render('card/home.html.twig');
25
    }
26
27 1
    #[Route('/game/card/session', name: 'card_session', methods: ['GET'])]
28
    public function session(
29
        SessionInterface $session,
30
    ): Response {
31
        /** @var DeckOfCards $deck */
32 1
        $deck = $session->get('cards_deck') ?? new DeckOfCards();
33
34 1
        $info = $deck->getString();
35
36 1
        $data = [
37 1
            'deck' => $info,
38 1
        ];
39
40 1
        return $this->render('card/session.html.twig', $data);
41
    }
42
43 7
    #[Route('/game/card/session/delete', name: 'card_session_delete', methods: ['GET'])]
44
    public function deleteSession(
45
        Request $request,
46
    ): Response {
47 7
        $request->getSession()->invalidate(1);
48
49 7
        $data = [
50 7
            'deck' => '',
51 7
        ];
52
53 7
        $this->addFlash(
54 7
            'notice',
55 7
            'Your session data has been deleted!'
56 7
        );
57
58 7
        return $this->render('card/session.html.twig', $data);
59
    }
60
61 1
    #[Route('/game/card/deck', name: 'card_deck')]
62
    public function deck(
63
        SessionInterface $session,
64
    ): Response {
65
        /** @var DeckOfCards $deck */
66 1
        $deck = $session->get('cards_deck') ?? new DeckOfCards();
67
68 1
        $deck->resetDeck();
69
70 1
        $session->set('cards_deck', $deck);
71
72 1
        $data = [
73 1
            'deck' => $deck->getString(),
74 1
        ];
75
76 1
        return $this->render('card/deck.html.twig', $data);
77
    }
78
79 1
    #[Route('/game/card/deck/shuffle', name: 'card_deck_shuffle')]
80
    public function deckShuffle(
81
        SessionInterface $session,
82
    ): Response {
83
        /** @var DeckOfCards $deck */
84 1
        $deck = $session->get('cards_deck') ?? new DeckOfCards();
85
86 1
        $deck->reshuffleDeck();
87
88 1
        $session->set('cards_deck', $deck);
89
90 1
        $data = [
91 1
            'deck' => $deck->getString(),
92 1
        ];
93
94 1
        return $this->render('card/deck.html.twig', $data);
95
    }
96
97 2
    #[Route('/game/card/deck/draw', name: 'card_deck_draw')]
98
    public function deckDraw(
99
        SessionInterface $session,
100
    ): Response {
101
        /** @var DeckOfCards $deck */
102 2
        $deck = $session->get('cards_deck') ?? new DeckOfCards();
103 2
        $hand = new CardHand();
104
105 2
        ($deck->cardCount() > 0) ?
106 1
        $hand->addCard($deck->drawCard()) :
107 1
        throw new \RuntimeException("Can't draw more cards as the deck is empty!");
108
109 1
        $session->set('cards_deck', $deck);
110
111 1
        $data = [
112 1
            'hand' => $hand->getString(),
113 1
            'deckNumber' => $deck->cardCount(),
114 1
        ];
115
116 1
        return $this->render('card/deck_draw.html.twig', $data);
117
    }
118
119 6
    #[Route("/game/card/deck/draw/{num<\d+>}", name: 'card_deck_draw_many')]
120
    public function deckDrawMany(
121
        int $num,
122
        SessionInterface $session,
123
    ): Response {
124 6
        if ($num > 52) {
125 1
            throw new \RuntimeException("Can't draw more than cards in deck!");
126
        }
127 5
        if ($num < 1) {
128 1
            throw new \RuntimeException("Can't draw less than 1 card!");
129
        }
130
131
        /** @var DeckOfCards $deck */
132 4
        $deck = $session->get('cards_deck') ?? new DeckOfCards();
133 4
        $hand = new CardHand();
134
135 4
        $cardCount = $deck->cardCount();
136
137 4
        if (0 == $cardCount) {
138 1
            throw new \RuntimeException("Can't draw more cards as the deck is empty!");
139
        }
140 4
        if ($cardCount < $num) {
141 1
            throw new \RuntimeException("
142
            Can't draw more cards as the deck currently have!\n
143 1
            The deck currently have ".$cardCount.' many cards in the deck.');
144
        }
145
146 4
        for ($i = 0; $i < $num; ++$i) {
147 4
            $hand->addCard($deck->drawCard());
148
        }
149
150 4
        $session->set('cards_deck', $deck);
151
152 4
        $data = [
153 4
            'hand' => $hand->getString(),
154 4
            'deckNumber' => $deck->cardCount(),
155 4
        ];
156
157 4
        return $this->render('card/deck_draw.html.twig', $data);
158
    }
159
}
160