FreePostageDiscount   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 23
c 1
b 0
f 0
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B calculateAmount() 0 35 7
A appliedAmount() 0 3 1
1
<?php
2
3
namespace SilverCommerce\Discounts\Model;
4
5
use SilverStripe\ORM\ArrayList;
6
use SilverCommerce\Discounts\Model\Discount;
7
use SilverCommerce\OrdersAdmin\Model\Estimate;
8
use SilverCommerce\TaxAdmin\Helpers\MathsHelper;
9
10
class FreePostageDiscount extends Discount
11
{
12
    private static $table_name = 'Discount_FreePostage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
13
14
    private static $description = "removes the postage cost from an order";
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
15
16
    public function calculateAmount(Estimate $estimate)
17
    {
18
        $cats = $this->Categories();
0 ignored issues
show
Bug introduced by
The method Categories() does not exist on SilverCommerce\Discounts\Model\FreePostageDiscount. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
        /** @scrutinizer ignore-call */ 
19
        $cats = $this->Categories();
Loading history...
19
        $all_products = ArrayList::create();
20
        $value = $estimate->getPostage()->getPrice();
21
        $min = (float) $this->MinOrder;
0 ignored issues
show
Bug Best Practice introduced by
The property MinOrder does not exist on SilverCommerce\Discounts\Model\FreePostageDiscount. Since you implemented __get, consider adding a @property annotation.
Loading history...
22
23
        if ($cats->count() > 0) {
24
            foreach ($cats as $cat) {
25
                $all_products->merge($cat->Products());
26
            }
27
28
            foreach ($estimate->Items() as $line_item) {
29
                $match = $line_item->FindStockItem();
30
                if (!$all_products->find('ID', $match->ID)) {
31
                    $value = 0;
32
                }
33
            }
34
        }
35
36
        $converted_value = (int) ($value * 100);
37
38
        $converted_amount = $converted_value;
39
40
        $amount = MathsHelper::round($converted_amount, 0)/100;
41
42
        if ($amount > $value) {
43
            $amount = $value;
44
        }
45
        
46
        if ($value < $min) {
47
            $amount = 0;
48
        }
49
50
        return $amount;
51
    }
52
53
    public function appliedAmount(AppliedDiscount $item)
54
    {
55
        return $this->calculateAmount($item->Estimate());
0 ignored issues
show
Bug introduced by
The method Estimate() does not exist on SilverCommerce\Discounts\Model\AppliedDiscount. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        return $this->calculateAmount($item->/** @scrutinizer ignore-call */ Estimate());
Loading history...
56
    }
57
}
58