Completed
Push — master ( f8f77e...d2a4ab )
by Jason
05:01
created

FoxyStripePurchaseForm::__construct()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 0
cts 13
cp 0
rs 6.7272
cc 7
eloc 12
nc 64
nop 7
crap 56
1
<?php
2
3
namespace Dynamic\FoxyStripe\Form;
4
5
use Dynamic\FoxyStripe\Model\OptionGroup;
6
use Dynamic\FoxyStripe\Page\ProductPage;
7
use SilverStripe\Forms\CompositeField;
8
use SilverStripe\Forms\DropdownField;
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\ORM\GroupedList;
15
16
/**
17
 * Class FoxyStripePurchaseForm.
18
 *
19
 * @property SiteConfig $site_config
20
 * @property ProductPage $product
21
 */
22
class FoxyStripePurchaseForm extends Form
23
{
24
    /**
25
     * @var
26
     */
27
    protected $site_config;
28
    /**
29
     * @var
30
     */
31
    private $product;
32
33
    /**
34
     * @param $siteConfig
35
     *
36
     * @return $this
37
     */
38
    public function setSiteConfig($siteConfig)
39
    {
40
        $siteConfig = $siteConfig === null ? SiteConfig::current_site_config() : $siteConfig;
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\SiteConfig 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...
41
        if ($siteConfig instanceof SiteConfig) {
42
            $this->site_config = $siteConfig;
43
44
            return $this;
45
        }
46
        throw new InvalidArgumentException('$siteConfig needs to be an instance of SiteConfig.');
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\InvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
47
    }
48
49
    /**
50
     * @return SiteConfig
51
     */
52
    public function getSiteConfig()
53
    {
54
        if (!$this->site_config) {
55
            $this->setSiteConfig(SiteConfig::current_site_config());
56
        }
57
58
        return $this->site_config;
59
    }
60
61
    /**
62
     * @param $product
63
     *
64
     * @return $this
65
     */
66
    public function setProduct($product)
67
    {
68
        if ($product instanceof ProductPage) {
69
            $this->product = $product;
70
71
            return $this;
72
        }
73
        throw new InvalidArgumentException('$product needs to be an instance of ProductPage.');
74
    }
75
76
    /**
77
     * @return ProductPage
78
     */
79
    public function getProduct()
80
    {
81
        return $this->product;
82
    }
83
84
    /**
85
     * FoxyStripePurchaseForm constructor.
86
     *
87
     * @param Controller     $controller
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\Controller 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...
88
     * @param string         $name
89
     * @param FieldList|null $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $siteConfig is correct as it would always require null to be passed?
Loading history...
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...
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...
90
     * @param FieldList|null $actions
91
     * @param null           $validator
92
     * @param null           $product
93
     * @param null           $siteConfig
94
     */
95
    public function __construct(
96
        $controller,
97
        $name,
98
        FieldList $fields = null,
99
        FieldList $actions = null,
100
        $validator = null,
101
        $product = null,
102
        $siteConfig = null
103
    ) {
104
        $this->setProduct($product);
105
        $this->setSiteConfig($siteConfig);
106
107
        $fields = ($fields != null && $fields->exists()) ?
108
            $this->getProductFields($fields) :
109
            $this->getProductFields(FieldList::create());
110
111
        $actions = ($actions != null && $actions->exists()) ?
112
            $this->getProductActions($actions) :
113
            $this->getProductActions(FieldList::create());
114
        $validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create();
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\RequiredFields 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...
115
116
        parent::__construct($controller, $name, $fields, $actions, $validator);
117
118
        //have to call after parent::__construct()
119
        $this->setAttribute('action', FoxyCart::FormActionURL());
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\FoxyCart 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...
120
        $this->disableSecurityToken();
121
    }
122
123
    /**
124
     * @param FieldList $fields
125
     *
126
     * @return FieldList
127
     */
128
    protected function getProductFields(FieldList $fields)
129
    {
130
        $hiddenTitle = ($this->product->ReceiptTitle) ?
0 ignored issues
show
Bug Best Practice introduced by
The property ReceiptTitle does not exist on Dynamic\FoxyStripe\Page\ProductPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
131
            htmlspecialchars($this->product->ReceiptTitle) :
132
            htmlspecialchars($this->product->Title);
133
        $code = $this->product->Code;
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist on Dynamic\FoxyStripe\Page\ProductPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
134
135
        if ($this->product->Available) {
0 ignored issues
show
Bug Best Practice introduced by
The property Available does not exist on Dynamic\FoxyStripe\Page\ProductPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
136
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue(
0 ignored issues
show
Bug introduced by
Dynamic\FoxyStripe\Page\..., 'name', $hiddenTitle) of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

136
            $fields->push(HiddenField::create(/** @scrutinizer ignore-type */ ProductPage::getGeneratedValue(
Loading history...
137
                $code,
138
                'name',
139
                $hiddenTitle
140
            ))->setValue($hiddenTitle));
141
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue(
142
                $code,
143
                'category',
144
                $this->product->Category()->Code
0 ignored issues
show
Bug introduced by
The method Category() does not exist on Dynamic\FoxyStripe\Page\ProductPage. 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

144
                $this->product->/** @scrutinizer ignore-call */ 
145
                                Category()->Code
Loading history...
145
            ))->setValue($this->product->Category()->Code));
146
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue(
147
                $code,
148
                'code',
149
                $this->product->Code
150
            ))->setValue($this->product->Code));
151
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue(
0 ignored issues
show
Bug introduced by
Dynamic\FoxyStripe\Page\...d', $this->product->ID) of type integer|string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

151
            $fields->push(HiddenField::create(/** @scrutinizer ignore-type */ ProductPage::getGeneratedValue(
Loading history...
152
                $code,
153
                'product_id',
154
                $this->product->ID
155
            ))->setValue($this->product->ID));
156
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue(
157
                $code,
158
                'price',
159
                $this->product->Price
0 ignored issues
show
Bug Best Practice introduced by
The property Price does not exist on Dynamic\FoxyStripe\Page\ProductPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
160
            ))->setValue($this->product->Price));//can't override id
161
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue(
162
                $code,
163
                'weight',
164
                $this->product->Weight
0 ignored issues
show
Bug Best Practice introduced by
The property Weight does not exist on Dynamic\FoxyStripe\Page\ProductPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
165
            ))->setValue($this->product->Weight));
166
167
            if ($this->product->PreviewImage()->exists()) {
0 ignored issues
show
Bug introduced by
The method PreviewImage() does not exist on Dynamic\FoxyStripe\Page\ProductPage. 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

167
            if ($this->product->/** @scrutinizer ignore-call */ PreviewImage()->exists()) {
Loading history...
168
                $fields->push(
169
                    HiddenField::create(ProductPage::getGeneratedValue(
170
                        $code,
171
                        'image',
172
                        $this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL
173
                    ))
174
                        ->setValue($this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL)
175
                );
176
            }
177
178
            $optionsSet = $this->getProductOptionSet();
179
            $fields->push($optionsSet);
180
181
            $quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10;
182
            $count = 1;
183
            $quantity = array();
184
            while ($count <= $quantityMax) {
185
                $countVal = ProductPage::getGeneratedValue(
186
                    $this->product->Code,
187
                    'quantity',
188
                    $count,
189
                    'value'
190
                );
191
                $quantity[$countVal] = $count;
192
                ++$count;
193
            }
194
195
            $fields->push(DropdownField::create('quantity', 'Quantity', $quantity));
196
197
            $fields->push(HeaderField::create('submitPrice', '$'.$this->product->Price, 4)
0 ignored issues
show
Bug introduced by
4 of type integer is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

197
            $fields->push(HeaderField::create('submitPrice', '$'.$this->product->Price, /** @scrutinizer ignore-type */ 4)
Loading history...
198
                ->addExtraClass('submit-price'));
199
            $fields->push(HeaderField::create('unavailableText', 'Selection unavailable', 4)
200
                ->addExtraClass('hidden unavailable-text'));
201
202
            $this->extend('updatePurchaseFormFields', $fields);
203
        } else {
204
            $fields->push(HeaderField::create('submitPrice', 'Currently Out of Stock'), 4);
0 ignored issues
show
Unused Code introduced by
The call to SilverStripe\Forms\FieldList::push() has too many arguments starting with 4. ( Ignorable by Annotation )

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

204
            $fields->/** @scrutinizer ignore-call */ 
205
                     push(HeaderField::create('submitPrice', 'Currently Out of Stock'), 4);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
205
        }
206
207
        $this->extend('updateFoxyStripePurchaseFormFields', $fields);
208
209
        return $fields;
210
    }
211
212
    /**
213
     * @param FieldList $actions
214
     *
215
     * @return FieldList
216
     */
217
    protected function getProductActions(FieldList $actions)
218
    {
219
        $actions->push($submit = FormAction::create(
220
            '',
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

220
            /** @scrutinizer ignore-type */ '',
Loading history...
221
            _t('ProductForm.AddToCart', 'Add to Cart')
222
        ));
223
        $submit->setAttribute(
224
            'name',
225
            ProductPage::getGeneratedValue(
226
                $this->product->Code,
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist on Dynamic\FoxyStripe\Page\ProductPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
227
                'Submit',
228
                _t('ProductForm.AddToCart', 'Add to Cart')
229
            )
230
        );
231
        if (!$this->site_config->StoreName ||
232
            $this->site_config->StoreName == '' ||
233
            !isset($this->site_config->StoreName) ||
234
            !$this->product->Available
0 ignored issues
show
Bug Best Practice introduced by
The property Available does not exist on Dynamic\FoxyStripe\Page\ProductPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
235
        ) {
236
            $submit->setAttribute('Disabled', true);
237
        }
238
239
        $this->extend('updateFoxyStripePurchaseFormActions', $fields);
240
241
        return $actions;
242
    }
243
244
    /**
245
     * @return CompositeField
246
     */
247
    protected function getProductOptionSet()
248
    {
249
        $assignAvailable = function ($self) {
250
            $this->extend('updateFoxyStripePurchaseForm', $form);
251
            $self->Available = ($self->getAvailability()) ? true : false;
252
        };
253
254
        $options = $this->product->ProductOptions();
0 ignored issues
show
Bug introduced by
The method ProductOptions() does not exist on Dynamic\FoxyStripe\Page\ProductPage. 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

254
        /** @scrutinizer ignore-call */ 
255
        $options = $this->product->ProductOptions();
Loading history...
255
        $groupedOptions = new GroupedList($options);
256
        $groupedBy = $groupedOptions->groupBy('ProductOptionGroupID');
257
258
        $optionsSet = CompositeField::create();
259
260
        foreach ($groupedBy as $id => $set) {
261
            $group = OptionGroup::get()->byID($id);
262
            $title = $group->Title;
263
            $name = preg_replace('/\s/', '_', $title);
264
            $set->each($assignAvailable);
265
            $disabled = array();
266
            $fullOptions = array();
267
            foreach ($set as $item) {
268
                $fullOptions[ProductPage::getGeneratedValue(
269
                    $this->product->Code,
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist on Dynamic\FoxyStripe\Page\ProductPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
270
                    $group->Title,
271
                    $item->getGeneratedValue(),
272
                    'value'
273
                )] = $item->getGeneratedTitle();
274
                if (!$item->Availability) {
275
                    array_push(
276
                        $disabled,
277
                        ProductPage::getGeneratedValue(
278
                            $this->product->Code,
279
                            $group->Title,
280
                            $item->getGeneratedValue(),
281
                            'value'
282
                        )
283
                    );
284
                }
285
            }
286
            $optionsSet->push(
287
                $dropdown = DropdownField::create($name, $title, $fullOptions)->setTitle($title)
288
            );
289
            $dropdown->setDisabledItems($disabled);
290
        }
291
292
        $optionsSet->addExtraClass('foxycartOptionsContainer');
293
294
        return $optionsSet;
295
    }
296
}
297