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

TTHamtaTopptips::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
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;
0 ignored issues
show
Bug introduced by
The type Tips\Egenskaper\Varden was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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;
23
24
	/**
25
	 * Initiera. Uppdatera konstruktor.
26
	 */
27 3
	public function __construct(private TT $tt) {
28 3
		parent::__construct($this->tt);
29 3
		$this->hämta_värden($this->tt->utdelning->spel->db);
30 3
		$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}";
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\TT\SVENSKA_SPEL_API_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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;
0 ignored issues
show
Bug introduced by
The constant Tips\Moduler\TT\TOM_ODDSVEKTOR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
114
				}
115
			}
116
		}
117
	}
118
}
119