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

AddToCartForm   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 93
dl 0
loc 257
rs 10
c 0
b 0
f 0
wmc 30

8 Methods

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