Completed
Push — master ( 265e3e...091d56 )
by Jason
07:34
created

ProductDataExtension::getHasDiscount()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 7
c 2
b 0
f 1
nc 7
nop 0
dl 0
loc 14
rs 9.6111
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
        $merge = array_merge(array_values($strict->column()), array_values($global->column()));
51
52
        $this->discounts_list = (!empty($merge))
53
            ? Discount::get()->byIDs($merge)
54
            : null;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62
    private function getDiscountsList()
63
    {
64
        if (!$this->discounts_list) {
65
            $this->setDiscountsList();
66
        }
67
68
        return $this->discounts_list;
69
    }
70
71
    /**
72
     * @param int $quantity
73
     * @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...
74
     * @return $this
75
     */
76
    public function setBestDiscount($quantity = 1, $optionKey = null)
77
    {
78
        /** @var HasManyList $filtered */
79
        if ($filtered = $this->getDiscountsList()) {
80
            $filtered = $filtered->filter('DiscountTiers.Quantity:LessThanOrEqual', $quantity);
81
82
            if ($filtered->count() == 1) {
83
                $this->best_discount = DiscountHelper::create($this->owner, $filtered->first(), $optionKey);
84
85
                return $this;
86
            }
87
88
            $bestDiscount = null;
89
90
            /** @var Discount $discount */
91
            foreach ($filtered as $discount) {
92
                if ($bestDiscount === null) {
93
                    $bestDiscount = DiscountHelper::create($this->owner, $discount, $optionKey);
94
                    continue;
95
                }
96
97
                $testDiscount = DiscountHelper::create($this->owner, $discount, $optionKey);
98
99
                $bestDiscount = (float)$bestDiscount->getDiscountedPrice() > (float)$testDiscount->getDiscountedPrice()
100
                    ? $testDiscount
101
                    : $bestDiscount;
102
            }
103
104
            $this->best_discount = $bestDiscount;
105
        } else {
106
            $this->best_discount = null;
107
        }
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return DiscountHelper
114
     */
115
    public function getBestDiscount()
116
    {
117
        //if (!$this->best_discount) {
118
        $this->setBestDiscount();
119
120
        //}
121
122
        return $this->best_discount;
123
    }
124
125
    /**
126
     * @param int $quantity
127
     * @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...
128
     * @return bool|mixed
129
     */
130
    public function getDiscountPrice($quantity = 1, $optionKey = null)
0 ignored issues
show
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

130
    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...
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

130
    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...
131
    {
132
        if ($discount = $this->getBestDiscount()) {
133
            return $discount->getDiscountedPrice();
134
        }
135
136
        return false;
137
    }
138
139
    /**
140
     * @return bool
141
     */
142
    public function getHasDiscount()
143
    {
144
        if ($discount = $this->getBestDiscount()) {
145
            $discount = $discount->getDiscount();
146
147
            $restrictions = $discount->hasMethod('getRestrictions')
148
                ? $discount->getRestrictions()
0 ignored issues
show
Bug introduced by
The method getRestrictions() does not exist on Dynamic\Foxy\Discounts\Model\Discount. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

148
                ? $discount->/** @scrutinizer ignore-call */ getRestrictions()
Loading history...
149
                : [];
150
151
            return $this->getBestDiscount() instanceof DiscountHelper
152
                && (empty($restrictions) || in_array($this->owner->ID, $restrictions));
153
        }
154
155
        return false;
156
    }
157
}
158