Passed
Pull Request — master (#20)
by Jason
01:32
created

AddToCartForm::getProductFields()   B

Complexity

Conditions 9
Paths 50

Size

Total Lines 99
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 51
c 0
b 0
f 0
nc 50
nop 1
dl 0
loc 99
rs 7.5135

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\Form;
4
5
use Dynamic\Foxy\Extension\Purchasable;
6
use Dynamic\Foxy\Extension\Shippable;
7
use Dynamic\Foxy\Model\Foxy;
8
use Dynamic\Foxy\Model\Setting;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\Form;
11
use SilverStripe\Forms\FormAction;
12
use SilverStripe\Forms\HeaderField;
13
use SilverStripe\Forms\HiddenField;
14
use SilverStripe\Forms\RequiredFields;
15
16
class AddToCartForm extends Form
17
{
18
    /**
19
     * @var
20
     */
21
    protected $foxy_setting;
22
23
    /**
24
     * @var
25
     */
26
    private $product;
27
28
    /**
29
     * @param $foxySetting
30
     *
31
     * @return $this
32
     */
33
    public function setFoxySetting($foxySetting)
34
    {
35
        $foxySetting = $foxySetting === null ? Setting::current_foxy_setting() : $foxySetting;
36
        if ($foxySetting instanceof Setting) {
37
            $this->foxy_setting = $foxySetting;
38
            return $this;
39
        }
40
        throw new \InvalidArgumentException('$foxySetting needs to be an instance of Foxy Setting.');
41
    }
42
43
    /**
44
     * @return FoxyStripeSetting
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Form\FoxyStripeSetting 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...
45
     */
46
    public function getFoxySetting()
47
    {
48
        if (!$this->foxy_setting) {
49
            $this->setFoxySetting(Setting::current_foxy_setting());
50
        }
51
        return $this->foxy_setting;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->foxy_setting returns the type Dynamic\Foxy\Model\Setting which is incompatible with the documented return type Dynamic\Foxy\Form\FoxyStripeSetting.
Loading history...
52
    }
53
54
    /**
55
     * @param $product
56
     *
57
     * @return $this
58
     */
59
    public function setProduct($product)
60
    {
61
        if ($product->isProduct()) {
62
            $this->product = $product;
63
            return $this;
64
        }
65
        throw new \InvalidArgumentException('$product needs to implement a Foxy DataExtension.');
66
    }
67
68
    /**
69
     * @return ProductPage
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Form\ProductPage 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...
70
     */
71
    public function getProduct()
72
    {
73
        return $this->product;
74
    }
75
76
    /**
77
     * AddToCartForm constructor.
78
     *
79
     * @param ContentController $controller
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Form\ContentController 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...
80
     * @param string $name
81
     * @param FieldList|null $fields
82
     * @param FieldList|null $actions
83
     * @param null $validator
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $validator is correct as it would always require null to be passed?
Loading history...
84
     * @param null $product
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $product is correct as it would always require null to be passed?
Loading history...
85
     * @param null $foxySetting
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $foxySetting is correct as it would always require null to be passed?
Loading history...
86
     *
87
     */
88
    public function __construct(
89
        $controller,
90
        $name,
91
        FieldList $fields = null,
92
        FieldList $actions = null,
93
        $validator = null,
94
        $product = null,
95
        $foxySetting = null
96
    ) {
97
        $this->setProduct($product);
98
        $this->setFoxySetting($foxySetting);
99
100
        $fields = ($fields != null && $fields->exists()) ?
101
            $this->getProductFields($fields) :
102
            $this->getProductFields(FieldList::create());
103
104
        $actions = ($actions != null && $actions->exists()) ?
105
            $this->getProductActions($actions) :
106
            $this->getProductActions(FieldList::create());
107
108
        $validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create();
109
110
        parent::__construct($controller, $name, $fields, $actions, $validator);
111
112
        //have to call after parent::__construct()
113
        $this->setAttribute('action', Foxy::FormActionURL());
114
        $this->disableSecurityToken();
115
        $this->setHTMLID($this->getTemplateHelper()->generateFormID($this) . "_{$product->ID}");
116
    }
117
118
    /**
119
     * @param FieldList $fields
120
     *
121
     * @return FieldList
122
     */
123
    protected function getProductFields(FieldList $fields)
124
    {
125
        $hiddenTitle = ($this->product->ReceiptTitle) ?
126
            htmlspecialchars($this->product->ReceiptTitle) :
127
            htmlspecialchars($this->product->Title);
128
        $code = $this->product->Code;
129
130
        if ($this->product->isAvailable()) {
131
            $fields->push(
132
                HiddenField::create('name')
133
                    ->setValue(
134
                        Foxy::getGeneratedValue($code, 'name', $hiddenTitle, 'value')
135
                    )
136
            );
137
138
            $fields->push(
139
                HiddenField::create('category')
140
                    ->setValue(
141
                        Foxy::getGeneratedValue($code, 'category', $this->product->FoxyCategory()->Code, 'value')
142
                    )
143
            );
144
145
            $fields->push(
146
                HiddenField::create('code')
147
                    ->setValue(
148
                        Foxy::getGeneratedValue($code, 'code', $this->product->Code, 'value')
149
                    )
150
            );
151
152
            $fields->push(
153
                HiddenField::create('product_id')
154
                    ->setValue(
155
                        Foxy::getGeneratedValue($code, 'product_id', $this->product->ID, 'value')
156
                    )
157
            );
158
159
            $fields->push(
160
                HiddenField::create('price')
161
                    ->setValue(
162
                        Foxy::getGeneratedValue($code, 'price', $this->product->Price, 'value')
163
                    )
164
            );
165
166
            if ($this->product->hasExtension(Shippable::class)) {
167
                if ($this->product->Weight > 0) {
168
                    $fields->push(
169
                        HiddenField::create('weight')
170
                            ->setValue(
171
                                Foxy::getGeneratedValue($code, 'weight', $this->product->Weight, 'value')
172
                            )
173
                    );
174
                }
175
            }
176
177
            $image = null;
178
            if ($this->product->getImage()) {
179
                $image = $this->product->getImage()->getCMSThumbnail()->absoluteURL;
180
            }
181
            if ($image) {
182
                $fields->push(
183
                    HiddenField::create('image')
184
                        ->setValue(
185
                            Foxy::getGeneratedValue($code, 'image', $image, 'value')
186
                        )
187
                );
188
            }
189
190
            /*
191
            // TODO: revisit after product options are implemented
192
            $optionsSet = $this->getProductOptionSet();
193
            $fields->push($optionsSet);
194
            $quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10;
195
            $fields->push(QuantityField::create('x:visibleQuantity')->setTitle('Quantity')->setValue(1));
196
            $fields->push(
197
                HiddenField::create('quantity')
198
                    ->setValue(
199
                        Foxy::getGeneratedValue($code, 'quantity', 1, 'value')
200
                    )
201
            );
202
            */
203
204
            $fields->push(
205
                HeaderField::create('submitPrice', '$' . $this->product->Price, 4)
206
                    ->addExtraClass('submit-price')
207
            );
208
            $fields->push(
209
                $unavailable = HeaderField::create('unavailableText', 'Selection unavailable', 4)
210
                    ->addExtraClass('unavailable-text')
211
            );
212
            if (!empty(trim($this->foxy_setting->StoreDomain)) && $this->product->isAvailable()) {
213
                $unavailable->addExtraClass('hidden');
214
            }
215
216
            $this->extend('updateProductFields', $fields);
217
        } else {
218
            $fields->push(HeaderField::create('unavailableText', 'currently unavaiable', 4));
219
        }
220
221
        return $fields;
222
    }
223
224
    /**
225
     * @param FieldList $actions
226
     *
227
     * @return FieldList
228
     */
229
    protected function getProductActions(FieldList $actions)
230
    {
231
        if (!empty(trim($this->foxy_setting->StoreDomain)) && $this->product->isAvailable()) {
232
            $actions->push(
233
                FormAction::create(
234
                    'x:submit',
235
                    _t(__CLASS__ . '.AddToCart', 'Add to Cart')
236
                )
237
                    ->addExtraClass('fs-add-to-cart-button')
238
                    ->setName('x:submit')
239
            );
240
        }
241
242
        $this->extend('updateProductActions', $actions);
243
244
        return $actions;
245
    }
246
}
247