Passed
Push — main ( f27548...ea3e8e )
by Karl
05:47
created

DeckOfCards::drawCards()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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