Passed
Push — master ( 44cbe3...1a2823 )
by Jason
01:49
created

ProductOption::getOptionModifierActionSymbol()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 2
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
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
13
class ProductOption extends DataObject
14
{
15
    /**
16
     * @var array
17
     */
18
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        'Title' => 'Varchar(255)',
20
    );
21
22
    /**
23
     * @var array
24
     */
25
    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...
26
        'Types' => OptionType::class,
27
    ];
28
29
    /**
30
     * @var string
31
     */
32
    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...
33
34
    /**
35
     * @var string
36
     */
37
    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...
38
39
    /**
40
     * @var string
41
     */
42
    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...
43
44
    /**
45
     * @return FieldList
46
     */
47
    public function getCMSFields()
48
    {
49
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
50
51
            $fields->removeByName([
52
                'Types',
53
            ]);
54
55
            $fields->addFieldsToTab('Root.Main', array(
56
                CheckboxField::create('ManyMany[Available]', 'Available for purchase'),
57
58
                // Weight Modifier Fields
59
                HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 4),
60
                TextField::create("ManyMany[WeightModifier]")
61
                    ->setTitle(_t('OptionItem.WeightModifier', 'Weight')),
62
                DropdownField::create(
63
                    'ManyMany[WeightModifierAction]',
64
                    _t('OptionItem.WeightModifierAction', 'Weight Modification'),
65
                    array(
66
                        'Add' => _t(
67
                            'OptionItem.WeightAdd',
68
                            'Add to Base Weight',
69
                            'Add to weight'
70
                        ),
71
                        'Subtract' => _t(
72
                            'OptionItem.WeightSubtract',
73
                            'Subtract from Base Weight',
74
                            'Subtract from weight'
75
                        ),
76
                        'Set' => _t('OptionItem.WeightSet', 'Set as a new Weight'),
77
                    )
78
                )
79
                    ->setEmptyString('')
80
                    ->setDescription(_t(
81
                        'OptionItem.WeightDescription',
82
                        'Does weight modify or replace base weight?'
83
                    )),
84
85
                // Price Modifier FIelds
86
                HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 4),
87
                CurrencyField::create('ManyMany[PriceModifier]')
88
                    ->setTitle(_t('OptionItem.PriceModifier', 'Price')),
89
                DropdownField::create(
90
                    'ManyMany[PriceModifierAction]',
91
                    _t('OptionItem.PriceModifierAction', 'Price Modification'),
92
                    array(
93
                        'Add' => _t(
94
                            'OptionItem.PriceAdd',
95
                            'Add to Base Price',
96
                            'Add to price'
97
                        ),
98
                        'Subtract' => _t(
99
                            'OptionItem.PriceSubtract',
100
                            'Subtract from Base Price',
101
                            'Subtract from price'
102
                        ),
103
                        'Set' => _t('OptionItem.PriceSet', 'Set as a new Price'),
104
                    )
105
                )
106
                    ->setEmptyString('')
107
                    ->setDescription(_t('OptionItem.PriceDescription', 'Does price modify or replace base price?')),
108
109
                // Code Modifier Fields
110
                HeaderField::create('CodeHD', _t('OptionItem.CodeHD', 'Modify Code'), 4),
111
                TextField::create('ManyMany[CodeModifier]')
112
                    ->setTitle(_t('OptionItem.CodeModifier', 'Code')),
113
                DropdownField::create(
114
                    'ManyMany[CodeModifierAction]',
115
                    _t('OptionItem.CodeModifierAction', 'Code Modification'),
116
                    array(
117
                        'Add' => _t(
118
                            'OptionItem.CodeAdd',
119
                            'Add to Base Code',
120
                            'Add to code'
121
                        ),
122
                        'Subtract' => _t(
123
                            'OptionItem.CodeSubtract',
124
                            'Subtract from Base Code',
125
                            'Subtract from code'
126
                        ),
127
                        'Set' => _t('OptionItem.CodeSet', 'Set as a new Code'),
128
                    )
129
                )
130
                    ->setEmptyString('')
131
                    ->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')),
132
            ));
133
        });
134
135
        return parent::getCMSFields();
136
    }
137
138
    /**
139
     * @param $oma
140
     * @param bool $returnWithOnlyPlusMinus
141
     *
142
     * @return string
143
     */
144
    public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus = false)
145
    {
146
        switch ($oma) {
147
            case 'Subtract':
148
                $symbol = '-';
149
                break;
150
            case 'Set':
151
                $symbol = ($returnWithOnlyPlusMinus) ? '' : ':';
152
                break;
153
            default:
154
                $symbol = '+';
155
        }
156
        return $symbol;
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function getWeightModifierWithSymbol()
163
    {
164
        return self::getOptionModifierActionSymbol($this->WeightModifierAction) . $this->WeightModifier;
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...
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...
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getPriceModifierWithSymbol()
171
    {
172
        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...
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getCodeModifierWithSymbol()
179
    {
180
        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...
181
    }
182
183
    /**
184
     * @return mixed
185
     */
186
    public function getProductOptionGroupTitle()
187
    {
188
        return $this->ProductOptionGroup()->Title;
0 ignored issues
show
Bug introduced by
The method ProductOptionGroup() does not exist on Dynamic\Foxy\Model\ProductOption. 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

188
        return $this->/** @scrutinizer ignore-call */ ProductOptionGroup()->Title;
Loading history...
189
    }
190
191
    /**
192
     * @return string
193
     */
194
    public function getGeneratedValue()
195
    {
196
        $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...
197
        $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...
198
        $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...
199
        $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...
200
        $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...
201
        return $this->Title . '{p' . $modPriceWithSymbol . '|w' . $modWeight . '|c' . $modCode . '}';
202
    }
203
204
    /**
205
     * @return mixed|string
206
     */
207
    public function getGeneratedTitle()
208
    {
209
        $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...
210
        $title = $this->Title;
211
        $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...
212
            ': (' . self::getOptionModifierActionSymbol(
213
                $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...
214
                $returnWithOnlyPlusMinus = true
215
            ) . '$' . $modPrice . ')' :
216
            '';
217
        return $title;
218
    }
219
220
    /**
221
     * @return bool
222
     */
223
    public function getAvailability()
224
    {
225
        $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...
226
        $this->extend('updateOptionAvailability', $available);
227
        return $available;
228
    }
229
230
    /**
231
     * @return string
232
     */
233
    public function getIsAvailable()
234
    {
235
        if ($this->getAvailability()) {
236
            return 'yes';
237
        }
238
        return 'no';
239
    }
240
}
241