PlanSubscriptionUsage::feature()   A
last analyzed

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 Carbon\Carbon;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Builder;
10
use Rinvex\Support\Traits\ValidatingTrait;
11
use Illuminate\Database\Eloquent\SoftDeletes;
12
use Illuminate\Database\Eloquent\Relations\BelongsTo;
13
14
/**
15
 * Rinvex\Subscriptions\Models\PlanSubscriptionUsage.
16
 *
17
 * @property int                 $id
18
 * @property int                 $subscription_id
19
 * @property int                 $feature_id
20
 * @property int                 $used
21
 * @property \Carbon\Carbon|null $valid_until
22
 * @property \Carbon\Carbon|null $created_at
23
 * @property \Carbon\Carbon|null $updated_at
24
 * @property \Carbon\Carbon|null $deleted_at
25
 * @property-read \Rinvex\Subscriptions\Models\PlanFeature      $feature
26
 * @property-read \Rinvex\Subscriptions\Models\PlanSubscription $subscription
27
 *
28
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage byFeatureSlug($featureSlug)
29
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereCreatedAt($value)
30
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereDeletedAt($value)
31
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereFeatureId($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereId($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereSubscriptionId($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereUpdatedAt($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereUsed($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereValidUntil($value)
37
 * @mixin \Eloquent
38
 */
39
class PlanSubscriptionUsage extends Model
40
{
41
    use SoftDeletes;
42
    use ValidatingTrait;
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected $fillable = [
48
        'subscription_id',
49
        'feature_id',
50
        'used',
51
        'valid_until',
52
    ];
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected $casts = [
58
        'subscription_id' => 'integer',
59
        'feature_id' => 'integer',
60
        'used' => 'integer',
61
        'valid_until' => 'datetime',
62
        'deleted_at' => 'datetime',
63
    ];
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected $observables = [
69
        'validating',
70
        'validated',
71
    ];
72
73
    /**
74
     * The default rules that the model will validate against.
75
     *
76
     * @var array
77
     */
78
    protected $rules = [];
79
80
    /**
81
     * Whether the model should throw a
82
     * ValidationException if it fails validation.
83
     *
84
     * @var bool
85
     */
86
    protected $throwValidationExceptions = true;
87
88
    /**
89
     * Create a new Eloquent model instance.
90
     *
91
     * @param array $attributes
92
     */
93
    public function __construct(array $attributes = [])
94
    {
95
        parent::__construct($attributes);
96
97
        $this->setTable(config('rinvex.subscriptions.tables.plan_subscription_usage'));
98
        $this->setRules([
99
            'subscription_id' => 'required|integer|exists:'.config('rinvex.subscriptions.tables.plan_subscriptions').',id',
100
            'feature_id' => 'required|integer|exists:'.config('rinvex.subscriptions.tables.plan_features').',id',
101
            'used' => 'required|integer',
102
            'valid_until' => 'nullable|date',
103
        ]);
104
    }
105
106
    /**
107
     * Subscription usage always belongs to a plan feature.
108
     *
109
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
110
     */
111
    public function feature(): BelongsTo
112
    {
113
        return $this->belongsTo(config('rinvex.subscriptions.models.plan_feature'), 'feature_id', 'id', 'feature');
114
    }
115
116
    /**
117
     * Subscription usage always belongs to a plan subscription.
118
     *
119
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
120
     */
121
    public function subscription(): BelongsTo
122
    {
123
        return $this->belongsTo(config('rinvex.subscriptions.models.plan_subscription'), 'subscription_id', 'id', 'subscription');
124
    }
125
126
    /**
127
     * Scope subscription usage by feature slug.
128
     *
129
     * @param \Illuminate\Database\Eloquent\Builder $builder
130
     * @param string                                $featureSlug
131
     *
132
     * @return \Illuminate\Database\Eloquent\Builder
133
     */
134
    public function scopeByFeatureSlug(Builder $builder, string $featureSlug): Builder
135
    {
136
        $feature = app('rinvex.subscriptions.plan_feature')->where('slug', $featureSlug)->first();
137
138
        return $builder->where('feature_id', $feature->getKey() ?? null);
139
    }
140
141
    /**
142
     * Check whether usage has been expired or not.
143
     *
144
     * @return bool
145
     */
146
    public function expired(): bool
147
    {
148
        if (is_null($this->valid_until)) {
149
            return false;
150
        }
151
152
        return Carbon::now()->gte($this->valid_until);
153
    }
154
}
155