Skip to content

Commit

Permalink
feature: add region and subregion to query airport response
Browse files Browse the repository at this point in the history
  • Loading branch information
amvid committed Jul 18, 2024
1 parent 849c4fe commit 5df249f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
23 changes: 18 additions & 5 deletions src/Airport/Action/Query/QueryAirportsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
{
public function __construct(
private AirportRepositoryInterface $airportRepository,
) {
}
) {}

public function run(QueryAirportsActionRequest $request): QueryAirportsActionResponse
{
Expand All @@ -28,15 +27,23 @@ public function run(QueryAirportsActionRequest $request): QueryAirportsActionRes
/** @var array<QueryChildrenAirportResponse> $res */
$res = [];
$map = [];
$regions = [];
$subregions = [];

foreach ($airports as $airport) {
$city = $airport->getCity();
$country = $city->getCountry();
$subregion = $country->getSubregion();
$region = $subregion->getRegion();
$cityIata = $city->getIata() ?? $airport->getIata();

$regions[$country->getTitle()] = $region->getTitle();
$subregions[$country->getTitle()] = $subregion->getTitle();

if (!isset($map[$country->getTitle()])) {
$map[$country->getTitle()] = [];
}

if (!isset($map[$country->getTitle()][$cityIata])) {
$map[$country->getTitle()][$cityIata] = [];
}
Expand All @@ -53,7 +60,9 @@ public function run(QueryAirportsActionRequest $request): QueryAirportsActionRes
$res[] = new QueryChildrenAirportResponse(
$cityTitle . ' (Any)',
$cityIata,
$countryTitle
$countryTitle,
$regions[$countryTitle],
$subregions[$countryTitle],
);

$parentIndex = count($res) - 1;
Expand All @@ -62,15 +71,19 @@ public function run(QueryAirportsActionRequest $request): QueryAirportsActionRes
$res[$parentIndex]->children[] = new QueryAirportResponse(
$airport->getTitle(),
$airport->getIata(),
$countryTitle
$countryTitle,
$regions[$countryTitle],
$subregions[$countryTitle],
);
}
} else {
$airport = $airports[0];
$res[] = new QueryAirportResponse(
$airport->getTitle(),
$airport->getIata(),
$countryTitle
$countryTitle,
$regions[$countryTitle],
$subregions[$countryTitle],
);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Airport/Controller/Response/QueryAirportResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ class QueryAirportResponse
public string $title;
public string $iata;
public string $country;
public string $region;
public string $subregion;

public function __construct(string $title, string $iata, string $country)
public function __construct(string $title, string $iata, string $country, string $region, string $subregion)
{

$this->title = $title;
$this->iata = $iata;
$this->country = $country;
$this->region = $region;
$this->subregion = $subregion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ class QueryChildrenAirportResponse
public string $title;
public string $iata;
public string $country;
public string $subregion;
public string $region;

/** @var array<QueryAirportResponse> */
public array $children = [];

public function __construct(string $title, string $iata, string $country, array $children = [])
public function __construct(string $title, string $iata, string $country, string $region, string $subregion, array $children = [])
{
$this->title = $title;
$this->iata = $iata;
$this->country = $country;
$this->subregion = $subregion;
$this->region = $region;
$this->children = $children;
}
}
8 changes: 7 additions & 1 deletion tests/Unit/Airport/Action/Query/QueryAirportsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ public function testShouldReturnAirports(): void
$action = new QueryAirportsAction($this->airportRepository);
$response = $action->run($this->request);

$country = $airport->getCity()->getCountry();
$subregion = $country->getSubregion();
$region = $subregion->getRegion();

$this->assertCount(1, $response->airports);
$this->assertEquals($airport->getTitle(), $response->airports[0]->title);
$this->assertEquals($airport->getIata(), $response->airports[0]->iata);
$this->assertEquals($airport->getCity()->getCountry()->getTitle(), $response->airports[0]->country);
$this->assertEquals($country->getTitle(), $response->airports[0]->country);
$this->assertEquals($subregion->getTitle(), $response->airports[0]->subregion);
$this->assertEquals($region->getTitle(), $response->airports[0]->region);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ public function testValidInstantiation(): void
{
$airport = AirportDummy::get();

$country = $airport->getCity()->getCountry();
$subregion = $country->getSubregion();
$region = $subregion->getRegion();

$actual = new QueryAirportResponse(
$airport->getTitle(),
$airport->getIata(),
$airport->getCity()->getCountry()->getTitle()
$airport->getCity()->getCountry()->getTitle(),
$region->getTitle(),
$subregion->getTitle(),
);

$this->assertEquals($airport->getTitle(), $actual->title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ class QueryChildrenAirportResponseTest extends TestCase
public function testValidInstantiation(): void
{
$airport = AirportDummy::get();
$country = $airport->getCity()->getCountry();
$subregion = $country->getSubregion();
$region = $subregion->getRegion();

$children = new QueryAirportResponse(
$airport->getTitle(),
$airport->getIata(),
$airport->getCity()->getCountry()->getTitle()
$airport->getCity()->getCountry()->getTitle(),
$region->getTitle(),
$subregion->getTitle(),
);

$actual = new QueryChildrenAirportResponse(
$airport->getTitle(),
$airport->getIata(),
$airport->getCity()->getCountry()->getTitle(),
$region->getTitle(),
$subregion->getTitle(),
[$children]
);

Expand Down

0 comments on commit 5df249f

Please sign in to comment.