Completed
Push — master ( 76cc03...7ee247 )
by Zura
08:07
created

Promocode::isExpired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
118
    }
119
120
    /**
121
     * Check if code is expired.
122
     *
123
     * @return bool
124
     */
125
    public function isExpired()
126
    {
127
        return $this->expires_at ? Carbon::now()->gte($this->expires_at) : false;
0 ignored issues
show
Documentation introduced by
The property expires_at does not exist on object<Gabievi\Promocodes\Model\Promocode>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
128
    }
129
}
130