Passed
Push — main ( 22e7dc...c4a240 )
by Karl
05:37
created

CardDealer::deal()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 17
rs 9.6111
1
<?php
2
3
namespace App\Model;
4
5
use Exception;
6
7
class CardDealer
8
{
9
    public function deal(DeckOfCards $deck, int $numberOfPlayers, int $cardsPerPlayer): array
10
    {
11
        $hands = [];
12
        for ($i = 0; $i < $numberOfPlayers; $i++) {
13
            $hands[] = new CardHand();
14
        }
15
16
        for ($i = 0; $i < $cardsPerPlayer; $i++) {
17
            foreach ($hands as $hand) {
18
                if (!$deck->hasCards()) {
19
                    throw new Exception('Not enough cards to deal');
20
                }
21
                $hand->addCard($deck->drawCards(1)[0]);
22
            }
23
        }
24
25
        return $hands;
26
    }
27
}
28