Passed
Push — main ( afa687...718d9a )
by N.
04:20
created

Matcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 94.87%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 113
ccs 37
cts 39
cp 0.9487
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

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