Passed
Push — main ( b83d00...e1d27e )
by N.
04:48
created

Matcher::spara_matcher()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 42
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2.0004

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 0
dl 0
loc 42
ccs 20
cts 21
cp 0.9524
crap 2.0004
rs 9.6333
c 0
b 0
f 0
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
	 * Init.
36
	 */
37 12
	public function __construct(public Spel $spel) {
38 12
		$this->hämta_matcher();
39
	}
40
41
	/**
42
	 * Hämta enskild match från databas.
43
	 */
44 12
	public function hämta_matcher(): void {
45
		/**
46
		 * Initiera tomma fält.
47
		 */
48 12
		$this->match = TOMRAD;
49 12
		$this->resultat = 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 12
		if ($sats === false) {
61
			return;
62
		}
63
64
		/**
65
		 * Iterera över matchkolumner.
66
		 */
67 12
		$rad = $sats->fetchAll(PDO::FETCH_ASSOC)[0];
68 12
		$this->spelstopp = $rad["spelstopp"];
69 12
		$this->komplett = (bool) $rad["komplett"];
70
71
		/**
72
		 * Populera fält för matcher, resultat och matchstatus.
73
		 */
74 12
		foreach (MATCHKOLUMNER as $match) {
75 12
			$nyckel = $match - 1;
76 12
			$this->match[$nyckel] = $rad["match$match"];
77 12
			$this->resultat[$nyckel] = $rad["resultat$match"];
78 12
			$this->matchstatus[$nyckel] = $rad["status$match"];
79
		}
80
	}
81
82
	/**
83
	 * Spara matcher till databas.
84
	 */
85 1
	public function spara_matcher(): void {
86
		/**
87
		 * Kompaktera och stränga matchdata.
88
		 */
89 1
		$cstr = implode(', ', array_map(fn (int $i): string => "`match$i`, `resultat$i`, `status$i`", MATCHKOLUMNER));
90 1
		$vstr = implode(', ', array_map(fn (int $i): string => ":match$i, :resultat$i, :status$i", MATCHKOLUMNER));
91
92
		/**
93
		 * Är alla matcher spelade och inte lottade?
94
		 */
95 1
		$this->komplett = !in_array(0, $this->matchstatus, true);
96
97
		/**
98
		 * Ersätt matchdata för aktuell omgång.
99
		 */
100 1
		$sats = $this->spel->db->instans->prepare("REPLACE INTO `matcher`
101 1
			(`omgång`, `speltyp`, `spelstopp`, `komplett`, $cstr) VALUES
102 1
			(:omgang, :speltyp, :spelstopp, :komplett, $vstr)");
103 1
		$sats->bindValue(':omgang', $this->spel->omgång, PDO::PARAM_INT);
104 1
		$sats->bindValue(':speltyp', $this->spel->speltyp->value, PDO::PARAM_INT);
105 1
		$sats->bindValue(':spelstopp', $this->spelstopp, PDO::PARAM_STR);
106 1
		$sats->bindValue(':komplett', $this->komplett, PDO::PARAM_BOOL);
107
108
		/**
109
		 * Bind fält för matcher, resultat och matchstatus till platshållare.
110
		 */
111 1
		foreach (MATCHKOLUMNER as $match) {
112 1
			$nyckel = $match - 1;
113 1
			$sats->bindValue(":match$match", $this->match[$nyckel], PDO::PARAM_STR);
114 1
			$sats->bindValue(":resultat$match", $this->resultat[$nyckel], PDO::PARAM_STR);
115 1
			$sats->bindValue(":status$match", $this->matchstatus[$nyckel], PDO::PARAM_STR);
116
		}
117
118
		/**
119
		 * Logga.
120
		 */
121 1
		$kommentar = match ($sats->execute()) {
122 1
			true => ": ✅ Sparade matcher.",
123
			false => ": ❌ Kunde inte spara matcher."
124 1
		};
125
126 1
		$this->spel->db->logg->logga(self::class . "$kommentar ({$this->spel->omgång})");
127
	}
128
}
129