AppliedDiscount::updateDiscount()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 5
c 2
b 1
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace SilverCommerce\Discounts\Model;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverCommerce\Discounts\DiscountFactory;
7
use SilverCommerce\OrdersAdmin\Model\Estimate;
8
use SilverCommerce\OrdersAdmin\Model\LineItem;
9
10
class AppliedDiscount extends DataObject
11
{
12
    private static $table_name = 'AppliedDiscount';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
13
14
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
15
        'Title' => 'Varchar',
16
        'Code' => 'Varchar',
17
        'Value' => 'Currency'
18
    ];
19
20
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
21
        'Estimate' => Estimate::class
22
    ];
23
24
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
25
        'Items' => LineItem::class
26
    ];
27
28
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
29
        'Title',
30
        'Code',
31
        'Value'
32
    ];
33
34
    public function appliedAmount(AppliedDiscount $item)
35
    {
36
        return $this->calculateValue($item->Estimate()->getTotal());
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

36
        return $this->calculateValue($item->/** @scrutinizer ignore-call */ Estimate()->getTotal());
Loading history...
Bug introduced by
The method calculateValue() 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

36
        return $this->/** @scrutinizer ignore-call */ calculateValue($item->Estimate()->getTotal());
Loading history...
37
    }
38
39
    public function updateDiscount()
40
    {
41
        $discount = $this->getDiscount();
42
        if ($discount->exists()) {
43
            $value = $discount->appliedAmount($this);
44
45
            $this->Value = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property Value does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
            $this->write();
47
        }
48
    }
49
50
    public function getDiscount()
51
    {
52
        return DiscountFactory::create()->getByIdent($this->Code);
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist on SilverCommerce\Discounts\Model\AppliedDiscount. Since you implemented __get, consider adding a @property annotation.
Loading history...
53
    }
54
}
55