Skip to content

Commit 8d4c4da

Browse files
committed
Merge branch 'release'
2 parents f9d8ba0 + 8dde268 commit 8d4c4da

7 files changed

Lines changed: 94 additions & 26 deletions

File tree

app/data/CrimeViewDataProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,20 @@ public function getRouteData(string $from, string $to, $countDistribution): arra
1717
{
1818
$result = [];
1919
$result['from'] = $this->cityDataProvider->getCityByName($from);
20+
if (!$result['from']) {
21+
return array();
22+
}
23+
2024
$result['to'] = $this->cityDataProvider->getCityByName($to);
25+
if (!$result['to']) {
26+
return array();
27+
}
2128

2229
$countiesOnRoute = $this->countyDataProvider->getCountiesOnRoute($result['from'], $result['to']);
30+
if (!$countiesOnRoute) {
31+
return array();
32+
}
33+
2334
$this->crimeDataProvider->fillCountiesWithCrimeStats($countiesOnRoute, $countDistribution);
2435

2536
$result['counties'] = $countiesOnRoute;

app/data/interfaces/ICityDataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ interface ICityDataProvider
99
*
1010
* @return City The city model
1111
*/
12-
public function getCityByName(string $name): City;
12+
public function getCityByName(string $name): ?City;
1313
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* MapQuestApiProvider uses MapQuestApi to retrieve city information.
5+
*/
6+
class MapQuestApiDataProvider implements ICityDataProvider
7+
{
8+
public function getCityByName(string $name): ?City
9+
{
10+
$clean_name = trim(explode(",", $name)[0]);
11+
$url = "http://www.mapquestapi.com/geocoding/v1/address?key=&location={$clean_name}";
12+
13+
$body = file_get_contents($url);
14+
$json = json_decode($body);
15+
$status = $json->info->statuscode;
16+
17+
if ($status != 0) {
18+
throw new InvalidArgumentException("City not found: $name");
19+
}
20+
21+
$countryCode = $json->results[0]->locations[0]->adminArea1;
22+
23+
if ($countryCode != 'DE') {
24+
throw new InvalidArgumentException("City not found: $name");
25+
}
26+
27+
return new City(
28+
$json->results[0]->locations[0]->adminArea5,
29+
$json->results[0]->locations[0]->adminArea5Type,
30+
$json->results[0]->locations[0]->latLng->lat,
31+
$json->results[0]->locations[0]->latLng->lng
32+
);
33+
}
34+
}

app/data/providers/OriginDataProvider.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ public function getCountiesOnRoute(City $from, City $to): array
7272
$fromLatAdjusted = $fromLat + 0.0000001;
7373
$pathToGeoJson = "https://public.opendatasoft.com/explore/dataset/landkreise-in-germany/download?format=geojson&geofilter.polygon=(" . $fromLat . "," . $fromLon . "),(" . $toLat . "," . $toLon . "),(" . $fromLatAdjusted . "," . $fromLon . "),(" . $fromLat . "," . $fromLon . ")";
7474

75-
$geoJson = file_get_contents($pathToGeoJson);
75+
try {
76+
$geoJson = file_get_contents($pathToGeoJson);
77+
} catch (Exception $e) {
78+
return array();
79+
}
80+
7681
$raw = json_decode($geoJson, true);
7782
$features = $raw["features"];
7883

@@ -92,12 +97,12 @@ public function getCountiesOnRoute(City $from, City $to): array
9297
return $counties;
9398
}
9499

95-
public function getCityByName(string $name): City
100+
public function getCityByName(string $name): ?City
96101
{
97102
$json = $this->getNominatimJson($name);
98103

99-
if (sizeof($json) == 0) {
100-
throw new InvalidArgumentException("City not found: $name");
104+
if (!$json) {
105+
return null;
101106
}
102107

103108
$name = $json[0]["display_name"];
@@ -128,8 +133,18 @@ private function getNominatimJson($cityName)
128133
];
129134
$context = stream_context_create($opts);
130135

131-
$body = file_get_contents($url, false, $context);
132-
return json_decode($body, true);
136+
try {
137+
$body = file_get_contents($url, false, $context);
138+
} catch (Exception $e) {
139+
return null;
140+
}
141+
142+
$json = json_decode($body, true);
143+
if (sizeof($json) == 0) {
144+
throw new InvalidArgumentException("City not found: $cityName");
145+
}
146+
147+
return $json;
133148
}
134149

135150
private function csvNumberToFloat(string $number): float

app/data/providers/SampleDataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getCountiesOnRoute(City $from, City $to): array
6666
return $counties;
6767
}
6868

69-
public function getCityByName(string $name): City
69+
public function getCityByName(string $name): ?City
7070
{
7171
switch (strtolower($name)) {
7272
case "regensburg":

app/views/home.view.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
<body>
99
<?php require __DIR__ . '/shared/navbar.view.php'; ?>
10-
<div class='container'>
11-
<div class='row align-items-center align-items-xl-center mt-5 justify-content-between'>
10+
<div class='container mt-5'>
11+
<div class='row align-items-center align-items-xl-center justify-content-between'>
1212
<div class='col-sm-6 mb-2'>
1313
<h1>CrimeView </h1>
1414
<small>
@@ -116,4 +116,4 @@ function showSearch() {
116116
</script>
117117
</body>
118118

119-
</html>
119+
</html>

tests/data/DataProviderTest.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,30 @@ public function testCrimeDataProvider(CrimeViewDataProvider $provider, array $ci
1111
{
1212
foreach ($cites as $route) {
1313
$data = $provider->getRouteData($route["from"], $route["to"], 3);
14-
1514
$this->assertIsArray($data);
16-
$this->assertInstanceOf('City', $data['from']);
17-
$this->assertInstanceOf('City', $data['to']);
18-
$this->assertIsArray($data['counties']);
1915

20-
foreach ($data['counties'] as $county) {
21-
$this->assertInstanceOf('County', $county);
16+
if (sizeof($data) > 0) {
17+
$this->assertInstanceOf('City', $data['from']);
18+
$this->assertInstanceOf('City', $data['to']);
19+
$this->assertIsArray($data['counties']);
20+
21+
foreach ($data['counties'] as $county) {
22+
$this->assertInstanceOf('County', $county);
2223

23-
$id = $county->getId();
24-
$this->assertIsString($id);
24+
$id = $county->getId();
25+
$this->assertIsString($id);
2526

26-
$crimeStatsArray = $county->getCrimeStats();
27-
foreach ($crimeStatsArray as $crimeStats) {
28-
$this->assertInstanceOf('CrimeStats', $crimeStats);
27+
$crimeStatsArray = $county->getCrimeStats();
28+
foreach ($crimeStatsArray as $crimeStats) {
29+
$this->assertInstanceOf('CrimeStats', $crimeStats);
2930

30-
$crimeRate = $crimeStats->getRate();
31-
$this->assertEqualsWithDelta(0.5, $crimeRate, 0.5);
31+
$crimeRate = $crimeStats->getRate();
32+
$this->assertEqualsWithDelta(0.5, $crimeRate, 0.5);
33+
}
3234
}
33-
}
3435

35-
$this->assertEqualsWithDelta(0.5, $data['averageCrimeRate'], 0.5);
36+
$this->assertEqualsWithDelta(0.5, $data['averageCrimeRate'], 0.5);
37+
}
3638
}
3739
}
3840

@@ -55,6 +57,11 @@ public function testErrorCrimeDataProvider(CrimeViewDataProvider $provider, arra
5557
foreach ($cites as $route) {
5658
$this->expectException(InvalidArgumentException::class);
5759
$data = $provider->getRouteData($route["from"], $route["to"], 3);
60+
$this->assertIsArray($data);
61+
62+
if (!$data) {
63+
throw new InvalidArgumentException();
64+
}
5865
}
5966
}
6067

@@ -75,6 +82,7 @@ public function dataProvider()
7582
array($originDataProvider, $dataSet), array($localDataProvider, $dataSet), array($sampleDataProvider, $dataSet)
7683
);
7784
}
85+
7886

7987

8088
public function errorDataProvider()

0 commit comments

Comments
 (0)