FixedRateDiscount   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B calculateAmount() 0 32 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
9
class FixedRateDiscount extends Discount
10
{
11
    private static $table_name = 'Discount_FixedRate';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
12
13
    private static $description = "Simple fixed value discount";
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
14
15
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
16
        "Amount"    => "Decimal"
17
    ];
18
19
    public function calculateAmount(Estimate $estimate)
20
    {
21
        $cats = $this->Categories();
0 ignored issues
show
Bug introduced by
The method Categories() does not exist on SilverCommerce\Discounts\Model\FixedRateDiscount. 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

21
        /** @scrutinizer ignore-call */ 
22
        $cats = $this->Categories();
Loading history...
22
        $all_products = ArrayList::create();
23
        $value = $estimate->getSubTotal();
24
        $min = (float) $this->MinOrder;
0 ignored issues
show
Bug Best Practice introduced by
The property MinOrder does not exist on SilverCommerce\Discounts\Model\FixedRateDiscount. Since you implemented __get, consider adding a @property annotation.
Loading history...
25
26
        if ($cats->count() > 0) {
27
            $value = 0;
28
            foreach ($cats as $cat) {
29
                $all_products->merge($cat->Products());
30
            }
31
32
            foreach ($estimate->Items() as $line_item) {
33
                $match = $line_item->FindStockItem();
34
                if ($all_products->find('ID', $match->ID)) {
35
                    $value += ($line_item->Quantity * $line_item->UnitPrice);
36
                }
37
            }
38
        }
39
40
        $amount = $this->Amount;
0 ignored issues
show
Bug Best Practice introduced by
The property Amount does not exist on SilverCommerce\Discounts\Model\FixedRateDiscount. Since you implemented __get, consider adding a @property annotation.
Loading history...
41
42
        if ($value < $min) {
43
            $amount = 0;
44
        }
45
46
        if ($amount > $value) {
47
            $amount = $value;
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