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

GameBlackJackService::processAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 4
nop 2
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Service;
4
5
use App\Card\BlackJack;
6
use App\Card\BlackJackRules;
7
use App\Card\Deck;
8
use App\Card\PayoutBlackJack;
9
10
/**
11
 * Service class for handling the core logic of the BlackJack game.
12
 *
13
 * This class handles the game setup, handling of user actions,
14
 * validating bets.
15
 */
16
class GameBlackJackService
17
{
18
    /**
19
     * Create a new instance of the BlackJack game with a fresh deck and hands.
20
     *
21
     * @return BlackJack A new game instance with shuffled deck and empty hands.
22
     */
23 2
    private function createGameInstance(): BlackJack
24
    {
25 2
        $deck = new Deck();
26 2
        $deck->shuffle();
27
28 2
        $rules = new BlackJackRules();
29 2
        $payout = new PayoutBlackJack();
30
31 2
        return new BlackJack($deck, $rules, $payout);
32
    }
33
34
    /**
35
     * Create a brand new BlackJack game with initial player settings.
36
     *
37
     * @param string $playerName The name of the player.
38
     * @param int $balance The starting balance for the player.
39
     * @param int $bet The amount the player bets.
40
     * @param int $numHands Number of hands to play this round.
41
     * @return BlackJack Initialized BlackJack game.
42
     */
43 2
    public function createGame(string $playerName, int $balance, int $bet, int $numHands = 1): BlackJack
44
    {
45 2
        $blackJack = $this->createGameInstance();
46 2
        $blackJack->setPlayerName($playerName);
47 2
        $blackJack->setBalance($balance);
48 2
        $blackJack->setBet($bet);
49 2
        $blackJack->startGame($numHands);
50
51 2
        return $blackJack;
52
    }
53
54
    /**
55
     * Start a new round while keeping the player's name and balance.
56
     *
57
     * @param BlackJack $prevRound The previous round's game object.
58
     * @param int $bet The new bet amount for the round.
59
     * @param int $numHands Number of hands to play this round.
60
     * @return BlackJack New BlackJack game.
61
     */
62 1
    public function newRound(BlackJack $prevRound, int $bet, int $numHands = 1): BlackJack
63
    {
64 1
        $newGame = $this->createGameInstance();
65 1
        $newGame->setPlayerName($prevRound->getPlayerName());
66 1
        $newGame->setBalance($prevRound->getBalance());
67 1
        $newGame->setBet($bet);
68 1
        $newGame->startGame($numHands);
69
70 1
        return $newGame;
71
    }
72
73
    /**
74
     * Process a player's action (draw or stay) during the current round.
75
     *
76
     * @param BlackJack $game The current game state.
77
     * @param string $action The action to process: 'draw' or 'stay'.
78
     * @return BlackJack The updated game state.
79
     */
80 1
    public function processAction(BlackJack $game, string $action): BlackJack
81
    {
82 1
        if (!$game->isGameOver()) {
83 1
            if ($action === 'draw') {
84 1
                $game->drawForPlayer();
85 1
            } elseif ($action === 'stay') {
86 1
                $game->stay();
87
            }
88
        }
89 1
        return $game;
90
    }
91
92
    /**
93
     * Initialize a new game or continue from an existing one.
94
     *
95
     * @param BlackJack|null $existingGame An existing game instance or null.
96
     * @param string $playerName The player's name.
97
     * @param int $betAmount The amount to bet.
98
     * @param int $numHands Number of hands to play this round.
99
     * @return BlackJack A new or initialized game.
100
     */
101 2
    public function initOrCreate(?BlackJack $existingGame, string $playerName, int $betAmount, int $numHands = 1): BlackJack
102
    {
103 2
        $balance = $existingGame instanceof BlackJack ? $existingGame->getBalance() : 1000;
104 2
        return $this->createGame($playerName, $balance, $betAmount, $numHands);
105
    }
106
}
107