From d19b9a09b75bc682585a5bddfc9e6650dd7fb078 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Tue, 13 Feb 2018 14:22:50 +1300 Subject: [PATCH] Upgrade to namespaces, SilverStripe 4 --- _config/config.yml | 6 ++-- composer.json | 28 +++++++++++++------ .../CheckoutStepAddressLocationFallback.php | 3 +- {code => src/Extensions}/AddressGeocoding.php | 12 +++++--- {code => src/Extensions}/GeocodedUserInfo.php | 21 ++++++++++---- tests/GeocodingTest.php | 16 ++++++----- tests/addresses.yml | 4 +-- 7 files changed, 58 insertions(+), 32 deletions(-) rename code/CheckoutStep_AddressLocationFallback.php => src/Checkout/CheckoutStepAddressLocationFallback.php (95%) rename {code => src/Extensions}/AddressGeocoding.php (96%) rename {code => src/Extensions}/GeocodedUserInfo.php (91%) diff --git a/_config/config.yml b/_config/config.yml index 44e3ee2..446bdd2 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -2,9 +2,9 @@ Name: shopgeocodingconfig After: 'silvershop/*' --- -Address: +SilverShop\Model\Address: extensions: - - AddressGeocoding + - SilverShop\Geocoding\Extensions\AddressGeocoding enable_geocoding: true mapdefaults: latitude: -41.292694 @@ -12,5 +12,5 @@ Address: zoom: 5 Page: extensions: - - GeocodedUserInfo + - SilverShop\Geocoding\Extensions\GeocodedUserInfo geocode_visitor_ip: true diff --git a/composer.json b/composer.json index cc2d9e2..3681a7d 100644 --- a/composer.json +++ b/composer.json @@ -1,15 +1,27 @@ { "name": "silvershop/geocoding", "description": "Geocoding support for SilverShop.", - "type": "silverstripe-module", + "type": "silverstripe-vendormodule", "keywords": ["silverstripe", "ecommerce", "shop", "geocoding", "address", "ip"], - "license": "BSD2", - "extra": { - "installer-name": "silvershop-geocoding" - }, "require": { - "silvershop/core": "*", - "betterbrief/silverstripe-googlemapfield": "~1.2", + "silvershop/core": "^3", + "betterbrief/silverstripe-googlemapfield": "^2", "willdurand/geocoder": "~2.8.0" - } + }, + "license": "BSD-3-Clause", + "authors": [ { + "name": "SilverShop Contributors", + "homepage": "https://github.com/silvershop/silvershop-stock/graphs/contributors" + }], + "extra": { + "branch-alias": { + "dev-master": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "SilverShop\\Geocoding\\": "src/", + "SilverShop\\Geocoding\\Tests\\": "tests/unit/" + } + } } diff --git a/code/CheckoutStep_AddressLocationFallback.php b/src/Checkout/CheckoutStepAddressLocationFallback.php similarity index 95% rename from code/CheckoutStep_AddressLocationFallback.php rename to src/Checkout/CheckoutStepAddressLocationFallback.php index 0b7d279..6cad598 100644 --- a/code/CheckoutStep_AddressLocationFallback.php +++ b/src/Checkout/CheckoutStepAddressLocationFallback.php @@ -3,9 +3,8 @@ /** * A fallback to use if geocoding fails to locate address. * - * @package silvershop-geocoding */ -class CheckoutStep_AddressLocationFallback extends CheckoutStep +class CheckoutStepAddressLocationFallback extends CheckoutStep { private static $allowed_actions = array( diff --git a/code/AddressGeocoding.php b/src/Extensions/AddressGeocoding.php similarity index 96% rename from code/AddressGeocoding.php rename to src/Extensions/AddressGeocoding.php index 6367ab5..f75aabb 100644 --- a/code/AddressGeocoding.php +++ b/src/Extensions/AddressGeocoding.php @@ -1,8 +1,12 @@ owner->Latitude = $geocoded->getLatitude(); $this->owner->Longitude = $geocoded->getLongitude(); } catch (Exception $e) { - SS_Log::log($e, SS_Log::ERR); + } } diff --git a/code/GeocodedUserInfo.php b/src/Extensions/GeocodedUserInfo.php similarity index 91% rename from code/GeocodedUserInfo.php rename to src/Extensions/GeocodedUserInfo.php index 0bc4ac9..d0e8189 100644 --- a/code/GeocodedUserInfo.php +++ b/src/Extensions/GeocodedUserInfo.php @@ -1,8 +1,12 @@ getAddress(); $autocode = Page::config()->geocode_visitor_ip; + if ((!$location && $autocode) || Controller::curr()->getRequest()->getVar('relocateuser')) { ShopUserInfo::singleton()->setAddress(new Address($this->findLocation())); } @@ -18,11 +23,11 @@ public function contentcontrollerInit() protected function findLocation() { $ip = Controller::curr()->getRequest()->getIP(); - //TODO:what to do if there is no ip? - //rewrite localhost to a testing ip + if (in_array($ip, array( '127.0.0.1', '::1' ))) { $ip = Address::config()->test_ip; } + return $this->addressFromIP($ip); } @@ -30,13 +35,15 @@ protected function addressFromIP($ip) { $geocoder = AddressGeocoding::get_geocoder(); $geodata = array(); + try { if ($ip) { $geodata = $geocoder->geocode($ip)->toArray(); } } catch (Exception $e) { - SS_Log::log($e, SS_Log::ERR); + } + $geodata = array_filter($geodata); $datamap = array( 'Country' => 'countryCode', @@ -46,7 +53,9 @@ protected function addressFromIP($ip) 'Latitude' => 'latitude', 'Longitude' => 'longitude' ); + $mappeddata = array(); + foreach ($datamap as $addressfield => $geofield) { if (is_array($geofield)) { if ($data = implode(" ", array_intersect_key($geodata, array_combine($geofield, $geofield)))) { diff --git a/tests/GeocodingTest.php b/tests/GeocodingTest.php index c353ff6..90e3bde 100644 --- a/tests/GeocodingTest.php +++ b/tests/GeocodingTest.php @@ -1,24 +1,26 @@ objFromFixture("Address", "address1"); + $address = $this->objFromFixture(Address::class, "address1"); $this->assertEquals(174.77908, $address->Longitude); $this->assertEquals(-41.292915, $address->Latitude); } public function testAddressDistanceTo() { - $from = $this->objFromFixture("Address", "address1"); - $to = $this->objFromFixture("Address", "address2"); + $from = $this->objFromFixture(Address::class, "address1"); + $to = $this->objFromFixture(Address::class, "address2"); $this->assertEquals(0, $from->distanceTo($from)); $this->assertEquals(0, $to->distanceTo($to)); $this->assertEquals(494.42414833321, $from->distanceTo($to)); diff --git a/tests/addresses.yml b/tests/addresses.yml index 2578935..0981319 100644 --- a/tests/addresses.yml +++ b/tests/addresses.yml @@ -1,4 +1,4 @@ -Address: +SilverShop\Model\Address: address1: Country: NZ State: Wellington @@ -17,4 +17,4 @@ Address: PostalCode: 1010 Company: SilverStripe LTD Longitude: 174.76705230 - Latitude: -36.84646160 \ No newline at end of file + Latitude: -36.84646160