Passed
Push — master ( 37d7f4...6bd508 )
by Nic
02:29
created

ProductDataExtension::getBestDiscount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Extension;
4
5
use Dynamic\Foxy\Discounts\DiscountHelper;
6
use Dynamic\Foxy\Discounts\Model\Discount;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\ORM\HasManyList;
9
10
/**
11
 * Class ProductDataExtension
12
 * @package Dynamic\Foxy\Discounts\Extension
13
 */
14
class ProductDataExtension extends DataExtension
15
{
16
    /**
17
     * @var
18
     */
19
    private $discounts_list;
20
21
    /**
22
     * @var DiscountHelper
23
     */
24
    private $best_discount;
25
26
    /**
27
     * @var array
28
     */
29
    private static $belongs_many_many = [
0 ignored issues
show
introduced by
The private property $belongs_many_many is not used, and could be removed.
Loading history...
30
        'Discounts' => Discount::class,
31
    ];
32
33
    /**
34
     * @return $this
35
     */
36
    private function setDiscountsList()
37
    {
38
        $list = Discount::get()->filter([
39
            'StartTime:LessThanOrEqual' => date("Y-m-d H:i:s", strtotime('now')),
40
            'EndTime:GreaterThanOrEqual' => date("Y-m-d H:i:s", strtotime('now')),
41
        ]);
42
43
        $strict = $list->filter([
44
            'Products.Count():GreaterThan' => 0,
45
            'Products.ID' => $this->owner->ID,
46
        ]);
47
48
        $global = $list->filter('Products.Count()', 0);
49
50
        $this->discounts_list = Discount::get()
51
            ->byIDs(array_merge(array_values($strict->column()), array_values($global->column())));
52
53
        return $this;
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59
    private function getDiscountsList()
60
    {
61
        if (!$this->discounts_list) {
62
            $this->setDiscountsList();
63
        }
64
65
        return $this->discounts_list;
66
    }
67
68
    /**
69
     * @param int $quantity
70
     * @param null $optionKey
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $optionKey is correct as it would always require null to be passed?
Loading history...
71
     * @return $this
72
     */
73
    public function setBestDiscount($quantity = 1, $optionKey = null)
74
    {
75
        /** @var HasManyList $filtered */
76
        $filtered = $this->getDiscountsList()->filter('DiscountTiers.Quantity:LessThanOrEqual', $quantity);
77
78
        if ($filtered->count() == 1) {
79
            $this->best_discount = DiscountHelper::create($this->owner, $filtered->first(), $optionKey);
80
81
            return $this;
82
        }
83
84
        $bestDiscount = null;
85
86
        /** @var Discount $discount */
87
        foreach ($filtered as $discount) {
88
            if ($bestDiscount === null) {
89
                $bestDiscount = DiscountHelper::create($this->owner, $discount, $optionKey);
90
                continue;
91
            }
92
93
            $testDiscount = DiscountHelper::create($this->owner, $discount, $optionKey);
94
95
            $bestDiscount = (float)$bestDiscount->getDiscountedPrice() > (float)$testDiscount->getDiscountedPrice()
96
                ? $testDiscount
97
                : $bestDiscount;
98
        }
99
100
        $this->best_discount = $bestDiscount;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @return DiscountHelper
107
     */
108
    public function getBestDiscount()
109
    {
110
        if (!$this->best_discount) {
111
            $this->setBestDiscount();
112
        }
113
114
        return $this->best_discount;
115
    }
116
117
    /**
118
     * @param int $quantity
119
     * @param null $optionKey
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $optionKey is correct as it would always require null to be passed?
Loading history...
120
     * @return bool|mixed
121
     */
122
    public function getDiscountPrice($quantity = 1, $optionKey = null)
0 ignored issues
show
Unused Code introduced by
The parameter $optionKey is not used and could be removed. ( Ignorable by Annotation )

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

122
    public function getDiscountPrice($quantity = 1, /** @scrutinizer ignore-unused */ $optionKey = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $quantity is not used and could be removed. ( Ignorable by Annotation )

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

122
    public function getDiscountPrice(/** @scrutinizer ignore-unused */ $quantity = 1, $optionKey = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
    {
124
        if ($discount = $this->getBestDiscount()) {
125
            return $discount->getDiscountedPrice();
126
        }
127
128
        return false;
129
    }
130
}
131