Passed
Push — main ( c4a240...6a0053 )
by Karl
05:42
created

CardCollection::drawCards()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace App\Model;
4
5
use Exception;
6
7
class CardCollection
8
{
9
    private array $cards = [];
10
11
    public function addCard(Card $card): void
12
    {
13
        $this->cards[] = $card;
14
    }
15
16
    public function drawCards(int $numberOfCards): array
17
    {
18
        $drawnCards = [];
19
        for ($i = 0; $i < $numberOfCards; $i++) {
20
            if (empty($this->cards)) {
21
                throw new Exception('No cards left in the deck');
22
            }
23
            $drawnCards[] = array_pop($this->cards);
24
        }
25
        return $drawnCards;
26
    }
27
28
    public function getCards(): array
29
    {
30
        return $this->cards;
31
    }
32
33
    public function setCards(array $cards): void
34
    {
35
        $this->cards = $cards;
36
    }
37
38
    public function hasCards(): bool
39
    {
40
        return !empty($this->cards);
41
    }
42
43
    public function getNumberOfCards(): int
44
    {
45
        return count($this->cards);
46
    }
47
}
48