Passed
Branch main (339f6d)
by Cornelia
17:44 queued 07:47
created

CardFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Card;
4
5
/**
6
 * Class CardFactory.
7
 *
8
 * Factory class for creating Card or CardGraphic objects,
9
 * depending on configuration.
10
 */
11
class CardFactory
12
{
13
    /**
14
     * Whether to create graphic cards or plain text cards.
15
     */
16
    private bool $useGraphics;
17
18
    /**
19
     * CardFactory constructor.
20
     *
21
     * @param bool $useGraphics true to return CardGraphic instances, false for Card
22
     */
23 32
    public function __construct(bool $useGraphics = false)
24
    {
25 32
        $this->useGraphics = $useGraphics;
26
    }
27
28
    /**
29
     * Creates a card instance based on the factory's setting.
30
     *
31
     * @param string $suit  The suit of the card (e.g. Hearts).
32
     * @param string $value The value of the card (e.g. 10, Queen).
33
     *
34
     * @return Card|CardGraphic a new instance of Card or CardGraphic
35
     */
36
    public function createCard(string $suit, string $value)
37
    {
38
        if ($this->useGraphics) {
39
            return new CardGraphic($suit, $value);
40
        }
41
42
        return new Card($suit, $value);
43
    }
44
}
45