/** * Main point of entry for the Schools endpoint. * * @param Request $req The request to be handled. * @param Application $app The Silex Application used to handle the request. * * @return array An array of schools in Hampton, VABeach, Norfolk, Portsmouth, Chesapeake, and Suffolk. */ public function main(Request $req, Application $app) { $requestedCities = ['Hampton', 'Norfolk', 'Virginia Beach', 'Portsmouth', 'Chesapeake', 'Suffolk']; if ($req->query->has('cities')) { $requestedCities = explode(',', $req->query->get('cities')); } $groupByField = $req->query->get('groupBy'); $averageField = $req->query->get('averageField'); $resultSet = []; foreach ($requestedCities as $requestedCity) { $url = self::formatRequestUrl($requestedCity); $response = $app['guzzle']->get($url, []); $schools = self::convertToJson($response->getBody()); $resultSet[] = self::filterResultsByCity($schools, $requestedCity); } if (!empty($averageField) && !empty($groupByField)) { $resultSet = self::calculateAverages($resultSet, $averageField, $groupByField); } $herculesResponse = new HerculesResponse('/schools', 200, $resultSet); // The frontend expects a JSONP format, to do this the response must be wrapped in a callback. return $_GET['callback'] . '(' . $herculesResponse->to_json() . ')'; }
/** * Verifies behaviour when validating a response fails while converting to json. * * @return void * * expectedException \InvalidResponseException */ public function testToJson_missingData() { $actual = new HerculesResponse(null, null, null, null); $actual->to_json(); }