diff --git a/bin/console.php b/bin/console.php index 3da21bc..4eb6a54 100755 --- a/bin/console.php +++ b/bin/console.php @@ -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; @@ -15,5 +16,6 @@ $application->add(new LeaguesCommand()); $application->add(new TeamsCommand()); $application->add(new FixturesCommand()); +$application->add(new GetLeagueMatchesCommand()); $application->run(); \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 09d655a..e1405a5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -30,6 +30,7 @@ src/*/Tests + src/Command src/autoload.php diff --git a/src/Command/GetLeagueMatchesCommand.php b/src/Command/GetLeagueMatchesCommand.php new file mode 100644 index 0000000..d3180ce --- /dev/null +++ b/src/Command/GetLeagueMatchesCommand.php @@ -0,0 +1,102 @@ +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' + ) + ); + } + } +} \ No newline at end of file