TTHamtaTopptips::bearbeta_tipsdata()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 9
nop 1
dl 0
loc 29
ccs 0
cts 16
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Klass TTHamtaTopptips.
5
 * @author Niklas Dougherty
6
 */
7
8
declare(strict_types=1);
9
10
namespace Tips\Moduler\TT;
11
12
use Tips\Egenskaper\Varden;
13
use Tips\Egenskaper\Ajax;
14
use Tips\Moduler\TT;
15
16
/**
17
 * Klass TTHamtaTopptips.
18
 * @SuppressWarnings("PHPMD.UnusedPrivateMethod")
19
 */
20
final class TTHamtaTopptips extends Spara {
21
	use Varden;
22
	use Ajax;
0 ignored issues
show
Bug introduced by
The trait Tips\Egenskaper\Ajax requires the property $logg which is not provided by Tips\Moduler\TT\TTHamtaTopptips.
Loading history...
23
24
	/**
25
	 * Initiera. Uppdatera konstruktor.
26
	 */
27 20
	public function __construct(private TT $tt) {
28 20
		parent::__construct($this->tt);
29 20
		$this->hämta_värden($this->tt->utdelning->spel->db);
30 20
		$this->förgrena();
31
	}
32
33
	/**
34
	 * Hämta topptips vid Svenska spel.
35
	 * $_REQUEST['hämta_topptips']
36
	 */
37
	private function hämta_topptips(): bool {
0 ignored issues
show
Unused Code introduced by
The method hämta_topptips() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
38
		$url = SVENSKA_SPEL_API_URL . "{$this->tt->typer['produkt']}/draws?accesskey={$this->api}";
39
40
		// $objekt = (object) json_decode((string) file_get_contents(JSONFIL));
41
		$objekt = hämta_objekt($url);
42
43
		/**
44
		 * Säkerställ att data från Svenska spel finns.
45
		 */
46
		$draws = match (true) {
47
			$objekt === null => false,
48
			isset($objekt->draws) && $objekt->draws => $objekt->draws[0],
49
			default => false
50
		};
51
52
		/**
53
		 * Återvänd i annat fall.
54
		 */
55
		if (!$draws || !$draws->drawNumber) {
56
			$this->tt->utdelning->spel->db->logg->logga(self::class . ": ❌ Inga TT-data.");
57
			return false;
58
		}
59
60
		/**
61
		 * Vi har tipsdata från Svenska spel.
62
		 * Extrahera och returnera.
63
		 */
64
		$this->tt->omgång = $draws->drawNumber;
65
		$this->tt->omsättning = intval($draws->turnover);
66
		$this->tt->spelstopp = strval($draws->closeTime);
67
		$this->tt->överskjutande = intval($draws->fund->rolloverIn);
68
		$this->tt->extrapengar = intval($draws->fund->extraMoney);
69
70
		/**
71
		 * Extrahera omgångsdata.
72
		 */
73
		$this->bearbeta_tipsdata($draws->events);
74
75
		$this->tt->utdelning->spel->db->logg->logga(self::class . ": ✅ Hämtade TT-data. ({$this->tt->omgång})");
76
		$this->spara_data();
77
78
		return true;
79
	}
80
81
	/**
82
	 * Plocka ut data från JSON-objekt.
83
	 * @param object[] $events
84
	 */
85
	private function bearbeta_tipsdata(array $events): void {
86
		foreach ($events as $k => $event) {
87
			$index = intval($k);
88
89
			/**
90
			 * Plocka ut matcher.
91
			 */
92
			if (isset($event->participants)) {
93
				$this->tt->hemmalag[$index] = $event->participants[0]->name;
94
				$this->tt->bortalag[$index] = $event->participants[1]->name;
95
			}
96
97
			/**
98
			 * Plocka ut streck.
99
			 */
100
			if (isset($event->distribution)) {
101
				$this->tt->tt_streck[$index] = array_map(
102
					'floatval',
103
					[$event->distribution->home, $event->distribution->draw, $event->distribution->away]
104
				);
105
106
				/**
107
				 * Plocka ut odds.
108
				 */
109
				if (isset($event->odds->home, $event->odds->draw, $event->odds->away)) {
110
					$this->tt->tt_odds[$index] = $event->odds->home ? array_map(
111
						'flyttal',
112
						[$event->odds->home, $event->odds->draw, $event->odds->away]
113
					) : TOM_ODDSVEKTOR;
114
				}
115
			}
116
		}
117
	}
118
}
119