Passed
Push — main ( b436ac...f1b4ad )
by N.
04:39
created

Matcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 115
ccs 39
cts 41
cp 0.9512
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hämta_matcher() 0 40 3
A spara_matcher() 0 42 2
A __construct() 0 2 1
1
<?php
2
3
/**
4
 * Klass Matcher.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Klasser;
11
12
use PDO;
13
14
/**
15
 * Klass Matcher.
16
 * Hämta och spara matchdata.
17
 */
18
final class Matcher {
19
	public string $spelstopp = '';
20
	public bool $komplett = false;
21
	/**
22
	 * @var string[] $match
23
	 */
24
	public array $match;
25
	/**
26
	 * @var int[] $matchstatus
27
	 */
28
	public array $matchstatus;
29
	/**
30
	 * @var string[] $resultat
31
	 */
32
	public array $resultat;
33
34
	/**
35
	 * Initiera.
36
	 * Visa matcher i fliken för omgång.
37
	 */
38 12
	public function __construct(public Spel $spel) {
39 12
		$this->hämta_matcher();
40
	}
41
42
	/**
43
	 * Hämta enskild match från databas.
44
	 */
45 12
	public function hämta_matcher(): void {
46
		/**
47
		 * Initiera tomma fält.
48
		 */
49 12
		[$this->match, $this->resultat] = [TOMRAD, TOMRAD];
50 12
		$this->matchstatus = NOLLRAD;
51
52
		/**
53
		 * Hämta matcher från aktuell omgång.
54
		 */
55 12
		$sats = $this->spel->db->instans->prepare('SELECT *
56 12
			FROM `matcher` WHERE `omgång`=:omgang AND `speltyp`=:speltyp LIMIT 1');
57 12
		$sats->bindValue(':omgang', $this->spel->omgång, PDO::PARAM_INT);
58 12
		$sats->bindValue(':speltyp', $this->spel->speltyp->value, PDO::PARAM_INT);
59 12
		$sats->execute();
60
61
		/**
62
		 * Kräv data.
63
		 * Ibland skickas tomma data.
64
		 */
65 12
		if ($sats === false) {
66
			return;
67
		}
68
69
		/**
70
		 * Hämta rader.
71
		 * Iterera över matchkolumner.
72
		 */
73 12
		$rad = $sats->fetchAll(PDO::FETCH_ASSOC)[0];
74 12
		$this->spelstopp = $rad["spelstopp"];
75 12
		$this->komplett = (bool) $rad["komplett"];
76
77
		/**
78
		 * Populera fält för matcher, resultat och matchstatus.
79
		 */
80 12
		foreach (MATCHKOLUMNER as $match) {
81 12
			$nyckel = $match - 1;
82 12
			$this->match[$nyckel] = $rad["match$match"];
83 12
			$this->resultat[$nyckel] = $rad["resultat$match"];
84 12
			$this->matchstatus[$nyckel] = $rad["status$match"];
85
		}
86
	}
87
88
	/**
89
	 * Spara matcher till databas.
90
	 */
91 1
	public function spara_matcher(): void {
92
		/**
93
		 * Kompaktera och stränga matchdata.
94
		 */
95 1
		$cstr = implode(', ', array_map(fn (int $i): string => "`match$i`, `resultat$i`, `status$i`", MATCHKOLUMNER));
96 1
		$vstr = implode(', ', array_map(fn (int $i): string => ":match$i, :resultat$i, :status$i", MATCHKOLUMNER));
97
98
		/**
99
		 * Är alla matcher spelade och inte lottade?
100
		 */
101 1
		$this->komplett = !in_array(0, $this->matchstatus, true);
102
103
		/**
104
		 * Ersätt matchdata för aktuell omgång.
105
		 */
106 1
		$sats = $this->spel->db->instans->prepare("REPLACE INTO `matcher`
107 1
			(`omgång`, `speltyp`, `spelstopp`, `komplett`, $cstr) VALUES
108 1
			(:omgang, :speltyp, :spelstopp, :komplett, $vstr)");
109 1
		$sats->bindValue(':omgang', $this->spel->omgång, PDO::PARAM_INT);
110 1
		$sats->bindValue(':speltyp', $this->spel->speltyp->value, PDO::PARAM_INT);
111 1
		$sats->bindValue(':spelstopp', $this->spelstopp, PDO::PARAM_STR);
112 1
		$sats->bindValue(':komplett', $this->komplett, PDO::PARAM_BOOL);
113
114
		/**
115
		 * Bind fält för matcher, resultat och matchstatus till platshållare.
116
		 */
117 1
		foreach (MATCHKOLUMNER as $match) {
118 1
			$nyckel = $match - 1;
119 1
			$sats->bindValue(":match$match", $this->match[$nyckel], PDO::PARAM_STR);
120 1
			$sats->bindValue(":resultat$match", $this->resultat[$nyckel], PDO::PARAM_STR);
121 1
			$sats->bindValue(":status$match", $this->matchstatus[$nyckel], PDO::PARAM_STR);
122
		}
123
124
		/**
125
		 * Logga.
126
		 */
127 1
		$kommentar = match ($sats->execute()) {
128 1
			true => ": ✅ Sparade matcher.",
129
			false => ": ❌ Kunde inte spara matcher."
130 1
		};
131
132 1
		$this->spel->db->logg->logga(self::class . "$kommentar ({$this->spel->omgång})");
133
	}
134
}
135