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
|
|
|
return $this->getTodayGames()->filter(function ($key, $game) { |
53
|
4 |
|
return in_array($game->score['phase'], ['FINAL', 'FINAL OVERTIME']); |
54
|
4 |
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns a collection with one |
59
|
|
|
* specific game or null if there's |
60
|
|
|
* no finished game by that team. |
61
|
|
|
* |
62
|
|
|
* @param string $team |
63
|
|
|
* |
64
|
|
|
* @return \PHPCollections\Collections\GenericList|null |
65
|
|
|
*/ |
66
|
1 |
|
public function getFinishedGameByTeam(string $team): ?GenericList |
67
|
|
|
{ |
68
|
|
|
return $this->getFinishedGames()->filter(function ($key, $game) use ($team) { |
69
|
1 |
|
return $game->gameSchedule['homeTeamAbbr'] === $team || |
70
|
1 |
|
$game->gameSchedule['visitorTeamAbbr'] === $team; |
71
|
1 |
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns a collection of the games |
76
|
|
|
* that are currently being played |
77
|
|
|
* or null if there is no live games |
78
|
|
|
* at the moment. |
79
|
|
|
* |
80
|
|
|
* @return \PHPCollections\Collections\GenericList|null |
81
|
|
|
*/ |
82
|
10 |
|
public function getLiveGames(): ?GenericList |
83
|
|
|
{ |
84
|
|
|
return $this->getWeekGames()->filter(function ($key, $game) { |
85
|
10 |
|
return !in_array($game->score['phase'], ['FINAL', 'SUSPENDED', 'FINAL OVERTIME', null]); |
86
|
10 |
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns a collection with one |
91
|
|
|
* specific game or null if there's |
92
|
|
|
* no game being played by that team. |
93
|
|
|
* |
94
|
|
|
* @param string $team |
95
|
|
|
* |
96
|
|
|
* @return \PHPCollections\Collections\GenericList|null |
97
|
|
|
*/ |
98
|
7 |
|
public function getLiveGameByTeam(string $team): ?GenericList |
99
|
|
|
{ |
100
|
|
|
return $this->getLiveGames()->filter(function ($key, $game) use ($team) { |
101
|
7 |
|
return $game->gameSchedule['homeTeamAbbr'] === $team || |
102
|
7 |
|
$game->gameSchedule['visitorTeamAbbr'] === $team; |
103
|
7 |
|
}); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns a collection of today games |
108
|
|
|
* or null if there is no games for today. |
109
|
|
|
* |
110
|
|
|
* @return \PHPCollections\Collections\GenericList|null |
111
|
|
|
*/ |
112
|
6 |
|
public function getTodayGames(): ?GenericList |
113
|
|
|
{ |
114
|
|
|
return $this->getWeekGames()->filter(function ($key, $game) { |
115
|
6 |
|
return str_replace('\/', '/', $game->gameSchedule['gameDate']) === $this->today->format('m/d/Y'); |
116
|
6 |
|
}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns a collection of the games |
121
|
|
|
* to be played this week. |
122
|
|
|
* |
123
|
|
|
* @return \PHPCollections\Collections\GenericList |
124
|
|
|
*/ |
125
|
18 |
|
public function getWeekGames(): GenericList |
126
|
|
|
{ |
127
|
18 |
|
if (Cache::has('games')) { |
128
|
17 |
|
return Cache::get('games'); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
$games = new GenericList(Game::class); |
132
|
1 |
|
$data = $this->client->get(); |
133
|
1 |
|
$parsedData = JSONParser::parse($data); |
134
|
|
|
|
135
|
1 |
|
foreach ($parsedData['gameScores'] as $key => $gameData) { |
136
|
1 |
|
$games->add(new Game($gameData)); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
Cache::put('games', $games, 1); |
140
|
|
|
|
141
|
1 |
|
return $games; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|