Skip to content

Commit

Permalink
Adds an interactive command to get one league matches (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasaba committed Sep 20, 2021
1 parent 81e87c1 commit c480f85
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use lucasaba\RapidAPI\Command\CountriesCommand;
use lucasaba\RapidAPI\Command\FixturesCommand;
use lucasaba\RapidAPI\Command\GetLeagueMatchesCommand;
use lucasaba\RapidAPI\Command\LeaguesCommand;
use lucasaba\RapidAPI\Command\TeamsCommand;
use Symfony\Component\Console\Application;
Expand All @@ -15,5 +16,6 @@
$application->add(new LeaguesCommand());
$application->add(new TeamsCommand());
$application->add(new FixturesCommand());
$application->add(new GetLeagueMatchesCommand());

$application->run();
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
</include>
<exclude>
<directory suffix=".php">src/*/Tests</directory>
<directory suffix=".php">src/Command</directory>
<file>src/autoload.php</file>
</exclude>
</coverage>
Expand Down
102 changes: 102 additions & 0 deletions src/Command/GetLeagueMatchesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php


namespace lucasaba\RapidAPI\Command;


use lucasaba\RapidAPI\Entity\League;
use lucasaba\RapidAPI\Request\CountriesRequest;
use lucasaba\RapidAPI\Request\FixturesRequest;
use lucasaba\RapidAPI\Request\LeaguesRequest;
use lucasaba\RapidAPI\Request\RoundsRequest;
use lucasaba\RapidAPI\Response\CountriesResponse;
use lucasaba\RapidAPI\Response\FixturesResponse;
use lucasaba\RapidAPI\Response\LeaguesResponse;
use lucasaba\RapidAPI\Response\RoundsResponse;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;

class GetLeagueMatchesCommand extends AbstractApiCommand
{
const SEASON = 2021;
protected static $defaultName = 'app:get-league-matches';

protected function configure(): void
{
$this->setDescription('Gets league matches')
->addArgument('token', InputArgument::REQUIRED, 'RapidAPI token');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$client = $this->getClient($input->getArgument('token'));
$helper = $this->getHelper('question');

// Select country
$countryRequest = new CountriesRequest();
$response = $client->get($countryRequest, CountriesResponse::class);
$countryChoice = [];
foreach ($response->getResponse() as $country) {
/** @var \lucasaba\RapidAPI\Entity\Country $country */
$countryChoice[] = $country->getName();
}
$question = new ChoiceQuestion('Select a country', $countryChoice);
$country = $helper->ask($input, $output, $question);

// Select league
$leaguesRequest = new LeaguesRequest();
$leaguesRequest->withCountry($country)->withSeason(self::SEASON)->withType(League::LEAGUE_TYPE_LEAGUE);
$response = $client->get($leaguesRequest, LeaguesResponse::class);
$leagueChoice = [];
foreach ($response->getResponse() as $league) {
/** @var \lucasaba\RapidAPI\Response\LeagueResponseElement $league */
$leagueChoice[$league->getLeague()->getId()] = $league->getLeague()->getName();
}
$question = new ChoiceQuestion('Select a league', $leagueChoice);
$league = $helper->ask($input, $output, $question);
$leagueId = array_search($league, $leagueChoice);
if (false === $leagueId) {
$output->writeln('Unable to get league id for ' . $league);
return Command::FAILURE;
}

// Get rounds
$roundsRequest = new RoundsRequest($leagueId, self::SEASON);
$response = $client->get($roundsRequest, RoundsResponse::class);
$rounds = $response->getResponse(); // Array of rounds names

// Get fixtures
foreach ($rounds as $round) {
$output->writeln($round);
$fixtureRequest = new FixturesRequest(self::SEASON);
$fixtureRequest->withRound($round)
->withLeague($leagueId);
$fixturesResponse = $client->get($fixtureRequest, FixturesResponse::class);
$this->displayMatches($fixturesResponse->getResponse(), $output);
}

return Command::SUCCESS;
}

/**
* @param \lucasaba\RapidAPI\Response\FixturesResponseElement[] $fixtures
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
private function displayMatches(array $fixtures, OutputInterface $output)
{
foreach ($fixtures as $fixture) {
$output->writeln(
sprintf('%s - %s - %s: %s-%s',
$fixture->getFixture()->getDate()->format('d/m/Y'),
$fixture->getTeams()->getHome()->getName(),
$fixture->getTeams()->getAway()->getName(),
$fixture->getGoals()->getHome() ?? 'TBD',
$fixture->getGoals()->getAway() ?? 'TBD'
)
);
}
}
}

0 comments on commit c480f85

Please sign in to comment.