Passed
Pull Request — master (#44)
by Jason
03:29
created

ProductOption   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 118
dl 0
loc 272
rs 10
c 0
b 0
f 0
wmc 23

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getPriceModifierWithSymbol() 0 3 1
A getOptionModifierActionSymbol() 0 13 4
A canCreate() 0 7 2
A getAvailability() 0 5 2
A getCodeModifierWithSymbol() 0 3 1
A canDelete() 0 7 2
A getGeneratedTitle() 0 11 3
A getGeneratedValue() 0 8 3
A getIsAvailable() 0 5 1
B getCMSFields() 0 90 1
A getWeightModifierWithSymbol() 0 3 1
A canEdit() 0 7 2
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\CurrencyField;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\HeaderField;
10
use SilverStripe\Forms\TextField;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\FieldType\DBBoolean;
13
use SilverStripe\Security\Permission;
14
use SilverStripe\Security\Security;
15
16
class ProductOption extends DataObject
17
{
18
    /**
19
     * @var array
20
     */
21
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
22
        'Title' => 'Varchar(255)',
23
    );
24
25
    /**
26
     * @var array
27
     */
28
    private static $belongs_many_many = [
0 ignored issues
show
introduced by
The private property $belongs_many_many is not used, and could be removed.
Loading history...
29
        'Types' => OptionType::class,
30
    ];
31
32
    /**
33
     * @var string
34
     */
35
    private static $table_name = 'ProductOption';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
36
37
    /**
38
     * @var string
39
     */
40
    private static $singular_name = 'Option';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
41
42
    /**
43
     * @var string
44
     */
45
    private static $plural_name = 'Options';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
46
47
    /**
48
     * @var array
49
     */
50
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
51
        'ManyMany[Available]' => true,
52
    ];
53
54
    /**
55
     * @var array
56
     */
57
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
58
        'Title' => 'Title',
59
        'IsAvailable' => 'Available',
60
    ];
61
62
    /**
63
     * @return FieldList
64
     */
65
    public function getCMSFields()
66
    {
67
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
68
69
            $fields->removeByName([
70
                'Types',
71
72
            ]);
73
74
            $fields->addFieldsToTab('Root.Main', array(
75
                CheckboxField::create('ManyMany[Available]', 'Available for purchase'),
76
77
                // Weight Modifier Fields
78
                HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 4),
79
                TextField::create("ManyMany[WeightModifier]")
80
                    ->setTitle(_t('OptionItem.WeightModifier', 'Weight')),
81
                DropdownField::create(
82
                    'ManyMany[WeightModifierAction]',
83
                    _t('OptionItem.WeightModifierAction', 'Weight Modification'),
84
                    array(
85
                        'Add' => _t(
86
                            'OptionItem.WeightAdd',
87
                            'Add to Base Weight',
88
                            'Add to weight'
89
                        ),
90
                        'Subtract' => _t(
91
                            'OptionItem.WeightSubtract',
92
                            'Subtract from Base Weight',
93
                            'Subtract from weight'
94
                        ),
95
                        'Set' => _t('OptionItem.WeightSet', 'Set as a new Weight'),
96
                    )
97
                )
98
                    ->setEmptyString('')
99
                    ->setDescription(_t(
100
                        'OptionItem.WeightDescription',
101
                        'Does weight modify or replace base weight?'
102
                    )),
103
104
                // Price Modifier FIelds
105
                HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 4),
106
                CurrencyField::create('ManyMany[PriceModifier]')
107
                    ->setTitle(_t('OptionItem.PriceModifier', 'Price')),
108
                DropdownField::create(
109
                    'ManyMany[PriceModifierAction]',
110
                    _t('OptionItem.PriceModifierAction', 'Price Modification'),
111
                    array(
112
                        'Add' => _t(
113
                            'OptionItem.PriceAdd',
114
                            'Add to Base Price',
115
                            'Add to price'
116
                        ),
117
                        'Subtract' => _t(
118
                            'OptionItem.PriceSubtract',
119
                            'Subtract from Base Price',
120
                            'Subtract from price'
121
                        ),
122
                        'Set' => _t('OptionItem.PriceSet', 'Set as a new Price'),
123
                    )
124
                )
125
                    ->setEmptyString('')
126
                    ->setDescription(_t('OptionItem.PriceDescription', 'Does price modify or replace base price?')),
127
128
                // Code Modifier Fields
129
                HeaderField::create('CodeHD', _t('OptionItem.CodeHD', 'Modify Code'), 4),
130
                TextField::create('ManyMany[CodeModifier]')
131
                    ->setTitle(_t('OptionItem.CodeModifier', 'Code')),
132
                DropdownField::create(
133
                    'ManyMany[CodeModifierAction]',
134
                    _t('OptionItem.CodeModifierAction', 'Code Modification'),
135
                    array(
136
                        'Add' => _t(
137
                            'OptionItem.CodeAdd',
138
                            'Add to Base Code',
139
                            'Add to code'
140
                        ),
141
                        'Subtract' => _t(
142
                            'OptionItem.CodeSubtract',
143
                            'Subtract from Base Code',
144
                            'Subtract from code'
145
                        ),
146
                        'Set' => _t('OptionItem.CodeSet', 'Set as a new Code'),
147
                    )
148
                )
149
                    ->setEmptyString('')
150
                    ->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')),
151
            ));
152
        });
153
154
        return parent::getCMSFields();
155
    }
156
157
    /**
158
     * @param $oma
159
     * @param bool $returnWithOnlyPlusMinus
160
     *
161
     * @return string
162
     */
163
    public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus = false)
164
    {
165
        switch ($oma) {
166
            case 'Subtract':
167
                $symbol = '-';
168
                break;
169
            case 'Set':
170
                $symbol = ($returnWithOnlyPlusMinus) ? '' : ':';
171
                break;
172
            default:
173
                $symbol = '+';
174
        }
175
        return $symbol;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getWeightModifierWithSymbol()
182
    {
183
        return self::getOptionModifierActionSymbol($this->WeightModifierAction) . $this->WeightModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property WeightModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property WeightModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
184
    }
185
186
    /**
187
     * @return string
188
     */
189
    public function getPriceModifierWithSymbol()
190
    {
191
        return self::getOptionModifierActionSymbol($this->PriceModifierAction) . $this->PriceModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property PriceModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getCodeModifierWithSymbol()
198
    {
199
        return self::getOptionModifierActionSymbol($this->CodeModifierAction) . $this->CodeModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property CodeModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property CodeModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
200
    }
201
202
    /**
203
     * @return string
204
     */
205
    public function getGeneratedValue()
206
    {
207
        $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0';
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
208
        $modPriceWithSymbol = self::getOptionModifierActionSymbol($this->PriceModifierAction) . $modPrice;
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
209
        $modWeight = ($this->WeightModifier) ? (string) $this->WeightModifier : '0';
0 ignored issues
show
Bug Best Practice introduced by
The property WeightModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
210
        $modWeight = self::getOptionModifierActionSymbol($this->WeightModifierAction) . $modWeight;
0 ignored issues
show
Bug Best Practice introduced by
The property WeightModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
211
        $modCode = self::getOptionModifierActionSymbol($this->CodeModifierAction) . $this->CodeModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property CodeModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property CodeModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
212
        return $this->Title . '{p' . $modPriceWithSymbol . '|w' . $modWeight . '|c' . $modCode . '}';
213
    }
214
215
    /**
216
     * @return mixed|string
217
     */
218
    public function getGeneratedTitle()
219
    {
220
        $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0';
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
221
        $title = $this->Title;
222
        $title .= ($this->PriceModifier != 0) ?
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->PriceModifier of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
223
            ': (' . self::getOptionModifierActionSymbol(
224
                $this->PriceModifierAction,
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
225
                $returnWithOnlyPlusMinus = true
226
            ) . '$' . $modPrice . ')' :
227
            '';
228
        return $title;
229
    }
230
231
    /**
232
     * @return bool
233
     */
234
    public function getAvailability()
235
    {
236
        $available = ($this->Available == 1) ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
The property Available does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
237
        $this->extend('updateOptionAvailability', $available);
238
        return $available;
239
    }
240
241
    /**
242
     * @return string
243
     */
244
    public function getIsAvailable()
245
    {
246
        $available = DBBoolean::create();
247
        $available->setValue($this->getAvailability());
248
        return $available->Nice();
249
    }
250
251
    /**
252
     * @param $member
253
     * @return bool|int|void
254
     */
255
    public function canCreate($member = null, $context = [])
256
    {
257
        if (!$member) {
258
            $member = Security::getCurrentUser();
259
        }
260
261
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
262
    }
263
264
    /**
265
     * @param $member
266
     * @return bool|int|void|null
267
     */
268
    public function canEdit($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

268
    public function canEdit($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
269
    {
270
        if (!$member) {
271
            $member = Security::getCurrentUser();
272
        }
273
274
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
275
    }
276
277
    /**
278
     * @param $member
279
     * @return bool|int|void
280
     */
281
    public function canDelete($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

281
    public function canDelete($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
282
    {
283
        if (!$member) {
284
            $member = Security::getCurrentUser();
285
        }
286
287
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
288
    }
289
}
290