Completed
Push — master ( 173198...82f4fc )
by Zura
07:18
created

Promocode::isOverAmount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Gabievi\Promocodes\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * @method static byCode(string $code)
10
 * @method static pluck(string $string)
11
 * @method static insert(array $records)
12
 */
13
class Promocode extends Model
14
{
15
    /**
16
     * Indicates if the model should be timestamped.
17
     *
18
     * @var bool
19
     */
20
    public $timestamps = false;
21
22
    /**
23
     * The attributes that are mass assignable.
24
     *
25
     * @var array
26
     */
27
    protected $fillable = ['code', 'reward', 'is_disposable', 'expires_at', 'quantity'];
28
29
    /**
30
     * The attributes that should be cast to native types.
31
     *
32
     * @var array
33
     */
34
    protected $casts = [
35
        'is_disposable' => 'boolean',
36
        'data' => 'array',
37
        'quantity' => 'integer'
38
    ];
39
40
    /**
41
     * The attributes that should be mutated to dates.
42
     *
43
     * @var array
44
     */
45
    protected $dates = ['expires_at'];
46
47
    /**
48
     * Promocode constructor.
49
     *
50
     * @param array $attributes
51
     */
52
    public function __construct(array $attributes = [])
53
    {
54
        parent::__construct($attributes);
55
56
        $this->table = config('promocodes.table', 'promocodes');
57
    }
58
59
    /**
60
     * Get the users who is related promocode.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
63
     */
64
    public function users()
65
    {
66
        return $this->belongsToMany(config('promocodes.user_model'), config('promocodes.relation_table'),
67
            config('promocodes.foreign_pivot_key', 'user_id'), config('promocodes.related_pivot_key', 'user_id'))
68
            ->withPivot('used_at');
69
    }
70
71
    /**
72
     * Query builder to find promocode using code.
73
     *
74
     * @param $query
75
     * @param $code
76
     *
77
     * @return mixed
78
     */
79
    public function scopeByCode($query, $code)
80
    {
81
        return $query->where('code', $code);
82
    }
83
84
    /**
85
     * Query builder to get disposable codes.
86
     *
87
     * @param $query
88
     * @return mixed
89
     */
90
    public function scopeIsDisposable($query)
91
    {
92
        return $query->where('is_disposable', true);
93
    }
94
95
    /**
96
     * Query builder to get non-disposable codes.
97
     *
98
     * @param $query
99
     * @return mixed
100
     */
101
    public function scopeIsNotDisposable($query)
102
    {
103
        return $query->where('is_disposable', false);
104
    }
105
106
    /**
107
     * Query builder to get expired promotion codes.
108
     *
109
     * @param $query
110
     * @return mixed
111
     */
112
    public function scopeExpired($query)
113
    {
114
        return $query->whereNotNull('expires_at')->whereDate('expires_at', '<=', Carbon::now());
115
    }
116
117
    /**
118
     * Check if code is disposable (ont-time).
119
     *
120
     * @return bool
121
     */
122
    public function isDisposable()
123
    {
124
        return $this->is_disposable;
125
    }
126
127
    /**
128
     * Check if code is expired.
129
     *
130
     * @return bool
131
     */
132
    public function isExpired()
133
    {
134
        return $this->expires_at ? Carbon::now()->gte($this->expires_at) : false;
0 ignored issues
show
Bug introduced by
The method gte does only exist in Carbon\CarbonInterface, but not in Carbon\Traits\Creator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
135
    }
136
137
    /**
138
     * Check if code amount is over.
139
     *
140
     * @return bool
141
     */
142
    public function isOverAmount()
143
    {
144
        if (is_null($this->quantity)) {
145
            return false;
146
        }
147
148
        return $this->quantity <= 0;
149
    }
150
}
151