Completed
Push — master ( 679a17...e3cf70 )
by
unknown
02:19
created

DiscountsAvailableUntil()   C

Complexity

Conditions 9
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.412
c 0
b 0
f 0
cc 9
eloc 14
nc 4
nop 0
1
<?php
2
3
4
class DiscountCouponProductDataExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
7
8
    /**
9
     * stadard SS declaration
10
     * @var Array
11
     */
12
    private static $belongs_many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $belongs_many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
        "ApplicableDiscountCoupons" => "DiscountCouponOption"
14
    );
15
16
17
    /**
18
     * Update Fields
19
     * @return FieldList
20
     */
21
    public function updateCMSFields(FieldList $fields)
22
    {
23
        $fields->addFieldsToTab(
24
            'Root.Discount',
25
            GridField::create(
0 ignored issues
show
Documentation introduced by
\GridField::create('Appl...lationEditor::create()) is of type object<GridField>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26
                'ApplicableDiscountCoupons',
27
                'Discount Coupons',
28
                $this->owner->ApplicableDiscountCoupons(),
29
                GridFieldConfig_RelationEditor::create()
30
            )
31
        );
32
        return $fields;
33
    }
34
35
    /**
36
     * @param float $price
0 ignored issues
show
Documentation introduced by
Should the type for parameter $price not be double|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
37
     *
38
     * @return float | null
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
39
     */
40
    public function updateCalculatedPrice($price = null)
41
    {
42
        $hasDiscount = false;
43
        $coupons = $this->owner->DirectlyApplicableDiscountCoupons();
44
        if ($coupons && $coupons->count()) {
45
            $discountPercentage = 0;
46
            $discountAbsolute = 0;
47
            foreach ($coupons as $coupon) {
48
                if ($coupon->isValid()) {
49
                    $hasDiscount = true;
50
                    if ($coupon->DiscountPercentage > $discountPercentage) {
51
                        $discountPercentage = $coupon->DiscountPercentage;
52
                    }
53
                    if ($coupon->DiscountAbsolute > $discountAbsolute) {
54
                        $discountAbsolute = $coupon->DiscountAbsolute;
55
                    }
56
                }
57
            }
58
            if ($hasDiscount) {
59
                $priceWithPercentageDiscount = $price - ($price * ($discountPercentage / 100));
60
                $priceWithAbsoluteDiscount = $price - $discountAbsolute;
61
                if ($priceWithPercentageDiscount < $priceWithAbsoluteDiscount) {
62
                    return $priceWithPercentageDiscount;
63
                } else {
64
                    return $priceWithAbsoluteDiscount;
65
                }
66
            }
67
        }
68
    }
69
70
    public function DirectlyApplicableDiscountCoupons()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
71
    {
72
        return $this->owner->ApplicableDiscountCoupons()
73
            ->filter(array("ApplyPercentageToApplicableProducts" => 1, "ApplyEvenWithoutCode" => 1));
74
    }
75
76
    /**
77
     *
78
     * @return SS_Date
0 ignored issues
show
Documentation introduced by
Should the return type not be DBField|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
79
     */
80
    public function DiscountsAvailableUntil()
81
    {
82
        $coupons = $this->DirectlyApplicableDiscountCoupons();
83
        $next = strtotime('+100 years');
84
        if ($coupons && $coupons->count()) {
85
            $discount = 0;
86
            foreach ($coupons as $coupon) {
87
                if ($coupon->isValid()) {
88
                    if ($coupon->EndDate && $coupon->DiscountAbsolute > $discount) {
89
                        $discount = $coupon->DiscountAbsolute;
90
                        $maxDate = strtotime($coupon->EndDate);
91
                        if ($maxDate < $next) {
92
                            $next = $maxDate;
93
                        }
94
                    }
95
                }
96
            }
97
        }
98
        if ($next) {
99
            return DBField::create_field('Date', $next);
100
        }
101
    }
102
}
103