SevenCardResult::createThreeOfAKind()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Cysha\Casino\Holdem\Cards\Results;
4
5
use Cysha\Casino\Cards\Card;
6
use Cysha\Casino\Cards\CardCollection;
7
use Cysha\Casino\Cards\Hand;
8
use Cysha\Casino\Cards\Results\StandardCardResult;
9
10
class SevenCardResult extends StandardCardResult
11
{
12
    const ROYAL_FLUSH = 9;
13
    const STRAIGHT_FLUSH = 8;
14
    const FOUR_OF_A_KIND = 7;
15
    const FULL_HOUSE = 6;
16
    const FLUSH = 5;
17
    const STRAIGHT = 4;
18
    const THREE_OF_A_KIND = 3;
19
    const TWO_PAIR = 2;
20
    const ONE_PAIR = 1;
21
    const HIGH_CARD = 0;
22
23
    /**
24
     * @param CardCollection $cards
25
     *
26
     * @return SevenCardResult
27
     */
28 4
    public static function createRoyalFlush(CardCollection $cards, Hand $hand): self
29
    {
30 4
        return new static(
31 4
            self::ROYAL_FLUSH,
32 4
            [0],
33
            $cards,
34 4
            'Royal Flush',
35
            $hand
36
        );
37
    }
38
39
    /**
40
     * @param CardCollection $cards
41
     *
42
     * @return SevenCardResult
43
     */
44 3
    public static function createStraightFlush(CardCollection $cards, Hand $hand): self
45
    {
46 3
        $highCard = $cards->sortByValue()->take(-1);
47 3
        $definition = sprintf('Straight Flush to %s', $highCard->first()->name());
48
49 3
        return new static(
50 3
            self::STRAIGHT_FLUSH,
51 3
            [$highCard->first()->value()],
52
            $cards,
53
            $definition,
54
            $hand
55
        );
56
    }
57
58
    /**
59
     * @param CardCollection $cards
60
     *
61
     * @return SevenCardResult
62
     */
63 5
    public static function createFourOfAKind(CardCollection $cards, Hand $hand): self
64
    {
65 5
        $cardGroups = $cards->groupByValue();
66
67 5
        $firstGroup = $cardGroups->first()->first();
68 5
        $kickerGroup = $cardGroups->last()->first();
69
70 5
        $definition = sprintf('4 of a Kind - %ss', $firstGroup->name());
71
72 5
        return new static(
73 5
            self::FOUR_OF_A_KIND,
74 5
            [$firstGroup->value(), $kickerGroup->value()],
75
            $cards,
76
            $definition,
77
            $hand
78
        );
79
    }
80
81
    /**
82
     * @param CardCollection $cards
83
     *
84
     * @return SevenCardResult
85
     */
86 3
    public static function createFullHouse(CardCollection $cards, Hand $hand): self
87
    {
88 3
        $cardGroups = $cards->groupByValue()->take(2);
89
90 3
        $firstCardGroup = $cardGroups->first()->first();
91 3
        $lastCardGroup = $cardGroups->last()->first();
92 3
        $definition = sprintf(
93 3
            'Full House - %ss over %ss',
94 3
            $firstCardGroup->name(),
95 3
            $lastCardGroup->name()
96
        );
97
98 3
        return new static(
99 3
            self::FULL_HOUSE,
100 3
            [$firstCardGroup->value(), $lastCardGroup->value()],
101
            $cards,
102
            $definition,
103
            $hand
104
        );
105
    }
106
107
    /**
108
     * @param CardCollection $cards
109
     *
110
     * @return SevenCardResult
111
     */
112 3
    public static function createFlush(CardCollection $cards, Hand $hand): self
113
    {
114 3
        $highCard = $cards->sortByValue()->take(-1);
115 3
        $definition = sprintf('Flush to %s', $highCard->first()->name());
116
117 3
        return new static(
118 3
            self::FLUSH,
119 3
            [$highCard->first()->value()],
120
            $cards,
121
            $definition,
122
            $hand
123
        );
124
    }
125
126
    /**
127
     * @param CardCollection $cards
128
     *
129
     * @return SevenCardResult
130
     */
131 9
    public static function createStraight(CardCollection $cards, Hand $hand): self
132
    {
133 9
        $highCard = $cards->sortByValue()->take(-1);
134 9
        $definition = sprintf('Straight to %s', $highCard->first()->name());
135
136 9
        return new static(
137 9
            self::STRAIGHT,
138 9
            [$highCard->first()->value()],
139
            $cards,
140
            $definition,
141
            $hand
142
        );
143
    }
144
145
    /**
146
     * @param CardCollection $cards
147
     *
148
     * @return SevenCardResult
149
     */
150 3
    public static function createThreeOfAKind(CardCollection $cards, Hand $hand): self
151
    {
152 3
        $cardGroups = $cards->groupByValue();
153
154 3
        $firstCard = $cardGroups->get(0)->first();
155 3
        $kicker = $cardGroups->get(1)->first();
156 3
        $definition = sprintf('3 of a Kind - %ss', $firstCard->name());
157
158 3
        return new static(
159 3
            self::THREE_OF_A_KIND,
160 3
            [$firstCard->value(), $kicker->value()],
161
            $cards,
162
            $definition,
163
            $hand
164
        );
165
    }
166
167
    /**
168
     * @param CardCollection $cards
169
     *
170
     * @return SevenCardResult
171
     */
172 13
    public static function createTwoPair(CardCollection $cards, Hand $hand): self
173
    {
174 13
        $cardGroups = $cards->groupByValue();
175 13
        $firstCard = $cardGroups->get(0)->first();
176 13
        $secondCard = $cardGroups->get(1)->first();
177 13
        $kickerCard = $cardGroups->get(2)->first();
178
179 13
        $definition = sprintf(
180 13
            'Two Pair - %ss and %ss',
181 13
            $firstCard->name(),
182 13
            $secondCard->name()
183
        );
184
185 13
        return new static(
186 13
            self::TWO_PAIR,
187 13
            [$firstCard->value(), $secondCard->value(), $kickerCard->value()],
188
            $cards,
189
            $definition,
190
            $hand
191
        );
192
    }
193
194
    /**
195
     * @param CardCollection $cards
196
     *
197
     * @return SevenCardResult
198
     */
199 7
    public static function createOnePair(CardCollection $cards, Hand $hand): self
200
    {
201 7
        $cardGroups = $cards->groupByValue();
202
203 7
        $pair = $cardGroups->get(0)->first();
204 7
        $kicker = $cardGroups->get(1)->first();
205 7
        $definition = sprintf('Pair of %ss', $pair->name());
206
207 7
        return new static(
208 7
            self::ONE_PAIR,
209 7
            [$pair->value(), $kicker->value()],
210
            $cards,
211
            $definition,
212
            $hand
213
        );
214
    }
215
216
    /**
217
     * @param CardCollection $cards
218
     *
219
     * @return SevenCardResult
220
     */
221 7
    public static function createHighCard(CardCollection $cards, Hand $hand): self
222
    {
223 7
        $cardValues = $cards->sortByValue()->values();
224
225 7
        $highCard = $cardValues->get(4);
226
227 7
        $definition = sprintf('High Card - %s', $highCard->name());
228
229 7
        return new static(
230 7
            self::HIGH_CARD,
231 7
            $cardValues->map(function (Card $card) {
232 7
                return $card->value();
233 7
            })->toArray(),
234
            $cards,
235
            $definition,
236
            $hand
237
        );
238
    }
239
}
240