Passed
Pull Request — master (#309)
by Jason
13:47
created

FoxyStripePurchaseForm::getProductOptionSet()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.439
c 0
b 0
f 0
cc 5
eloc 27
nc 4
nop 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\Form;
4
5
use SilverStripe\Forms\Form;
6
7
/**
8
 * Class FoxyStripePurchaseForm
9
 *
10
 * @property SiteConfig $site_config
11
 * @property ProductPage $product
12
 *
13
 */
14
class FoxyStripePurchaseForm extends Form
15
{
16
17
    /**
18
     * @var
19
     */
20
    protected $site_config;
21
    /**
22
     * @var
23
     */
24
    private $product;
25
26
    /**
27
     * @param $siteConfig
28
     * @return $this
29
     */
30
    public function setSiteConfig($siteConfig)
31
    {
32
        $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...
33
        if ($siteConfig instanceof SiteConfig) {
34
            $this->site_config = $siteConfig;
35
            return $this;
36
        }
37
        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...
38
    }
39
40
    /**
41
     * @return SiteConfig
42
     */
43
    public function getSiteConfig()
44
    {
45
        if (!$this->site_config) {
46
            $this->setSiteConfig(SiteConfig::current_site_config());
47
        }
48
        return $this->site_config;
49
    }
50
51
    /**
52
     * @param $product
53
     * @return $this
54
     */
55
    public function setProduct($product)
56
    {
57
        if ($product instanceof ProductPage) {
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\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...
58
            $this->product = $product;
59
            return $this;
60
        }
61
        throw new InvalidArgumentException('$product needs to be an instance of ProductPage.');
62
    }
63
64
    /**
65
     * @return ProductPage
66
     */
67
    public function getProduct()
68
    {
69
        return $this->product;
70
    }
71
72
    /**
73
     * FoxyStripePurchaseForm constructor.
74
     * @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...
75
     * @param string $name
76
     * @param FieldList|null $fields
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...
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...
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...
77
     * @param FieldList|null $actions
78
     * @param null $validator
79
     * @param null $product
80
     * @param null $siteConfig
81
     */
82
    public function __construct(
83
        $controller,
84
        $name,
85
        FieldList $fields = null,
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\FieldList 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...
86
        FieldList $actions = null,
87
        $validator = null,
88
        $product = null,
89
        $siteConfig = null
90
    )
91
    {
92
        $this->setProduct($product);
93
        $this->setSiteConfig($siteConfig);
94
95
        $fields = ($fields != null && $fields->exists()) ? $this->getProductFields($fields) : $this->getProductFields(FieldList::create());
96
97
        $actions = ($actions != null && $actions->exists()) ? $this->getProductActions($actions) : $this->getProductActions(FieldList::create());
98
        $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...
99
100
        parent::__construct($controller, $name, $fields, $actions, $validator);
101
102
        //have to call after parent::__construct()
103
        $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...
104
        $this->disableSecurityToken();
105
106
    }
107
108
    /**
109
     * @param FieldList $fields
110
     * @return FieldList
111
     */
112
    protected function getProductFields(FieldList $fields)
113
    {
114
        $hiddenTitle = ($this->product->ReceiptTitle) ? htmlspecialchars($this->product->ReceiptTitle) : htmlspecialchars($this->product->Title);
115
        $code = $this->product->Code;
116
117
        if ($this->product->Available) {
118
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'name',
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\HiddenField 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...
119
                $hiddenTitle))->setValue($hiddenTitle));
120
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'category',
121
                $this->product->Category()->Code))->setValue($this->product->Category()->Code));
122
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'code',
123
                $this->product->Code))->setValue($this->product->Code));
124
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'product_id',
125
                $this->product->ID))->setValue($this->product->ID));
126
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'price',
127
                $this->product->Price))->setValue($this->product->Price));//can't override id
128
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'weight',
129
                $this->product->Weight))->setValue($this->product->Weight));
130
131
132
133
            if ($this->product->PreviewImage()->exists()) {
134
                $fields->push(
135
                    HiddenField::create(ProductPage::getGeneratedValue($code, 'image',
136
                        $this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL))
137
                        ->setValue($this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL)
138
                );
139
            }
140
141
            $optionsSet = $this->getProductOptionSet();
142
            $fields->push($optionsSet);
143
144
            $quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10;
145
            $count = 1;
146
            $quantity = array();
147
            while ($count <= $quantityMax) {
148
                $countVal = ProductPage::getGeneratedValue($this->product->Code, 'quantity', $count, 'value');
149
                $quantity[$countVal] = $count;
150
                $count++;
151
            }
152
153
            $fields->push(DropdownField::create('quantity', 'Quantity', $quantity));
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\DropdownField 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...
154
155
            $fields->push(HeaderField::create('submitPrice', '$' . $this->product->Price, 4)->addExtraClass('submit-price'));
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\HeaderField 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...
156
            $fields->push(HeaderField::create('unavailableText', 'Selection unavailable', 4)->addExtraClass('hidden unavailable-text'));
157
158
            $this->extend('updatePurchaseFormFields', $fields);
159
        } else {
160
            $fields->push(HeaderField::create('submitPrice', 'Currently Out of Stock'), 4);
161
        }
162
163
        $this->extend('updateFoxyStripePurchaseFormFields', $fields);
164
165
        return $fields;
166
    }
167
168
    /**
169
     * @param FieldList $actions
170
     * @return FieldList
171
     */
172
    protected function getProductActions(FieldList $actions)
173
    {
174
175
        $actions->push($submit = FormAction::create(
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\FormAction 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...
176
            '',
177
            _t('ProductForm.AddToCart', 'Add to Cart')
178
        ));
179
        $submit->setAttribute('name',
180
            ProductPage::getGeneratedValue($this->product->Code, 'Submit', _t('ProductForm.AddToCart', 'Add to Cart')));
181
        if (!$this->site_config->StoreName || $this->site_config->StoreName == '' || !isset($this->site_config->StoreName) || !$this->product->Available) {
182
            $submit->setAttribute('Disabled', true);
183
        }
184
185
        $this->extend('updateFoxyStripePurchaseFormActions', $fields);
186
187
        return $actions;
188
    }
189
190
    /**
191
     * @return CompositeField
192
     */
193
    protected function getProductOptionSet()
194
    {
195
196
        $assignAvailable = function ($self) {
197
            $this->extend('updateFoxyStripePurchaseForm', $form);
198
            $self->Available = ($self->getAvailability()) ? true : false;
199
        };
200
201
        $options = $this->product->ProductOptions();
202
        $groupedOptions = new GroupedList($options);
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\GroupedList 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...
203
        $groupedBy = $groupedOptions->groupBy('ProductOptionGroupID');
204
205
        $optionsSet = CompositeField::create();
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\CompositeField 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...
206
207
        foreach ($groupedBy as $id => $set) {
208
            $group = OptionGroup::get()->byID($id);
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\OptionGroup 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...
209
            $title = $group->Title;
210
            $name = preg_replace('/\s/', '_', $title);
211
            $set->each($assignAvailable);
212
            $disabled = array();
213
            $fullOptions = array();
214
            foreach ($set as $item) {
215
                $fullOptions[ProductPage::getGeneratedValue($this->product->Code, $group->Title,
216
                    $item->getGeneratedValue(),
217
                    'value')] = $item->getGeneratedTitle();
218
                if (!$item->Availability) {
219
                    array_push($disabled,
220
                        ProductPage::getGeneratedValue($this->product->Code, $group->Title, $item->getGeneratedValue(),
221
                            'value'));
222
                }
223
            }
224
            $optionsSet->push(
225
                $dropdown = DropdownField::create($name, $title, $fullOptions)->setTitle($title)
226
            );
227
            $dropdown->setDisabledItems($disabled);
228
        }
229
230
        $optionsSet->addExtraClass('foxycartOptionsContainer');
231
232
        return $optionsSet;
233
    }
234
235
}