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

DeckOfCards::dealCards()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 3
b 0
f 0
nc 3
nop 2
dl 0
loc 14
rs 9.9666
ccs 7
cts 7
cp 1
crap 3
1
<?php
2
3
namespace App\Model;
4
5
use App\Model\CardCollection;
6
use SplSubject;
7
use SplObjectStorage;
8
use SplObserver;
9
use Exception;
10
11
abstract class DeckOfCards implements SplSubject
12
{
13
    /**
14
     * @var array<string> The suits of the cards in a deck.
15
     */
16
    public static $suitsOfCards = ['Spade', 'Diamond', 'Heart', 'Club'];
17
18
    public static $namesOfCards = [
19
        'Ace' => [1, 14],
20
        '2' => [2],
21
        '3' => [3],
22
        '4' => [4],
23
        '5' => [5],
24
        '6' => [6],
25
        '7' => [7],
26
        '8' => [8],
27
        '9' => [9],
28
        '10' => [10],
29
        'Jack' => [11],
30
        'Queen' => [12],
31
        'King' => [13],
32
    ];
33
    /**
34
     * @var CardCollection The cards in the deck.
35
     **/
36
    public CardCollection $cardCollection;
37
38
    protected SplObjectStorage $observers;
39
40
    protected bool $isShuffled = false;
41
42
    /**
43
     * Constructor for the DeckOfCards class.
44
     *
45
     * @param array $observers An optional array of observer objects to attach to the deck.
46
     */
47
    public function __construct(array $observers = [])
48
    {
49
        $this->cardCollection = new CardCollection();
50 17
        $this->observers = new SplObjectStorage();
51
        foreach ($observers as $observer) {
52 17
            $this->attach($observer);
53 17
        }
54 17
    }
55 1
56
    public function getObservers(): SplObjectStorage
57
    {
58
        return $this->observers;
59 1
    }
60
61 1
    public function attach(SplObserver $observer): void
62
    {
63
        $this->observers->attach($observer);
64 2
    }
65
    public function detach(SplObserver $observer): void
66 2
    {
67
        $this->observers->detach($observer);
68 1
    }
69
    public function notify(): void
70 1
    {
71
        foreach ($this->observers as $observer) {
72 16
            $observer->update($this->getDeck());
73
        }
74 16
    }
75
76
    abstract public static function create(array $observers = []): DeckOfCards;
77
78
    abstract public function sort(): void;
79
80
    public function addCard(Card $card): void
81
    {
82
        $this->cardCollection->addCard($card);
83
        $this->notify();
84
    }
85
86
    /**
87
     * Get the deck of cards.
88
     *
89
     * @return array<Card> The array representing the deck of cards.
90
     */
91
    public function getDeck(): array
92
    {
93
        return $this->cardCollection->getCards();
94
    }
95
96
    public function setDeck(array $cards): void
97
    {
98
        $this->cardCollection->setCards($cards);
99
        $this->notify();
100
    }
101
102
    public function hasCards(): bool
103
    {
104
        return $this->cardCollection->hasCards();
105 16
    }
106
107 16
    public function getNumberOfCards(): int
108 16
    {
109
        return $this->cardCollection->getNumberOfCards();
110
    }
111 3
112
    /**
113
     * Draws a specified number of cards from the deck.
114 3
     *
115 3
     * @param int $numberOfCards The number of cards to draw.
116 3
     * @return array<Card> An array of drawn cards.
117
     */
118
    public function drawCards(int $numberOfCards): array
119
    {
120
        $drawnCards = $this->cardCollection->drawCards($numberOfCards);
121
        $this->notify();
122
        return $drawnCards;
123
    }
124
}
125