1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFLScores\Models; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use NFLScores\Http\AbstractHttpClient; |
8
|
|
|
use NFLScores\Utilities\JSONParser; |
9
|
|
|
use PHPCollections\Collections\GenericList; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Manipulates NFL games related data. |
13
|
|
|
*/ |
14
|
|
|
class NFL |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The HTTP client for fetching remote data. |
18
|
|
|
* |
19
|
|
|
* @var \NFLScores\Http\AbstractHttpClient |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The current datetime, |
25
|
|
|
* represents today's date. |
26
|
|
|
* |
27
|
|
|
* @var \DateTime |
28
|
|
|
*/ |
29
|
|
|
private $today; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates a new NFL object. |
33
|
|
|
* |
34
|
|
|
* @param \NFLScores\Http\AbstractHttpClient $httpClient |
35
|
|
|
* @param \DateTime $dateTime |
36
|
|
|
*/ |
37
|
27 |
|
public function __construct(AbstractHttpClient $httpClient, DateTime $datetime) |
38
|
|
|
{ |
39
|
27 |
|
$this->client = $httpClient; |
40
|
27 |
|
$this->today = $datetime; |
41
|
27 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a collection with |
45
|
|
|
* the finished games for today |
46
|
|
|
* or null if there's not finished games. |
47
|
|
|
* |
48
|
|
|
* @return \PHPCollections\Collections\GenericList|null |
49
|
|
|
*/ |
50
|
4 |
|
public function getFinishedGames(): ?GenericList |
51
|
|
|
{ |
52
|
4 |
|
$todayGames = $this->getTodayGames(); |
53
|
|
|
|
54
|
4 |
|
if (is_null($todayGames)) { |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $todayGames->filter(function ($key, $game) { |
59
|
4 |
|
return in_array($game->score['phase'], ['FINAL', 'FINAL OVERTIME']); |
60
|
4 |
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns a collection with one |
65
|
|
|
* specific game or null if there's |
66
|
|
|
* no finished game by that team. |
67
|
|
|
* |
68
|
|
|
* @param string $team |
69
|
|
|
* |
70
|
|
|
* @return \PHPCollections\Collections\GenericList|null |
71
|
|
|
*/ |
72
|
1 |
|
public function getFinishedGameByTeam(string $team): ?GenericList |
73
|
|
|
{ |
74
|
1 |
|
$finishedGames = $this->getFinishedGames(); |
75
|
|
|
|
76
|
1 |
|
if (is_null($finishedGames)) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $finishedGames->filter(function ($key, $game) use ($team) { |
81
|
1 |
|
return $game->gameSchedule['homeTeamAbbr'] === $team || |
82
|
1 |
|
$game->gameSchedule['visitorTeamAbbr'] === $team; |
83
|
1 |
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns a collection of the games |
88
|
|
|
* that are currently being played |
89
|
|
|
* or null if there is no live games |
90
|
|
|
* at the moment. |
91
|
|
|
* |
92
|
|
|
* @return \PHPCollections\Collections\GenericList|null |
93
|
|
|
*/ |
94
|
10 |
|
public function getLiveGames(): ?GenericList |
95
|
|
|
{ |
96
|
10 |
|
$todayGames = $this->getTodayGames(); |
97
|
|
|
|
98
|
10 |
|
if (is_null($todayGames)) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $todayGames->filter(function ($key, $game) { |
103
|
10 |
|
return !in_array($game->score['phase'], ['FINAL', 'SUSPENDED', 'FINAL OVERTIME', null]); |
104
|
10 |
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns a collection with one |
109
|
|
|
* specific game or null if there's |
110
|
|
|
* no game being played by that team. |
111
|
|
|
* |
112
|
|
|
* @param string $team |
113
|
|
|
* |
114
|
|
|
* @return \PHPCollections\Collections\GenericList|null |
115
|
|
|
*/ |
116
|
7 |
|
public function getLiveGameByTeam(string $team): ?GenericList |
117
|
|
|
{ |
118
|
7 |
|
$livegames = $this->getLiveGames(); |
119
|
|
|
|
120
|
7 |
|
if (is_null($livegames)) { |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $livegames->filter(function ($key, $game) use ($team) { |
125
|
7 |
|
return $game->gameSchedule['homeTeamAbbr'] === $team || |
126
|
7 |
|
$game->gameSchedule['visitorTeamAbbr'] === $team; |
127
|
7 |
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns a collection of today games |
132
|
|
|
* or null if there is no games for today. |
133
|
|
|
* |
134
|
|
|
* @return \PHPCollections\Collections\GenericList|null |
135
|
|
|
*/ |
136
|
16 |
|
public function getTodayGames(): ?GenericList |
137
|
|
|
{ |
138
|
|
|
return $this->getWeekGames()->filter(function ($key, $game) { |
139
|
16 |
|
return str_replace('\/', '/', $game->gameSchedule['gameDate']) === $this->today->format('m/d/Y'); |
140
|
16 |
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns a collection of the games |
145
|
|
|
* to be played this week. |
146
|
|
|
* |
147
|
|
|
* @return \PHPCollections\Collections\GenericList |
148
|
|
|
*/ |
149
|
18 |
|
public function getWeekGames(): GenericList |
150
|
|
|
{ |
151
|
18 |
|
if (Cache::has('games')) { |
152
|
17 |
|
return Cache::get('games'); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
$games = new GenericList(Game::class); |
156
|
1 |
|
$data = $this->client->get(); |
157
|
1 |
|
$parsedData = JSONParser::parse($data); |
158
|
|
|
|
159
|
1 |
|
foreach ($parsedData['gameScores'] as $key => $gameData) { |
160
|
1 |
|
$games->add(new Game($gameData)); |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
Cache::put('games', $games, 1); |
164
|
|
|
|
165
|
1 |
|
return $games; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|