Skip to content

Commit

Permalink
Adds Fixtures information (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasaba committed Sep 20, 2021
1 parent 545a7ad commit 81e87c1
Show file tree
Hide file tree
Showing 15 changed files with 4,475 additions and 24 deletions.
2 changes: 2 additions & 0 deletions bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require __DIR__.'/../vendor/autoload.php';

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

$application->run();
5 changes: 1 addition & 4 deletions src/Command/CountriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$response = $client->get($request, CountriesResponse::class);
var_dump($response);
/*$content = file_get_contents(__DIR__ . '/../../risultati.json');
$result = $serializer->deserialize($content, 'lucasaba\RapidAPI\Response\CountriesResponse', 'json');
var_dump($result);*/
$output->write(sprintf('There are %s results', $response->getResults()));

return Command::SUCCESS;
}
Expand Down
51 changes: 51 additions & 0 deletions src/Command/FixturesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php


namespace lucasaba\RapidAPI\Command;


use lucasaba\RapidAPI\Request\FixturesRequest;
use lucasaba\RapidAPI\Response\FixturesResponse;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class FixturesCommand extends AbstractApiCommand
{
protected static $defaultName = 'app:get-fixtures';

protected function configure(): void
{
$this->setDescription('Gets teams information')
->addArgument('token', InputArgument::REQUIRED, 'RapidAPI token')
->addOption('league', null, InputOption::VALUE_OPTIONAL, 'The league id of the league')
->addOption('season', null, InputOption::VALUE_REQUIRED, 'The season of the fixtures')
->addOption('from', null, InputOption::VALUE_OPTIONAL, 'Date from')
->addOption('to', null, InputOption::VALUE_OPTIONAL, 'Date to');
}

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

$request = new FixturesRequest($input->getOption('season'));
if ($input->getOption('league')) {
$request->withLeague($input->getOption('league'));
}
if ($input->getOption('from')) {
$request->withDateFrom($input->getOption('from'));
}

if ($input->getOption('to')) {
$request->withDateTo($input->getOption('to'));
}

$response = $client->get($request, FixturesResponse::class, true);

$output->write(sprintf('There are %s results', $response->getResults()));

return Command::SUCCESS;
}
}
5 changes: 1 addition & 4 deletions src/Command/LeaguesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$response = $client->get($request, LeaguesResponse::class, true);
var_dump($response);
/*$content = file_get_contents(__DIR__ . '/../../risultati.json');
$result = $serializer->deserialize($content, 'lucasaba\RapidAPI\Response\CountriesResponse', 'json');
var_dump($result);*/
$output->write(sprintf('There are %s results', $response->getResults()));

return Command::SUCCESS;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Command/TeamsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$response = $client->get($request, TeamsResponse::class, true);
var_dump($response);
/*$content = file_get_contents(__DIR__ . '/../../risultati.json');
$result = $serializer->deserialize($content, 'lucasaba\RapidAPI\Response\CountriesResponse', 'json');
var_dump($result);*/
$output->write(sprintf('There are %s results', $response->getResults()));

return Command::SUCCESS;
}
Expand Down
24 changes: 21 additions & 3 deletions src/Entity/League.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class League

private int $id;
private string $name;
private string $type;
private ?string $type;
private string $logo;
private ?int $season;
private ?string $round;

/**
* @return int
Expand All @@ -32,9 +34,9 @@ public function getName(): string
}

/**
* @return string
* @return string|null
*/
public function getType(): string
public function getType(): ?string
{
return $this->type;
}
Expand All @@ -46,4 +48,20 @@ public function getLogo(): string
{
return $this->logo;
}

/**
* @return int|null
*/
public function getSeason(): ?int
{
return $this->season;
}

/**
* @return string|null
*/
public function getRound(): ?string
{
return $this->round;
}
}
10 changes: 5 additions & 5 deletions src/Entity/MatchGoals.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

class MatchGoals
{
private int $home;
private int $away;
private ?int $home;
private ?int $away;

/**
* @return int
* @return int|null
*/
public function getHome(): int
public function getHome(): ?int
{
return $this->home;
}

/**
* @return int
*/
public function getAway(): int
public function getAway(): ?int
{
return $this->away;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Entity/MatchTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MatchTeam
private int $id;
private string $name;
private string $logo;
private bool $winner;
private ?bool $winner;

/**
* @return int
Expand All @@ -36,9 +36,9 @@ public function getLogo(): string
}

/**
* @return bool
* @return bool|null
*/
public function isWinner(): bool
public function isWinner(): ?bool
{
return $this->winner;
}
Expand Down
26 changes: 25 additions & 1 deletion src/Entity/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,29 @@ class Status
{
private string $long;
private string $short;
private int $elapsed;
private ?int $elapsed;

/**
* @return string
*/
public function getLong(): string
{
return $this->long;
}

/**
* @return string
*/
public function getShort(): string
{
return $this->short;
}

/**
* @return int|null
*/
public function getElapsed(): ?int
{
return $this->elapsed;
}
}
109 changes: 109 additions & 0 deletions src/Request/FixturesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php


namespace lucasaba\RapidAPI\Request;


class FixturesRequest implements IRequest
{
const ENDPOINT = '/fixtures';

private ?int $id = null; // The id of the fixture
private ?string $date = null; // string YYYY-MM-DD
private ?int $league = null; // The id of the league
private int $season; // The season of the league
private ?int $team = null; // The id of the team
private ?string $from = null; // string YYYY-MM-DD
private ?string $to = null; // string YYYY-MM-DD
private ?string $round = null; // The round of the fixture

public function __construct(int $season = null)
{
$this->season = $season ?? date('Y');
}

public function withId(int $id): FixturesRequest
{
$this->id = $id;
return $this;
}

public function withDate(string $date): FixturesRequest
{
$this->date = $date;
return $this;
}

public function withLeague(int $leagueId): FixturesRequest
{
$this->league = $leagueId;
return $this;
}

public function withTeam(int $teamId): FixturesRequest
{
$this->team = $teamId;
return $this;
}

public function withDateFrom(string $dateFrom): FixturesRequest
{
$this->from = $dateFrom;
return $this;
}

public function withDateTo(string $dateTo): FixturesRequest
{
$this->to = $dateTo;
return $this;
}

public function withRound(string $round): FixturesRequest
{
$this->round = $round;
return $this;
}

public function getQuery(): array
{
$query = [];
if ($this->id) {
$query['id'] = $this->id;
}

if ($this->date) {
$query['date'] = $this->date;
}

if ($this->league) {
$query['league'] = $this->league;
}

if ($this->season) {
$query['season'] = $this->season;
}

if ($this->team) {
$query['team'] = $this->team;
}

if ($this->from) {
$query['from'] = $this->from;
}

if ($this->to) {
$query['to'] = $this->to;
}

if ($this->round) {
$query['round'] = $this->round;
}

return $query;
}

public function getEndpoint(): string
{
return self::ENDPOINT;
}
}
41 changes: 41 additions & 0 deletions src/Request/Tests/FixturesRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace lucasaba\RapidAPI\Request\Tests;

use lucasaba\RapidAPI\Request\FixturesRequest;
use PHPUnit\Framework\TestCase;

class FixturesRequestTest extends TestCase
{
public function testAddsAll(): void
{
$request = new FixturesRequest(2021);
$request->withId(1)
->withLeague(2)
->withTeam(3)
->withDate('2021-01-01')
->withDateFrom('2021-01-01')
->withDateTo('2021-01-01')
->withRound('Round 1');

$this->assertEquals(
[
'id' => 1,
'season' => 2021,
'league' => 2,
'team' => 3,
'date' => '2021-01-01',
'from' => '2021-01-01',
'to' => '2021-01-01',
'round' => 'Round 1',
],
$request->getQuery()
);
}

public function testEndpointIsReturned()
{
$request = new FixturesRequest();
$this->assertEquals('/fixtures', $request->getEndpoint());
}
}
Loading

0 comments on commit 81e87c1

Please sign in to comment.