Completed
Push — master ( e4f576...f9bb8d )
by Abdelrahman
08:03
created

PlanSubscriptionUsage::feature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Subscriptions\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Rinvex\Cacheable\CacheableEloquent;
9
use Illuminate\Database\Eloquent\Builder;
10
use Rinvex\Support\Traits\ValidatingTrait;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
13
/**
14
 * Rinvex\Subscriptions\Models\PlanSubscriptionUsage.
15
 *
16
 * @property int                                               $id
17
 * @property int                                               $subscription_id
18
 * @property int                                               $feature_id
19
 * @property int                                               $used
20
 * @property \Carbon\Carbon                                    $valid_until
21
 * @property \Carbon\Carbon                                    $created_at
22
 * @property \Carbon\Carbon                                    $updated_at
23
 * @property \Carbon\Carbon                                    $deleted_at
24
 * @property-read \Rinvex\Subscriptions\Models\PlanFeature      $feature
25
 * @property-read \Rinvex\Subscriptions\Models\PlanSubscription $subscription
26
 *
27
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage byFeatureName($featureName)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
28
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereCreatedAt($value)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
29
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereDeletedAt($value)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
30
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereFeatureId($value)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
31
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereId($value)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
32
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereSubscriptionId($value)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereUpdatedAt($value)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereUsed($value)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereValidUntil($value)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
36
 * @mixin \Eloquent
37
 */
38
class PlanSubscriptionUsage extends Model
39
{
40
    use ValidatingTrait;
41
    use CacheableEloquent;
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected $fillable = [
47
        'subscription_id',
48
        'feature_id',
49
        'used',
50
        'valid_until',
51
    ];
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected $casts = [
57
        'subscription_id' => 'integer',
58
        'feature_id' => 'integer',
59
        'used' => 'integer',
60
        'valid_until' => 'datetime',
61
        'deleted_at' => 'datetime',
62
    ];
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected $observables = [
68
        'validating',
69
        'validated',
70
    ];
71
72
    /**
73
     * The default rules that the model will validate against.
74
     *
75
     * @var array
76
     */
77
    protected $rules = [];
78
79
    /**
80
     * Whether the model should throw a
81
     * ValidationException if it fails validation.
82
     *
83
     * @var bool
84
     */
85
    protected $throwValidationExceptions = true;
86
87
    /**
88
     * Create a new Eloquent model instance.
89
     *
90
     * @param array $attributes
91
     */
92
    public function __construct(array $attributes = [])
93
    {
94
        parent::__construct($attributes);
95
96
        $this->setTable(config('rinvex.subscriptions.tables.plan_subscription_usage'));
97
        $this->setRules([
98
            'subscription_id' => 'required|integer|exists:'.config('rinvex.subscriptions.tables.plan_subscriptions').',id',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
99
            'feature_id' => 'required|integer|exists:'.config('rinvex.subscriptions.tables.plan_features').',id',
100
            'used' => 'required|integer',
101
            'valid_until' => 'nullable|date',
102
        ]);
103
    }
104
105
    /**
106
     * Subscription usage always belongs to a plan feature.
107
     *
108
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
109
     */
110
    public function feature(): BelongsTo
111
    {
112
        return $this->belongsTo(config('rinvex.subscriptions.models.plan_feature'), 'feature_id', 'id');
113
    }
114
115
    /**
116
     * Subscription usage always belongs to a plan subscription.
117
     *
118
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
119
     */
120
    public function subscription(): BelongsTo
121
    {
122
        return $this->belongsTo(config('rinvex.subscriptions.models.plan_subscription'), 'subscription_id', 'id');
123
    }
124
125
    /**
126
     * Scope subscription usage by feature name.
127
     *
128
     * @param \Illuminate\Database\Eloquent\Builder $builder
129
     * @param string                                $featureName
130
     *
131
     * @return \Illuminate\Database\Eloquent\Builder
132
     */
133
    public function scopeByFeatureName(Builder $builder, string $featureName): Builder
134
    {
135
        $feature = PlanFeature::where('name', $featureName)->first();
136
137
        return $builder->where('feature_id', $feature->getKey() ?? null);
138
    }
139
140
    /**
141
     * Check whether usage has been expired or not.
142
     *
143
     * @return bool
144
     */
145
    public function expired(): bool
146
    {
147
        if (is_null($this->valid_until)) {
148
            return false;
149
        }
150
151
        return now()->gte($this->valid_until);
152
    }
153
}
154