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

DiscountHelper::getProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Dynamic\Foxy\Discounts;
4
5
use Dynamic\Foxy\Discounts\Model\Discount;
6
use Dynamic\Foxy\Discounts\Model\DiscountTier;
7
use Dynamic\Foxy\Model\ProductOption;
8
use SilverStripe\Core\Injector\Injectable;
9
10
/**
11
 * Class DiscountHelper
12
 * @package Dynamic\Foxy\Discounts
13
 */
14
class DiscountHelper
15
{
16
    use Injectable;
17
18
    /**
19
     * @var
20
     */
21
    private $product;
22
23
    /**
24
     * @var ProductOption
25
     */
26
    private $product_option;
27
28
    /**
29
     * @var Discount
30
     */
31
    private $discount;
32
33
    /**
34
     * @var
35
     */
36
    private $discounted_price;
37
38
    /**
39
     * @var DiscountTier
40
     */
41
    private $discount_tier;
42
43
    /**
44
     * DiscountHelper constructor.
45
     * @param $product
46
     * @param $discount
47
     * @param ProductOption|string|null $productOption
48
     */
49
    public function __construct($product, $discount, $productOption = null)
50
    {
51
        $this->setProduct($product);
52
        $this->setDiscount($discount);
53
54
        if ($productOption instanceof ProductOption || is_string($productOption)) {
55
            $this->setProductOption($productOption);
0 ignored issues
show
Bug introduced by
It seems like $productOption can also be of type string; however, parameter $productOption of Dynamic\Foxy\Discounts\D...per::setProductOption() does only seem to accept Dynamic\Foxy\Model\ProductOption, maybe add an additional type check? ( Ignorable by Annotation )

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

55
            $this->setProductOption(/** @scrutinizer ignore-type */ $productOption);
Loading history...
56
        }
57
58
        $this->setDiscountedPrice();
59
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    public function getProduct()
65
    {
66
        return $this->product;
67
    }
68
69
    /**
70
     * @param $product
71
     * @return $this
72
     */
73
    public function setProduct($product): self
74
    {
75
        $this->product = $product;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    public function getProductOption()
84
    {
85
        return $this->product_option;
86
    }
87
88
    /**
89
     * @param ProductOption $productOption
90
     * @return $this
91
     */
92
    public function setProductOption($productOption): self
93
    {
94
        $this->product_option = ($productOption instanceof ProductOption)
0 ignored issues
show
introduced by
$productOption is always a sub-type of Dynamic\Foxy\Model\ProductOption.
Loading history...
95
            ? $productOption
96
            : $this->getProduct()->Options()->filter('OptionModifierKey', $productOption)->first();
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104
    public function getDiscount()
105
    {
106
        return $this->discount;
107
    }
108
109
    /**
110
     * @param Discount $discount
111
     * @return $this
112
     */
113
    public function setDiscount(Discount $discount): self
114
    {
115
        $this->discount = $discount;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param int $quantity
122
     * @return $this
123
     */
124
    public function setDiscountTier($quantity = 1)
125
    {
126
        $this->discount_tier = $this->getDiscount()->DiscountTiers()
0 ignored issues
show
Bug introduced by
The method DiscountTiers() 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

126
        $this->discount_tier = $this->getDiscount()->/** @scrutinizer ignore-call */ DiscountTiers()
Loading history...
127
            ->filter('Quantity:LessThanOrEqual', $quantity)
128
            ->sort('Quantity DESC')->first();
129
130
        return $this;
131
    }
132
133
    /**
134
     * @return DiscountTier
135
     */
136
    public function getDiscountTier()
137
    {
138
        if (!$this->discount_tier) {
139
            $this->setDiscountTier();
140
        }
141
142
        return $this->discount_tier;
143
    }
144
145
    /**
146
     * @return float|int
147
     */
148
    public function setDiscountedPrice()
149
    {
150
        $price = ($this->getProductOption())
151
            ? $this->getProductOption()->getPrice($this->getProduct())
152
            : $this->getProduct()->Price;
153
154
        $tier = $this->getDiscountTier();
155
156
        $price = ($this->getDiscount()->Type == 'Percent')
0 ignored issues
show
Bug Best Practice introduced by
The property Type does not exist on Dynamic\Foxy\Discounts\Model\Discount. Since you implemented __get, consider adding a @property annotation.
Loading history...
157
            ? $price - ($price * ($tier->Percentage / 100))
0 ignored issues
show
Bug Best Practice introduced by
The property Percentage does not exist on Dynamic\Foxy\Discounts\Model\DiscountTier. Since you implemented __get, consider adding a @property annotation.
Loading history...
158
            : $price - $tier->Amount;
0 ignored issues
show
Bug Best Practice introduced by
The property Amount does not exist on Dynamic\Foxy\Discounts\Model\DiscountTier. Since you implemented __get, consider adding a @property annotation.
Loading history...
159
160
        return $this->discounted_price = $price;
161
    }
162
163
    /**
164
     * @return mixed
165
     */
166
    public function getDiscountedPrice()
167
    {
168
        if (!$this->discounted_price) {
169
            $this->setDiscountedPrice();
170
        }
171
172
        return $this->discounted_price;
173
    }
174
}
175