Passed
Pull Request — master (#17)
by Nic
02:34
created

DiscountHelper::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Dynamic\Foxy\Discounts;
4
5
use Dynamic\Foxy\Discounts\Model\Discount;
6
use Dynamic\Foxy\Model\ProductOption;
7
use SilverStripe\Core\Injector\Injectable;
8
9
/**
10
 * Class DiscountHelper
11
 * @package Dynamic\Foxy\Discounts
12
 */
13
class DiscountHelper
14
{
15
    use Injectable;
16
17
    /**
18
     * @var
19
     */
20
    private $product;
21
22
    /**
23
     * @var ProductOption
24
     */
25
    private $product_option;
26
27
    /**
28
     * @var Discount
29
     */
30
    private $discount;
31
32
    /**
33
     * @var
34
     */
35
    private $discounted_price;
36
37
    public function __construct($product, $discount, $productOption = null)
38
    {
39
        $this->setProduct($product);
40
        $this->setDiscount($discount);
41
42
        if ($productOption instanceof ProductOption || is_string($productOption)) {
43
            $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

43
            $this->setProductOption(/** @scrutinizer ignore-type */ $productOption);
Loading history...
44
        }
45
46
        $this->setDiscountedPrice();
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function getProduct()
53
    {
54
        return $this->product;
55
    }
56
57
    /**
58
     * @param $product
59
     * @return $this
60
     */
61
    public function setProduct($product): self
62
    {
63
        $this->product = $product;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return mixed
70
     */
71
    public function getProductOption()
72
    {
73
        return $this->product_option;
74
    }
75
76
    /**
77
     * @param ProductOption $productOption
78
     * @return $this
79
     */
80
    public function setProductOption($productOption): self
81
    {
82
        $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...
83
            ? $productOption
84
            : $this->getProduct()->Options()->filter('OptionModifierKey', $productOption)->first();
85
86
        return $this;
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92
    public function getDiscount()
93
    {
94
        return $this->discount;
95
    }
96
97
    /**
98
     * @param Discount $discount
99
     * @return $this
100
     */
101
    public function setDiscount(Discount $discount): self
102
    {
103
        $this->discount = $discount;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return float|int
110
     */
111
    public function setDiscountedPrice()
112
    {
113
        $price = ($this->getProductOption())
114
            ? $this->getProductOption()->getPrice($this->getProduct())
115
            : $this->getProduct()->Price;
116
117
        $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

117
        $tier = $this->getDiscount()->/** @scrutinizer ignore-call */ DiscountTiers()
Loading history...
118
            ->filter('Quantity:LessThanOrEqual', 1)
119
            ->sort('Quantity DESC')->first();
120
121
        $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...
122
            ? $price - ($price * ($tier->Percentage/100))
123
            : $price - $tier->Amount;
124
125
        return $this->discounted_price = $price;
126
    }
127
128
    /**
129
     * @return mixed
130
     */
131
    public function getDiscountedPrice()
132
    {
133
        if (!$this->discounted_price) {
134
            $this->setDiscountedPrice();
135
        }
136
137
        return $this->discounted_price;
138
    }
139
}
140