Passed
Pull Request — master (#11)
by Jason
02:21
created

DiscountTier::getCMSFields()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 22
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 35
rs 9.568
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Model;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\ORM\DataObject;
7
8
/**
9
 * Class DiscountTier
10
 * @package Dynamic\Foxy\Discounts\Model
11
 */
12
class DiscountTier extends DataObject
13
{
14
    /**
15
     * @var array
16
     */
17
    private static $db = [
1 ignored issue
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
18
        'Quantity' => 'Int',
19
        'Percentage' => 'Int',
20
        'Amount' => 'Currency',
21
    ];
22
23
    /**
24
     * @var array
25
     */
26
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
27
        'Discount' => Discount::class,
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
34
        'Quantity' => 1,
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
41
        'DiscountLabel' => [
42
            'title' => 'Discount',
43
        ],
44
        'Quantity',
45
    ];
46
47
    /**
48
     * @var string
49
     */
50
    private static $table_name = 'FoxyDiscountTier';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
51
52
    /**
53
     * @var array
54
     */
55
    private static $default_sort = array(
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
56
        'Quantity'
57
    );
58
59
    /**
60
     * @return FieldList|void
61
     */
62
    public function getCMSFields()
63
    {
64
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
65
            $fields->removeByName([
66
                'DiscountID',
67
            ]);
68
69
            $quantity = $fields->dataFieldByName('Quantity');
70
            $quantity->setTitle('Quantity to trigger discount');
71
72
            $type = $this->Discount()->Type;
0 ignored issues
show
Bug introduced by
The method Discount() does not exist on Dynamic\Foxy\Discounts\Model\DiscountTier. 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

72
            $type = $this->/** @scrutinizer ignore-call */ Discount()->Type;
Loading history...
73
            $percentage = $fields->dataFieldByName('Percentage')
74
                ->setTitle('Percent discount');
75
            $amount = $fields->dataFieldByName('Amount')
76
                ->setTitle('Amount to discount');
77
78
            $fields->removeByName([
79
                'Percentage',
80
                'Amount',
81
            ]);
82
83
            if ($type == 'Percent') {
84
                $fields->addFieldToTab(
85
                    'Root.Main',
86
                    $percentage
87
                );
88
            } elseif ($type == 'Amount') {
89
                $fields->addFieldToTab(
90
                    'Root.Main',
91
                    $amount
92
                );
93
            }
94
        });
95
96
        return parent::getCMSFields();
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getDiscountLabel()
103
    {
104
        $type = $this->Discount()->Type;
105
        if ($type == 'Percent') {
106
            return "{$this->Percentage}%";
0 ignored issues
show
Bug Best Practice introduced by
The property Percentage does not exist on Dynamic\Foxy\Discounts\Model\DiscountTier. Since you implemented __get, consider adding a @property annotation.
Loading history...
107
        } elseif ($type == 'Amount') {
108
            return $this->dbObject('Amount')->Nice();
109
        }
110
    }
111
}
112