Passed
Push — main ( d29be2...38376b )
by N.
03:16
created

Spikar::hämta_request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Klass Spikar.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Moduler\Ajax\TTAjax;
11
12
use Tips\Moduler\TT\Konstanter;
13
use Tips\Klasser\Databas;
14
use Tips\Klasser\DBPreferenser;
15
16
/**
17
 * Klass Spikar.
18
 */
19
class Spikar {
20
	use Konstanter;
21
22
	protected Databas $db;
23
	protected DBPreferenser $db_preferenser;
24
25
	/**
26
	 * Initiera.
27
	 */
28
	public function __construct() {
29
		$this->db = new Databas();
30
		$this->db_preferenser = new DBPreferenser($this->db);
31
	}
32
33
	/**
34
	 * Spara spikar för Topptipset.
35
	 */
36
	protected function tt_spikar(): void {
37
		$antal_spikar = array_fill(0, self::TT_MAX_SPIKFÄLT, 0);
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\Ajax\TTAjax\Spikar::TT_MAX_SPIKFÄLT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
		$spikar = $this->hämta_request('tt_spikar');
39
		$andel_spikar = $this->hämta_request('tt_andel_spikar');
40
41
		/**
42
		 * Kontrollera format.
43
		 */
44
		foreach ($spikar as $index => $spik) {
45
			$spik = (array) filter_var_array((array) $spik, FILTER_SANITIZE_SPECIAL_CHARS);
46
			$spikar[$index] = $spik;
47
48
			/**
49
			 * Räkna upp antal spikar.
50
			 */
51
			foreach ($spik as $vektor) {
52
				if ($vektor != TOM_STRÄNGVEKTOR) {
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\Ajax\TTAjax\TOM_STRÄNGVEKTOR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53
					$antal_spikar[$index]++;
54
				}
55
			}
56
57
			$andel_spikar[$index] = min($andel_spikar[$index], $antal_spikar[$index]);
58
		}
59
60
		$this->db_preferenser->spara_preferens(
61
			"topptips.spikar",
62
			implode(',', array_merge([], ...array_merge([], ...$spikar)))
63
		);
64
65
		$this->db_preferenser->spara_preferens(
66
			"topptips.andel_spikar",
67
			implode(',', $andel_spikar)
68
		);
69
70
		echo true;
0 ignored issues
show
Bug introduced by
Are you sure true of type true can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
		echo /** @scrutinizer ignore-type */ true;
Loading history...
71
	}
72
73
	/**
74
	 * Hämta request.
75
	 * Kontrollera indata och returnera fält.
76
	 * @return mixed[]
77
	 */
78
	private function hämta_request(string $request): array {
79
		$_REQUEST[$request] = is_string($_REQUEST[$request]) ?
80
			$_REQUEST[$request] : '';
81
		parse_str($_REQUEST[$request], $resultat);
82
		$resultat = (array) array_values($resultat)[0];
83
		return $resultat;
84
	}
85
}
86