Skip to content

Commit

Permalink
feat: Add filter by Sport.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiCO2k committed May 28, 2022
1 parent 479df53 commit e040b58
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
17 changes: 11 additions & 6 deletions app/Commands/GamesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

class GamesCommand extends Command
{
protected $signature = 'games';
protected $signature = 'games
{--sports= : The sports you want to only want to show}';

public function handle(Scores $scores): void
{
$sportsToShow = explode(',', $this->option('sports'));
$updateInSeconds = 20;
$sports = $this->getScores();
$sports = $this->getScores($sportsToShow);

live(function () use (&$updateInSeconds, &$sports) {
live(function () use (&$updateInSeconds, &$sports, $sportsToShow) {
if ($updateInSeconds === 0) {
$sports = $this->getScores();
$sports = $this->getScores($sportsToShow);
$updateInSeconds = 20;
}

Expand All @@ -28,8 +30,11 @@ public function handle(Scores $scores): void
})->refreshEvery(seconds: 1);
}

private function getScores(): array
/**
* @param $sportsToShow string[]
*/
private function getScores(array $sportsToShow = []): array
{
return (new Scores())->getScores();
return (new Scores())->getScores($sportsToShow);
}
}
12 changes: 10 additions & 2 deletions app/Scores.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ class Scores
/**
* Gets the Scores from the multiple sports available.
*
* @param $sportsToShow string[]
* @return array<string, Collection>
*/
public function getScores(): array
public function getScores(array $sportsToShow = []): array
{
$data = [];
$sports = self::$sportsAvailable;

foreach (self::$sportsAvailable as $sport => $class) {
$sportsToShow = array_filter($sportsToShow);
if ($sportsToShow !== []) {
$sports = collect(self::$sportsAvailable)
->filter(fn ($class, $sport) => in_array($sport, $sportsToShow));
}

foreach ($sports as $sport => $class) {
/** @var ScoresApi $class */
$data[$sport] = (new $class())->fetch();
}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/sport-leagues.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
@endforeach
</div>
@empty
<div class="text-gray italic">There are no live games currently in progress</div>
<div class="text-gray italic">There are no live games currently in progress...</div>
@endforelse

0 comments on commit e040b58

Please sign in to comment.