Passed
Pull Request — master (#309)
by Jason
06:15 queued 01:51
created

FoxyStripePurchaseForm::setSiteConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
cc 3
eloc 5
nc 4
nop 1
crap 12
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
class FoxyStripePurchaseForm extends Form
14
{
15
    /**
16
     * @var
17
     */
18
    protected $site_config;
19
    /**
20
     * @var
21
     */
22
    private $product;
23
24
    /**
25
     * @param $siteConfig
26
     *
27
     * @return $this
28
     */
29
    public function setSiteConfig($siteConfig)
30
    {
31
        $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...
32
        if ($siteConfig instanceof SiteConfig) {
33
            $this->site_config = $siteConfig;
34
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
49
        return $this->site_config;
50
    }
51
52
    /**
53
     * @param $product
54
     *
55
     * @return $this
56
     */
57
    public function setProduct($product)
58
    {
59
        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...
60
            $this->product = $product;
61
62
            return $this;
63
        }
64
        throw new InvalidArgumentException('$product needs to be an instance of ProductPage.');
65
    }
66
67
    /**
68
     * @return ProductPage
69
     */
70
    public function getProduct()
71
    {
72
        return $this->product;
73
    }
74
75
    /**
76
     * FoxyStripePurchaseForm constructor.
77
     *
78
     * @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...
79
     * @param string         $name
80
     * @param FieldList|null $fields
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...
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...
81
     * @param FieldList|null $actions
82
     * @param null           $validator
83
     * @param null           $product
84
     * @param null           $siteConfig
85
     */
86
    public function __construct(
87
        $controller,
88
        $name,
89
        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...
90
        FieldList $actions = null,
91
        $validator = null,
92
        $product = null,
93
        $siteConfig = null
94
    ) {
95
        $this->setProduct($product);
96
        $this->setSiteConfig($siteConfig);
97
98
        $fields = ($fields != null && $fields->exists()) ? $this->getProductFields($fields) : $this->getProductFields(FieldList::create());
99
100
        $actions = ($actions != null && $actions->exists()) ? $this->getProductActions($actions) : $this->getProductActions(FieldList::create());
101
        $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...
102
103
        parent::__construct($controller, $name, $fields, $actions, $validator);
104
105
        //have to call after parent::__construct()
106
        $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...
107
        $this->disableSecurityToken();
108
    }
109
110
    /**
111
     * @param FieldList $fields
112
     *
113
     * @return FieldList
114
     */
115
    protected function getProductFields(FieldList $fields)
116
    {
117
        $hiddenTitle = ($this->product->ReceiptTitle) ? htmlspecialchars($this->product->ReceiptTitle) : htmlspecialchars($this->product->Title);
118
        $code = $this->product->Code;
119
120
        if ($this->product->Available) {
121
            $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...
122
                $hiddenTitle))->setValue($hiddenTitle));
123
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'category',
124
                $this->product->Category()->Code))->setValue($this->product->Category()->Code));
125
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'code',
126
                $this->product->Code))->setValue($this->product->Code));
127
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'product_id',
128
                $this->product->ID))->setValue($this->product->ID));
129
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'price',
130
                $this->product->Price))->setValue($this->product->Price));//can't override id
131
            $fields->push(HiddenField::create(ProductPage::getGeneratedValue($code, 'weight',
132
                $this->product->Weight))->setValue($this->product->Weight));
133
134
            if ($this->product->PreviewImage()->exists()) {
135
                $fields->push(
136
                    HiddenField::create(ProductPage::getGeneratedValue($code, 'image',
137
                        $this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL))
138
                        ->setValue($this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL)
139
                );
140
            }
141
142
            $optionsSet = $this->getProductOptionSet();
143
            $fields->push($optionsSet);
144
145
            $quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10;
146
            $count = 1;
147
            $quantity = array();
148
            while ($count <= $quantityMax) {
149
                $countVal = ProductPage::getGeneratedValue($this->product->Code, 'quantity', $count, 'value');
150
                $quantity[$countVal] = $count;
151
                ++$count;
152
            }
153
154
            $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...
155
156
            $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...
157
            $fields->push(HeaderField::create('unavailableText', 'Selection unavailable', 4)->addExtraClass('hidden unavailable-text'));
158
159
            $this->extend('updatePurchaseFormFields', $fields);
160
        } else {
161
            $fields->push(HeaderField::create('submitPrice', 'Currently Out of Stock'), 4);
162
        }
163
164
        $this->extend('updateFoxyStripePurchaseFormFields', $fields);
165
166
        return $fields;
167
    }
168
169
    /**
170
     * @param FieldList $actions
171
     *
172
     * @return FieldList
173
     */
174
    protected function getProductActions(FieldList $actions)
175
    {
176
        $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...
177
            '',
178
            _t('ProductForm.AddToCart', 'Add to Cart')
179
        ));
180
        $submit->setAttribute('name',
181
            ProductPage::getGeneratedValue($this->product->Code, 'Submit', _t('ProductForm.AddToCart', 'Add to Cart')));
182
        if (!$this->site_config->StoreName || $this->site_config->StoreName == '' || !isset($this->site_config->StoreName) || !$this->product->Available) {
183
            $submit->setAttribute('Disabled', true);
184
        }
185
186
        $this->extend('updateFoxyStripePurchaseFormActions', $fields);
187
188
        return $actions;
189
    }
190
191
    /**
192
     * @return CompositeField
193
     */
194
    protected function getProductOptionSet()
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