Passed
Push — master ( a672ea...4036c4 )
by Nic
03:20
created

ProductOption::getCMSFields()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 95
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 64
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 95
rs 8.7853

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

298
    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...
299
    {
300
        if (!$member) {
301
            $member = Security::getCurrentUser();
302
        }
303
304
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
305
    }
306
307
    /**
308
     * @param $member
309
     * @return bool|int|void
310
     */
311
    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

311
    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...
312
    {
313
        if (!$member) {
314
            $member = Security::getCurrentUser();
315
        }
316
317
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
318
    }
319
}
320