Warehouse   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 10 1
B closest_to() 0 16 5
1
<?php
2
3
/**
4
 * @package silvershop-shipping
5
 */
6
class Warehouse extends DataObject
7
{
8
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
9
        'Title' => 'Varchar'
10
    );
11
12
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
13
        'Address' => 'Address'
14
    );
15
16
    public static $summary_fields = array(
17
        'Title', 'Address'
18
    );
19
20
    public function getCMSFields()
21
    {
22
        $fields = parent::getCMSFields();
23
        $fields->removeByName("AddressID");
24
        $fields->addFieldToTab("Root.Main",
25
            HasOneButtonField::create("Address", "Address", $this)
26
        );
27
28
        return $fields;
29
    }
30
31
    /**
32
     * Get the closest warehouse to an address.
33
     *
34
     * @param  Address $address
35
     * @return Warehouse
36
     */
37
    public static function closest_to(Address $address)
38
    {
39
        $warehouses = self::get()
40
            ->where("\"AddressID\" IS NOT NULL");
41
        $closestwarehouse = null;
42
        $shortestdistance = null;
43
        foreach ($warehouses as $warehouse) {
44
            $dist = $warehouse->Address()->distanceTo($address);
45
            if ($dist && ($shortestdistance === null || $dist < $shortestdistance)) {
46
                $closestwarehouse = $warehouse;
47
                $shortestdistance = $dist;
48
            }
49
        }
50
51
        return $closestwarehouse;
52
    }
53
}
54