public function getAreaForPostCode(PostcodeParser $postcodeParser) { global $CONFIG; if ($postcodeParser->isValid()) { $memcachedConnection = null; $areaRepo = new AreaRepository(); if ($CONFIG->memcachedServer) { $memcachedConnection = new \Memcached(); $memcachedConnection->addServer($CONFIG->memcachedServer, $CONFIG->memcachedPort); $url = $memcachedConnection->get($postcodeParser->getCanonical()); if ($url) { $urlBits = explode("/", $url); $urlBitsBits = explode("-", $urlBits[2]); $area = $areaRepo->loadBySlug($this->app['currentSite'], $urlBitsBits[0]); if ($area) { return $area; } } } $url = "http://mapit.mysociety.org/postcode/" . urlencode($postcodeParser->getCanonical()); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Meet Your Next MP'); $data = curl_exec($ch); $response = curl_getinfo($ch); curl_close($ch); if ($response['http_code'] != 200) { return; } $dataObj = json_decode($data); $mapItId = $dataObj->shortcuts->WMC; if (!$mapItId) { return; } $repo = new AreaMapItInfoRepository(); $areaMapIdInfo = $repo->getByMapItID($mapItId); if (!$areaMapIdInfo) { return; } $area = $areaRepo->loadById($areaMapIdInfo->getAreaId()); if (!$area) { return; } if ($memcachedConnection) { $memcachedConnection->set($postcodeParser->getCanonical(), '/area/' . $area->getSlugForUrl(), 60 * 60 * 24 * 30); } return $area; } }
function postcode(Request $request, Application $app) { global $CONFIG; $postcode = $request->query->get("postcode"); $urlExtra = ''; if ($request->query->get("page") == 'candidates') { $urlExtra = '/humans'; } $postcodeParser = new PostcodeParser($postcode); if (!$postcodeParser->isValid()) { $this->logPostcodeSearch($app, $postcode, false, array("Parser says not valid")); $app['flashmessages']->addMessage('That does not look like a valid postcode! Did you enter a full postcode?'); return $app->redirect('/'); } $memcachedConnection = null; if ($CONFIG->memcachedServer) { $memcachedConnection = new \Memcached(); $memcachedConnection->addServer($app['config']->memcachedServer, $app['config']->memcachedPort); $url = $memcachedConnection->get($postcodeParser->getCanonical()); if ($url) { $this->logPostcodeSearch($app, $postcode, true, array("From Cache")); return $app->redirect($url . $urlExtra); } } $url = "http://mapit.mysociety.org/postcode/" . urlencode($postcode); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Meet Your Next MP'); $data = curl_exec($ch); $response = curl_getinfo($ch); curl_close($ch); if ($response['http_code'] != 200) { $this->logPostcodeSearch($app, $postcode, false, array("not 200 response from API")); $app['flashmessages']->addMessage('Sorry, we had a problem with the postcode API! Did you enter a full postcode?'); return $app->redirect('/'); } $dataObj = json_decode($data); $mapItId = $dataObj->shortcuts->WMC; if (!$mapItId) { $this->logPostcodeSearch($app, $postcode, false, array("No mapidid")); $app['flashmessages']->addMessage('Sorry, we did not find your seat in the data!'); return $app->redirect('/'); } $repo = new AreaMapItInfoRepository(); $areaMapIdInfo = $repo->getByMapItID($mapItId); if (!$areaMapIdInfo) { $this->logPostcodeSearch($app, $postcode, false, array("no areaid")); $app['flashmessages']->addMessage('Sorry, we did not find your seat in the database!'); return $app->redirect('/'); } $repo = new AreaRepository(); $area = $repo->loadById($areaMapIdInfo->getAreaId()); if (!$area) { $this->logPostcodeSearch($app, $postcode, false, array("no area")); $app['flashmessages']->addMessage('Sorry, we did not find your area in the database!'); return $app->redirect('/'); } if ($memcachedConnection) { $memcachedConnection->set($postcodeParser->getCanonical(), '/area/' . $area->getSlugForUrl(), 60 * 60 * 24 * 30); } $this->logPostcodeSearch($app, $postcode, true, array("Cached")); return $app->redirect('/area/' . $area->getSlugForUrl() . $urlExtra); }