Completed
Push — master ( bc819e...93fc15 )
by Nic
16s queued 10s
created

DiscountTier::validate()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 10
nop 0
dl 0
loc 21
rs 9.6111
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 = [
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 \SilverStripe\ORM\ValidationResult|void
101
     */
102
    public function validate()
103
    {
104
        $response = parent::validate();
105
106
        if ($this->exists()) {
107
            $exclude['ID'] = $this->ID;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$exclude was never initialized. Although not strictly required by PHP, it is generally a good practice to add $exclude = array(); before regardless.
Loading history...
108
        }
109
110
        /** @var Discount $discount */
111
        if ($discount = Discount::get()->byID($this->DiscountID)) {
0 ignored issues
show
Bug Best Practice introduced by
The property DiscountID does not exist on Dynamic\Foxy\Discounts\Model\DiscountTier. Since you implemented __get, consider adding a @property annotation.
Loading history...
112
            $existing = $discount->DiscountTiers()->filter('Quantity', $this->Quantity);
0 ignored issues
show
Bug Best Practice introduced by
The property Quantity does not exist on Dynamic\Foxy\Discounts\Model\DiscountTier. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method DiscountTiers() does not exist on Dynamic\Foxy\Discounts\Model\Discount. 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

112
            $existing = $discount->/** @scrutinizer ignore-call */ DiscountTiers()->filter('Quantity', $this->Quantity);
Loading history...
113
            if (isset($exclude)) {
114
                $existing = $existing->exclude($exclude);
115
            }
116
117
            if ($existing->count() > 0) {
118
                $response->addError("A discount tier already has the quantity {$this->Quantity} set");
119
            }
120
        }
121
122
        return $response;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getDiscountLabel()
129
    {
130
        $type = $this->Discount()->Type;
131
        if ($type == 'Percent') {
132
            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...
133
        } elseif ($type == 'Amount') {
134
            return $this->dbObject('Amount')->Nice();
135
        }
136
    }
137
}
138