Investera::investera()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 72
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 24.3974

Importance

Changes 0
Metric Value
cc 5
eloc 34
nc 3
nop 0
dl 0
loc 72
ccs 3
cts 37
cp 0.0811
crap 24.3974
rs 9.0648
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Klass Investera.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Klasser;
11
12
use PDO;
13
use Tips\Klasser\Investera\Visa;
14
15
/**
16
 * Klass Investera.
17
 * Hanterar lagda spel på stryk- och europatips.
18
 * Tabell med vinster och utgifter samt motsvarande graf.
19
 */
20
final class Investera extends Visa {
21
	/**
22
	 * Hämta investeringsdata. Uppdatera.
23
	 * Spara i databas.
24
	 * Hantera grafer.
25
	 */
26 1
	public function investera(): void {
27
		/**
28
		 * Finns spelade rader?
29
		 */
30 1
		if ($this->tips->spelade->antal_utvalda_rader === 0) {
31 1
			return;
32
		}
33
34
		/**
35
		 * Hämta befintliga data.
36
		 * Om inga data finns föreligger ny post.
37
		 */
38
		$tid = date("Y-m-d H:i:s"); // defaulttid
39
		$this->vinstdata = TOM_VINSTMATRIS; // sätt default till tom
40
		$sats = $this->tips->spel->db->instans->prepare("SELECT `tid` FROM `investerade`
41
			WHERE `omgång`=:omgang AND `speltyp`=:speltyp AND `sekvens`=:sekvens LIMIT 1");
42
		$sats->bindValue(':omgang', $this->tips->utdelning->spel->omgång, PDO::PARAM_INT);
43
		$sats->bindValue(':speltyp', $this->tips->utdelning->spel->speltyp->value, PDO::PARAM_INT);
44
		$sats->bindValue(':sekvens', $this->tips->utdelning->spel->sekvens, PDO::PARAM_INT);
45
		$sats->bindColumn('tid', $tid, PDO::PARAM_STR);
46
		$sats->execute();
47
		$sats->fetchColumn();
48
		$sats->closeCursor();
49
50
		/**
51
		 * Är omgången avgjord?
52
		 * Radera tidigare grafer.
53
		 */
54
		if ($this->tips->utdelning->tipsrad_012 !== '') {
55
			$this->radera_graf($this->fil);
56
			$this->radera_graf($this->fil_ack);
57
58
			/**
59
			 * Plussa på vinstdata.
60
			 */
61
			foreach ($this->tips->spelade->tipsvektor as $tipsrad_012) {
62
				$rätt = antal_rätt($tipsrad_012, $this->tips->utdelning->tipsrad_012);
63
				if ($rätt >= 10) {
64
					$this->vinstdata[$rätt]++;
65
				}
66
			}
67
		}
68
69
		/**
70
		 * Slå samman vinstsumman.
71
		 */
72
		$vinst = vektorprodukt(array_reverse($this->vinstdata), $this->tips->utdelning->utdelning);
73
74
		/**
75
		 * Delete och insert av investdata.
76
		 * Inget behov av att kolla existens.
77
		 */
78
		$sats = $this->tips->spel->db->instans->prepare("REPLACE INTO `investerade`
79
			(`omgång`, `speltyp`, `sekvens`, `genererade`, `valda`, `vinst`, `tid`, `u10`, `u11`, `u12`, `u13`)
80
			VALUES (:omgang, :speltyp, :sekvens, :genererade, :valda, :vinst, :tid, :u10, :u11, :u12, :u13)");
81
		$sats->bindValue(':omgang', $this->tips->utdelning->spel->omgång, PDO::PARAM_INT);
82
		$sats->bindValue(':speltyp', $this->tips->utdelning->spel->speltyp->value, PDO::PARAM_INT);
83
		$sats->bindValue(':sekvens', $this->tips->utdelning->spel->sekvens, PDO::PARAM_INT);
84
		$sats->bindValue(':genererade', $this->tips->spelade->antal_genererade, PDO::PARAM_INT);
85
		$sats->bindValue(':valda', $this->tips->spelade->antal_utvalda_rader, PDO::PARAM_INT);
86
		$sats->bindValue(':vinst', $vinst, PDO::PARAM_INT);
87
		$sats->bindValue(':tid', $tid, PDO::PARAM_STR);
88
		$sats->bindValue(':u10', $this->vinstdata[10], PDO::PARAM_INT);
89
		$sats->bindValue(':u11', $this->vinstdata[11], PDO::PARAM_INT);
90
		$sats->bindValue(':u12', $this->vinstdata[12], PDO::PARAM_INT);
91
		$sats->bindValue(':u13', $this->vinstdata[13], PDO::PARAM_INT);
92
		$sats->execute();
93
94
		/**
95
		 * Logga händelse.
96
		 */
97
		$this->tips->spel->db->logg->logga(self::class . ": ✅ Investerade. ({$this->tips->utdelning->spel->omgång}, {$this->tips->utdelning->spel->sekvens})");
98
	}
99
100
	/**
101
	 * Radera graf om sådan finns.
102
	 */
103
	private function radera_graf(string $fil): void {
104
		if (is_file(GRAF . $fil)) {
105
			unlink(GRAF . $fil);
106
		}
107
	}
108
}
109