Passed
Push — main ( a387cb...339f6d )
by Cornelia
07:55
created

CardFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 1
b 0
f 0
dl 0
loc 32
ccs 2
cts 6
cp 0.3333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createCard() 0 7 2
A __construct() 0 3 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