Passed
Push — main ( e5e48e...78d8c3 )
by N.
05:08
created

Utdelning   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 98.08%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 93
ccs 51
cts 52
cp 0.9808
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hämta_utdelning() 0 36 2
A __construct() 0 3 1
A spara_utdelning() 0 40 1
1
<?php
2
3
/**
4
 * Klass Utdelning.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Klasser;
11
12
use PDO;
13
use Tips\Klasser\Utdelning\Utdelningsdata;
14
15
/**
16
 * Klass Utdelning.
17
 * Utdelning samt antal vinnare i respektive vinstgrupp.
18
 * Även datum och tipsrad.
19
 */
20
final class Utdelning extends Utdelningsdata {
21
	/**
22
	 * Initiera.
23
	 */
24 6
	public function __construct(public Spel $spel) {
25 6
		parent::__construct($spel);
26 6
		$this->hämta_utdelning();
27
	}
28
29
	/**
30
	 * Hämta utdelning.
31
	 */
32 6
	public function hämta_utdelning(): void {
33 6
		$sats = $this->spel->db->instans->prepare("SELECT `år`, `vecka`, `tipsrad_012`,
34
			`u13`, `u12`, `u11`, `u10`, `a13`, `a12`, `a11`, `a10`
35 6
			FROM `utdelning` WHERE `omgång`=:omgang AND `speltyp`=:speltyp LIMIT 1");
36 6
		$sats->bindValue(':omgang', $this->spel->omgång, PDO::PARAM_INT);
37 6
		$sats->bindValue(':speltyp', $this->spel->speltyp->value, PDO::PARAM_INT);
38 6
		$sats->bindColumn('år', $this->år, PDO::PARAM_INT);
39 6
		$sats->bindColumn('vecka', $this->vecka, PDO::PARAM_INT);
40 6
		$sats->bindColumn('tipsrad_012', $this->tipsrad_012, PDO::PARAM_STR);
41
42
		/**
43
		 * Populera utdelning.
44
		 */
45 6
		$vektor = ['u13', 'u12', 'u11', 'u10'];
46 6
		array_map(
47 6
			fn ($index): bool =>
48 6
			$sats->bindColumn($vektor[$index], $this->utdelning[$index], PDO::PARAM_INT),
49 6
			array_keys($vektor)
50 6
		);
51
52
		/**
53
		 * Populera vinnare.
54
		 */
55 6
		$vektor = ['a13', 'a12', 'a11', 'a10'];
56 6
		array_map(
57 6
			fn ($index): bool =>
58 6
			$sats->bindColumn($vektor[$index], $this->vinnare[$index], PDO::PARAM_INT),
59 6
			array_keys($vektor)
60 6
		);
61
62 6
		$sats->execute();
63 6
		if ($sats->fetch(PDO::FETCH_ASSOC) !== false) {
64 6
			$this->tipsrad = siffror_till_symboler($this->tipsrad_012);
65
		}
66
67 6
		$this->har_tipsrad = ($this->tipsrad_012 !== '');
68
	}
69
70
	/**
71
	 * Spara utdelning.
72
	 */
73 1
	public function spara_utdelning(): void {
74 1
		$sats = $this->spel->db->instans->prepare("REPLACE INTO `utdelning`
75
			(`omgång`, `speltyp`, `år`, `vecka`, `tipsrad_012`,
76
			`u13`, `u12`, `u11`, `u10`, `a13`, `a12`, `a11`, `a10`)
77
			VALUES (:omgang, :speltyp, :ar, :vecka, :tipsrad_012,
78 1
			:u13, :u12, :u11, :u10, :a13, :a12, :a11, :a10)");
79 1
		$sats->bindValue(':omgang', $this->spel->omgång, PDO::PARAM_INT);
80 1
		$sats->bindValue(':speltyp', $this->spel->speltyp->value, PDO::PARAM_INT);
81 1
		$sats->bindValue(':ar', $this->år, PDO::PARAM_INT);
82 1
		$sats->bindValue(':vecka', $this->vecka, PDO::PARAM_INT);
83 1
		$sats->bindValue(':tipsrad_012', $this->tipsrad_012, PDO::PARAM_STR);
84
85
		/**
86
		 * Populera utdelning.
87
		 */
88 1
		$vektor = ['u13', 'u12', 'u11', 'u10'];
89 1
		array_map(
90 1
			fn ($index): bool =>
91 1
			$sats->bindValue($vektor[$index], $this->utdelning[$index], PDO::PARAM_INT),
92 1
			array_keys($vektor)
93 1
		);
94
95
		/**
96
		 * Populera vinnare.
97
		 */
98 1
		$vektor = ['a13', 'a12', 'a11', 'a10'];
99 1
		array_map(
100 1
			fn ($index): bool =>
101 1
			$sats->bindValue($vektor[$index], $this->vinnare[$index], PDO::PARAM_INT),
102 1
			array_keys($vektor)
103 1
		);
104
105
		/**
106
		 * Exekvera och logga.
107
		 */
108 1
		$kommentar = match ($sats->execute()) {
109 1
			true => ": ✅ Sparade utdelning.",
110
			false => ": ❌ Kunde inte spara utdelning."
111 1
		};
112 1
		$this->spel->db->logg->logga(self::class . "$kommentar ({$this->spel->omgång})");
113
	}
114
}
115