Completed
Push — master ( dd0de2...7f19c4 )
by Roman
04:08
created

OrderShippingExtension::setShippingMethod()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 3
nop 1
1
<?php
2
3
/**
4
 * @package silvershop-shipping
5
 */
6
class OrderShippingExtension extends DataExtension
7
{
8
    private static $db = array(
9
        'ShippingTotal' => 'Currency'
10
    );
11
    private static $has_one = array(
12
        'ShippingMethod' => 'ShippingMethod'
13
    );
14
15
    public function createShippingPackage()
16
    {
17
        //create package, with total weight, dimensions, value, etc
18
        $weight = $width = $height = $depth = $value = $quantity = 0;
0 ignored issues
show
Unused Code introduced by
$quantity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$depth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$height is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$width is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$weight is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
19
20
        $items = $this->owner->Items();
21
        if (!$items->exists()) {
22
            return new ShippingPackage();
23
        }
24
25
        $weight = $items->Sum('Weight', true); //Sum is found on OrdItemList (Component Extension)
26
        $width = $items->Sum('Width', true);
27
        $height = $items->Sum('Height', true);
28
        $depth = $items->Sum('Depth', true);
29
30
        $value = $this->owner->SubTotal();
31
        $quantity = $items->Quantity();
32
33
        $package = new ShippingPackage($weight,
34
            array($height,$width,$depth),
35
            array(
36
                'value' => $value,
37
                'quantity' => $quantity
38
            )
39
        );
40
        return $package;
41
    }
42
43
    /**
44
     * Get shipping estimates
45
     * @return DataList
46
     */
47
    public function getShippingEstimates()
48
    {
49
        //$package = $this->order->createShippingPackage();
50
        $address = $this->owner->getShippingAddress();
51
        $estimator = new ShippingEstimator($this->owner, $address);
52
        $estimates = $estimator->getEstimates();
53
        return $estimates;
54
    }
55
56
    /*
57
     * Set shipping method and shipping cost
58
     * @param $option - shipping option to set, and calculate shipping from
59
     * @return boolean sucess/failure of setting
60
     */
61
    public function setShippingMethod(ShippingMethod $option)
62
    {
63
        $package = $this->owner->createShippingPackage();
64
        if (!$package) {
65
            throw new Exception(_t("OrderShippingExtension.NoPackage", "Shipping package information not available"));
66
        }
67
        $address = $this->owner->getShippingAddress();
68
        if (!$address || !$address->exists() && $option->requiresAddress()) {
69
            throw new Exception(_t("OrderShippingExtension.NoAddress", "No address has been set"));
70
        }
71
        $this->owner->ShippingTotal = $option->calculateRate($package, $address);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->owner->ShippingTotal is correct as $option->calculateRate($package, $address) (which targets ShippingMethod::calculateRate()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
72
        $this->owner->ShippingMethodID = $option->ID;
73
        $this->owner->write();
74
        return true;
75
    }
76
}
77