Completed
Push — require/versionedDO2 ( a1f545 )
by Jason
05:34
created

DistanceDataExtension::augmentSQL()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 1
crap 12
1
<?php
2
3
class DistanceDataExtension extends DataExtension
4
{
5
    /**
6
     * @param SQLQuery $query
7
     */
8
    public function augmentSQL(SQLQuery &$query)
9
    {
10
        $address = Controller::curr()->getRequest()->getVar('Address');
11
        if ($this->owner->hasMethod('updateAddressValue')) {
12
            $address = $this->owner->updateAddressValue($address);
13
        }
14
        if ($address) { // on frontend
15
            $coords = GoogleGeocoding::address_to_point($address);
16
17
            $Lat = $coords['lat'];
18
            $Lng = $coords['lng'];
19
20
            $query
21
                ->addSelect(array(
22
                    '( 3959 * acos( cos( radians(' . $Lat . ') ) * cos( radians( `Lat` ) ) * cos( radians( `Lng` ) - radians(' . $Lng . ') ) + sin( radians(' . $Lat . ') ) * sin( radians( `Lat` ) ) ) ) AS distance',
23
                ));
24
        }
25
    }
26
}
27