CouponTrait::setDiscountOnItem()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
namespace LukePOLO\LaraCart\Traits;
4
5
use Carbon\Carbon;
6
use LukePOLO\LaraCart\CartItem;
7
use LukePOLO\LaraCart\Exceptions\CouponException;
8
use LukePOLO\LaraCart\Exceptions\InvalidPrice;
9
use LukePOLO\LaraCart\LaraCart;
10
11
/**
12
 * Class CouponTrait.
13
 */
14
trait CouponTrait
15
{
16
    /**
17
     * @var bool
18
     */
19
    public $appliedToCart = true;
20
21
    use CartOptionsMagicMethodsTrait;
22
23
    /**
24
     * Sets all the options for the coupon.
25
     *
26
     * @param $options
27
     */
28
    public function setOptions($options)
29
    {
30
        foreach ($options as $key => $value) {
31
            $this->$key = $value;
32
        }
33
    }
34
35
    /**
36
     * Checks to see if we can apply the coupon.
37
     *
38
     * @return bool
39
     */
40
    public function canApply()
41
    {
42
        try {
43
            $this->discount(true);
0 ignored issues
show
Bug introduced by
The method discount() does not exist on LukePOLO\LaraCart\Traits\CouponTrait. Did you maybe mean maxDiscount()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
45
            return true;
46
        } catch (CouponException $e) {
47
            return false;
48
        }
49
    }
50
51
    /**
52
     * Get the reason why a coupon has failed to apply.
53
     *
54
     * @deprecated 1.3
55
     *
56
     * @return string
57
     */
58
    public function getMessage()
59
    {
60
        try {
61
            $this->discount(true);
0 ignored issues
show
Bug introduced by
The method discount() does not exist on LukePOLO\LaraCart\Traits\CouponTrait. Did you maybe mean maxDiscount()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
63
            return config('laracart.coupon_applied_message', 'Coupon Applied');
64
        } catch (CouponException $e) {
65
            return $e->getMessage();
66
        }
67
    }
68
69
    /**
70
     * Gets the failed message for a coupon.
71
     *
72
     * @return null|string
73
     */
74
    public function getFailedMessage()
75
    {
76
        try {
77
            $this->discount(true);
0 ignored issues
show
Bug introduced by
The method discount() does not exist on LukePOLO\LaraCart\Traits\CouponTrait. Did you maybe mean maxDiscount()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
78
        } catch (CouponException $e) {
79
            return $e->getMessage();
80
        }
81
    }
82
83
    /**
84
     * Checks the minimum subtotal needed to apply the coupon.
85
     *
86
     * @param $minAmount
87
     * @param $throwErrors
88
     *
89
     * @throws CouponException
90
     *
91
     * @return bool
92
     */
93
    public function checkMinAmount($minAmount, $throwErrors = true)
94
    {
95
        $laraCart = \App::make(LaraCart::SERVICE);
96
97
        if ($laraCart->subTotal(false, false, false) >= $minAmount) {
98
            return true;
99
        } else {
100
            if ($throwErrors) {
101
                throw new CouponException('You must have at least a total of '.$laraCart->formatMoney($minAmount));
102
            } else {
103
                return false;
104
            }
105
        }
106
    }
107
108
    /**
109
     * Returns either the max discount or the discount applied based on what is passed through.
110
     *
111
     * @param $maxDiscount
112
     * @param $discount
113
     * @param $throwErrors
114
     *
115
     * @throws CouponException
116
     *
117
     * @return mixed
118
     */
119
    public function maxDiscount($maxDiscount, $discount, $throwErrors = true)
120
    {
121
        if ($maxDiscount == 0 || $maxDiscount > $discount) {
122
            return $discount;
123
        } else {
124
            if ($throwErrors) {
125
                throw new CouponException('This has a max discount of '.\App::make(\LukePOLO\Laracart\LaraCart::SERVICE)->formatMoney($maxDiscount));
126
            } else {
127
                return $maxDiscount;
128
            }
129
        }
130
    }
131
132
    /**
133
     * Checks to see if the times are valid for the coupon.
134
     *
135
     * @param Carbon $startDate
136
     * @param Carbon $endDate
137
     * @param $throwErrors
138
     *
139
     * @throws CouponException
140
     *
141
     * @return bool
142
     */
143
    public function checkValidTimes(Carbon $startDate, Carbon $endDate, $throwErrors = true)
144
    {
145
        if (Carbon::now()->between($startDate, $endDate)) {
146
            return true;
147
        } else {
148
            if ($throwErrors) {
149
                throw new CouponException('This coupon has expired');
150
            } else {
151
                return false;
152
            }
153
        }
154
    }
155
156
    /**
157
     * Sets a discount to an item with what code was used and the discount amount.
158
     *
159
     * @param CartItem $item
160
     * @param $discountAmount
161
     *
162
     * @throws InvalidPrice
163
     */
164
    public function setDiscountOnItem(CartItem $item, $discountAmount)
165
    {
166
        if (!is_numeric($discountAmount)) {
167
            throw new InvalidPrice('You must use a discount amount.');
168
        }
169
        $this->appliedToCart = false;
170
        $item->code = $this->code;
0 ignored issues
show
Documentation introduced by
The property code does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property code does not exist on object<LukePOLO\LaraCart\Traits\CouponTrait>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
171
        $item->discount = $discountAmount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $discountAmount can also be of type double or string. However, the property $discount is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
172
        $item->couponInfo = $this->options;
173
    }
174
}
175