BlackJackController::blackJackHit()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 24
ccs 12
cts 12
cp 1
crap 3
rs 9.9
1
<?php
2
3
namespace App\Controller;
4
5
use App\Game\BlackJack;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpFoundation\Session\SessionInterface;
9
use Symfony\Component\Routing\Annotation\Route;
10
11
class BlackJackController extends AbstractController
12
{
13 1
    #[Route('/game', name: 'game_start')]
14
    public function home(): Response
15
    {
16 1
        return $this->render('game/home.html.twig');
17
    }
18
19 1
    #[Route('/game/doc', name: 'game_doc')]
20
    public function doc(): Response
21
    {
22 1
        return $this->render('game/doc.html.twig');
23
    }
24
25 1
    #[Route('/game/blackJack', name: 'game_black_jack', methods: ['GET'])]
26
    public function blackJack(
27
        SessionInterface $session,
28
    ): Response {
29
        /** @var BlackJack $blackJack */
30 1
        $blackJack = $session->get('black_jack') ?? new BlackJack();
31
32
        // If dealer don't have cards === new BlackJack
33 1
        if (0 === count($blackJack->getDealer()->getString())) {
34 1
            $blackJack->newGame();
35
        }
36
37 1
        $session->set('black_jack', $blackJack);
38
39 1
        $data = $blackJack->stateOfGame();
40
41 1
        return $this->render('game/black_jack.html.twig', $data);
42
    }
43
44 1
    #[Route('/game/blackJack/hit', name: 'black_jack_hit', methods: ['POST'])]
45
    public function blackJackHit(
46
        SessionInterface $session,
47
    ): Response {
48
        /** @var BlackJack $blackJack */
49 1
        $blackJack = $session->get('black_jack') ?? new BlackJack();
50
51
        // If dealer don't have cards === new BlackJack
52 1
        if (0 === count($blackJack->getDealer()->getString())) {
53 1
            $blackJack->newGame();
54
        }
55
56 1
        $blackJack->hitPlayer();
57
58 1
        $session->set('black_jack', $blackJack);
59
60 1
        if ($blackJack->isPlayerBust()) {
61 1
            $this->addFlash(
62 1
                'warning',
63 1
                'You have gone bust!'
64 1
            );
65
        }
66
67 1
        return $this->redirectToRoute('game_black_jack');
68
    }
69
70 1
    #[Route('/game/blackJack/stay', name: 'black_jack_stay', methods: ['POST'])]
71
    public function blackJackStay(
72
        SessionInterface $session,
73
    ): Response {
74
        /** @var BlackJack $blackJack */
75 1
        $blackJack = $session->get('black_jack') ?? new BlackJack();
76
77
        // If dealer don't have cards === new BlackJack
78 1
        if (0 === count($blackJack->getDealer()->getString())) {
79 1
            $blackJack->newGame();
80
        }
81
82 1
        $blackJack->stayPlayer();
83
84 1
        $session->set('black_jack', $blackJack);
85
86 1
        return $this->redirectToRoute('game_black_jack');
87
    }
88
89 1
    #[Route('/game/blackJack/reset', name: 'black_jack_reset', methods: ['POST'])]
90
    public function blackJackReset(
91
        SessionInterface $session,
92
    ): Response {
93
        /** @var BlackJack $blackJack */
94 1
        $blackJack = $session->get('black_jack') ?? new BlackJack();
95
96 1
        $blackJack->newGame();
97
98 1
        $session->set('black_jack', $blackJack);
99
100 1
        return $this->redirectToRoute('game_black_jack');
101
    }
102
}
103