Skip to content

Commit

Permalink
Upgrade to namespaces, SilverStripe 4
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Feb 13, 2018
1 parent f4d50fd commit d19b9a0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 32 deletions.
6 changes: 3 additions & 3 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Name: shopgeocodingconfig
After: 'silvershop/*'
---
Address:
SilverShop\Model\Address:
extensions:
- AddressGeocoding
- SilverShop\Geocoding\Extensions\AddressGeocoding
enable_geocoding: true
mapdefaults:
latitude: -41.292694
longitude: 174.7788669
zoom: 5
Page:
extensions:
- GeocodedUserInfo
- SilverShop\Geocoding\Extensions\GeocodedUserInfo
geocode_visitor_ip: true
28 changes: 20 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

/**
* @package silvershop-geocoding
*/
namespace SilverShop\Geocoding;

use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataExtension;
use SilverShop\Model\Address;
use Exception;

class AddressGeocoding extends DataExtension
{

Expand Down Expand Up @@ -75,7 +79,7 @@ public function geocodeAddress()
$this->owner->Latitude = $geocoded->getLatitude();
$this->owner->Longitude = $geocoded->getLongitude();
} catch (Exception $e) {
SS_Log::log($e, SS_Log::ERR);

This comment has been minimized.

Copy link
@jedateach

jedateach Oct 2, 2020

Contributor

I'm guessing this was removed because it was noisy - it certainly is for me for one of my sites.

But I'm thinking what about re-introducing it, making it configurable, and defaulting it to SS_Log::Notice or SS_Log::Info?

This comment has been minimized.

Copy link
@wilr

wilr Oct 2, 2020

Author Contributor

@jedateach Yep a notice would be fine. In SS4 would look something like Injector::inst()->create(LoggerInterface::class)->info($e);

This comment has been minimized.

Copy link
@jedateach

jedateach Oct 2, 2020

Contributor

Created #16 to note this


}
}

Expand Down
21 changes: 15 additions & 6 deletions code/GeocodedUserInfo.php → src/Extensions/GeocodedUserInfo.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?php

/**
* @package silvershop-geocoding
*/
namespace SilverShop\Geocoding\Extensions;

use SilverStripe\Control\Controller;
use SilverStripe\ORM\DataExtension;
use SilverShop\Model\Address;
use Page;

class GeocodedUserInfo extends DataExtension
{

public function contentcontrollerInit()
{
$location = ShopUserInfo::singleton()->getAddress();
$autocode = Page::config()->geocode_visitor_ip;

if ((!$location && $autocode) || Controller::curr()->getRequest()->getVar('relocateuser')) {
ShopUserInfo::singleton()->setAddress(new Address($this->findLocation()));
}
Expand All @@ -18,25 +23,27 @@ 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);
}

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',
Expand All @@ -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)))) {
Expand Down
16 changes: 9 additions & 7 deletions tests/GeocodingTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<?php

/**
* @package silvershop-geocoding
*/
namespace SilverShop\Geocoding\Tests;

use SilverStripe\Dev\SapphireTest;
use SilverShop\Model\Address;

class GeocodingTest extends SapphireTest
{

public static $fixture_file = 'silvershop-geocoding/tests/addresses.yml';
public static $fixture_file = 'addresses.yml';

public function testAddressModel()
{
$address = $this->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));
Expand Down
4 changes: 2 additions & 2 deletions tests/addresses.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Address:
SilverShop\Model\Address:
address1:
Country: NZ
State: Wellington
Expand All @@ -17,4 +17,4 @@ Address:
PostalCode: 1010
Company: SilverStripe LTD
Longitude: 174.76705230
Latitude: -36.84646160
Latitude: -36.84646160

0 comments on commit d19b9a0

Please sign in to comment.