Passed
Push — master ( 3a5fda...57ad9e )
by Nic
04:24
created

DiscountTier::getParent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Model;
4
5
use Dynamic\Foxy\Coupons\Model\Coupon;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Coupons\Model\Coupon was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\ORM\DataObject;
8
9
/**
10
 * Class DiscountTier
11
 * @package Dynamic\Foxy\Discounts\Model
12
 */
13
class DiscountTier extends DataObject
14
{
15
    /**
16
     * @var array
17
     */
18
    private static $db = [
19
        'Quantity' => 'Int',
20
        'Percentage' => 'Int',
21
        'Amount' => 'Currency',
22
    ];
23
24
    /**
25
     * @var array
26
     */
27
    private static $has_one = [
28
        'Discount' => Discount::class,
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    private static $defaults = [
35
        'Quantity' => 1,
36
    ];
37
38
    /**
39
     * @var array
40
     */
41
    private static $summary_fields = [
42
        'DiscountLabel' => [
43
            'title' => 'Discount',
44
        ],
45
        'Quantity',
46
    ];
47
48
    /**
49
     * @var string
50
     */
51
    private static $table_name = 'FoxyDiscountTier';
52
53
    /**
54
     * @var array
55
     */
56
    private static $default_sort = [
57
        'Quantity',
58
    ];
59
60
    /**
61
     * @return FieldList|void
62
     */
63
    public function getCMSFields()
64
    {
65
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
66
            $fields->removeByName([
67
                'DiscountID',
68
            ]);
69
70
            $quantity = $fields->dataFieldByName('Quantity');
71
            $quantity->setTitle('Quantity to trigger discount');
72
73
            /** @var Discount|Coupon $type */
74
            if ($parent = $this->getParent()) {
75
                $type = $parent->Type;
0 ignored issues
show
Bug introduced by
The property Type does not exist on string.
Loading history...
76
77
                $percentage = $fields->dataFieldByName('Percentage')
78
                    ->setTitle('Percent discount');
79
                $amount = $fields->dataFieldByName('Amount')
80
                    ->setTitle('Amount to discount');
81
82
                $fields->removeByName([
83
                    'Percentage',
84
                    'Amount',
85
                ]);
86
87
                if ($type == 'Percent') {
88
                    $fields->addFieldToTab(
89
                        'Root.Main',
90
                        $percentage
91
                    );
92
                } elseif ($type == 'Amount') {
93
                    $fields->addFieldToTab(
94
                        'Root.Main',
95
                        $amount
96
                    );
97
                }
98
            }
99
        });
100
101
        return parent::getCMSFields();
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    protected function getParent()
108
    {
109
        foreach ($this->hasOne() as $relationName => $className) {
110
            $field = "{$relationName}ID";
111
112
            if ($this->{$field} > 0) {
113
                return $className::get()->byID($this->{$field});
114
            }
115
        }
116
117
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
118
    }
119
120
    /**
121
     * @return \SilverStripe\ORM\ValidationResult|void
122
     */
123
    public function validate()
124
    {
125
        $response = parent::validate();
126
127
        if ($this->exists()) {
128
            $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...
129
        }
130
131
        /** @var Discount $discount */
132
        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...
133
            $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

133
            $existing = $discount->/** @scrutinizer ignore-call */ DiscountTiers()->filter('Quantity', $this->Quantity);
Loading history...
134
            if (isset($exclude)) {
135
                $existing = $existing->exclude($exclude);
136
            }
137
138
            if ($existing->count() > 0) {
139
                $response->addError("A discount tier already has the quantity {$this->Quantity} set");
140
            }
141
        }
142
143
        return $response;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getDiscountLabel()
150
    {
151
        $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

151
        $type = $this->/** @scrutinizer ignore-call */ Discount()->Type;
Loading history...
152
        $label = '';
153
154
        if ($type == 'Percent') {
155
            $label = "{$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...
156
        } elseif ($type == 'Amount') {
157
            $label = $this->dbObject('Amount')->Nice();
158
        }
159
160
        $this->extend('updateDiscountLabel', $label);
161
162
        return $label;
163
    }
164
}
165