Game::getHomeResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Eekes\VblApi\Entity;
5
6
class Game
7
{
8
    const RESULT_WIN = 'win';
9
    const RESULT_LOSS = 'loss';
10
    const RESULT_DRAW = 'draw';
11
12
    private string $id;
13
    private string $gameId;
14
    private string $homeTeamId;
15
    private string $awayTeamId;
16
    private string $homeTeamName;
17
    private string $awayTeamName;
18
    private \DateTimeImmutable $start;
19
    private ?string $accommodationId;
20
    private ?string $accommodationName;
21
    private string $groupId;
22
    private string $groupName;
23
    private int $homeScore = 0;
24
    private int $awayScore = 0;
25
    private string $homeResult;
26
    private string $awayResult;
27
28
    public static function createFromResponse(\stdClass $response): self
29
    {
30
        $game = new static;
31
32
        $game->id = $response->guid;
33
        $game->gameId = $response->wedID;
34
        $game->homeTeamId = $response->tTGUID;
35
        $game->homeTeamName = $response->tTNaam;
36
        $game->awayTeamId = $response->tUGUID;
37
        $game->awayTeamName = $response->tUNaam;
38
        $game->accommodationId = $response->accGUID;
39
        $game->accommodationName = $response->accNaam;
40
        $game->groupId = $response->pouleGUID;
41
        $game->groupName = $response->pouleNaam;
42
        $game->start = $game->buildStartDateTime($response->datumString, $response->beginTijd);
43
44
        $scores = explode("-", $response->uitslag);
45
46
        if (isset($scores[1])) {
47
            $game->homeScore = (int)$scores[0];
48
            $game->awayScore = (int)$scores[1];
49
50
            if ($game->getHomeScore() === $game->getAwayScore()) {
51
                $game->homeResult = self::RESULT_DRAW;
52
                $game->awayResult = self::RESULT_DRAW;
53
            } elseif ($game->getHomeScore() > $game->getAwayScore()) {
54
                $game->homeResult = self::RESULT_WIN;
55
                $game->awayResult = self::RESULT_LOSS;
56
            } else {
57
                $game->homeResult = self::RESULT_LOSS;
58
                $game->awayResult = self::RESULT_WIN;
59
            }
60
        }
61
62
        return $game;
63
    }
64
65
    private function buildStartDateTime(string $date, string $time): \DateTimeImmutable
66
    {
67
        if (empty($time)) {
68
            $time = '00.00';
69
        }
70
71
        return \DateTimeImmutable::createFromFormat('d-m-Y H.i', $date . ' ' . $time);
72
    }
73
74
    public function getId(): string
75
    {
76
        return $this->id;
77
    }
78
79
    public function getGameId(): string
80
    {
81
        return $this->gameId;
82
    }
83
84
    public function getHomeTeamId(): string
85
    {
86
        return $this->homeTeamId;
87
    }
88
89
    public function getAwayTeamId(): string
90
    {
91
        return $this->awayTeamId;
92
    }
93
94
    public function getHomeTeamName(): string
95
    {
96
        return $this->homeTeamName;
97
    }
98
99
    public function getAwayTeamName(): string
100
    {
101
        return $this->awayTeamName;
102
    }
103
104
    public function getStart(): \DateTimeImmutable
105
    {
106
        return $this->start;
107
    }
108
109
    public function getAccommodationId(): ?string
110
    {
111
        return $this->accommodationId;
112
    }
113
114
    public function getAccommodationName(): ?string
115
    {
116
        return $this->accommodationName;
117
    }
118
119
    public function getGroupId(): string
120
    {
121
        return $this->groupId;
122
    }
123
124
    public function getGroupName(): string
125
    {
126
        return $this->groupName;
127
    }
128
129
    public function getHomeScore(): int
130
    {
131
        return $this->homeScore;
132
    }
133
134
    public function getAwayScore(): int
135
    {
136
        return $this->awayScore;
137
    }
138
139
    public function getHomeResult(): string
140
    {
141
        return $this->homeResult;
142
    }
143
144
    public function getAwayResult(): string
145
    {
146
        return $this->awayResult;
147
    }
148
149
    public function isPlayed(): bool
150
    {
151
        if ($this->start < new \DateTimeImmutable('now')) {
152
            return true;
153
        }
154
155
        return false;
156
    }
157
}
158