Passed
Pull Request — master (#5)
by Jason
01:54
created

PageControllerExtension::updateAddToCartForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 9.9666
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Extension;
4
5
use Dynamic\Foxy\Form\AddToCartForm;
6
use Dynamic\Foxy\Model\FoxyHelper;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\Forms\HiddenField;
9
10
class PageControllerExtension extends Extension
11
{
12
    /**
13
     * @param $form
14
     */
15
    public function updateAddToCartForm(&$form)
16
    {
17
        if ($this->owner->data()->getActiveDiscount()) {
18
            $code = $this->owner->data()->Code;
19
            $fields = $form->Fields();
20
            $fields->push(
21
                HiddenField::create(AddToCartForm::getGeneratedValue(
22
                    $code,
23
                    'discount_quantity_percentage',
24
                    $this->getDiscountFieldValue())
25
                )->setValue($this->getDiscountFieldValue()
26
            ));
27
        }
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    public function getDiscountFieldValue()
34
    {
35
        if ($discount = $this->owner->data()->getActiveDiscount()) {
36
            $string = "|{$discount->Quantity}-{$discount->Percentage}";
37
            return "{$discount->Title}{allunits{$string}}";
38
        }
39
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
40
    }
41
}
42