Plan::features()   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 Spatie\Sluggable\SlugOptions;
8
use Rinvex\Support\Traits\HasSlug;
9
use Spatie\EloquentSortable\Sortable;
10
use Illuminate\Database\Eloquent\Model;
11
use Rinvex\Support\Traits\HasTranslations;
12
use Rinvex\Support\Traits\ValidatingTrait;
13
use Spatie\EloquentSortable\SortableTrait;
14
use Illuminate\Database\Eloquent\SoftDeletes;
15
use Illuminate\Database\Eloquent\Relations\HasMany;
16
17
/**
18
 * Rinvex\Subscriptions\Models\Plan.
19
 *
20
 * @property int                 $id
21
 * @property string              $slug
22
 * @property array               $name
23
 * @property array               $description
24
 * @property bool                $is_active
25
 * @property float               $price
26
 * @property float               $signup_fee
27
 * @property string              $currency
28
 * @property int                 $trial_period
29
 * @property string              $trial_interval
30
 * @property int                 $invoice_period
31
 * @property string              $invoice_interval
32
 * @property int                 $grace_period
33
 * @property string              $grace_interval
34
 * @property int                 $prorate_day
35
 * @property int                 $prorate_period
36
 * @property int                 $prorate_extend_due
37
 * @property int                 $active_subscribers_limit
38
 * @property int                 $sort_order
39
 * @property \Carbon\Carbon|null $created_at
40
 * @property \Carbon\Carbon|null $updated_at
41
 * @property \Carbon\Carbon|null $deleted_at
42
 * @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Subscriptions\Models\PlanFeature[]      $features
43
 * @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Subscriptions\Models\PlanSubscription[] $subscriptions
44
 *
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan ordered($direction = 'asc')
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereActiveSubscribersLimit($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereCreatedAt($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereCurrency($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereDeletedAt($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereDescription($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereGraceInterval($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereGracePeriod($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereId($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereInvoiceInterval($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereInvoicePeriod($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereIsActive($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereName($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan wherePrice($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereProrateDay($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereProrateExtendDue($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereProratePeriod($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereSignupFee($value)
63
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereSlug($value)
64
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereSortOrder($value)
65
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereTrialInterval($value)
66
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereTrialPeriod($value)
67
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\Plan whereUpdatedAt($value)
68
 * @mixin \Eloquent
69
 */
70
class Plan extends Model implements Sortable
71
{
72
    use HasSlug;
73
    use SoftDeletes;
74
    use SortableTrait;
75
    use HasTranslations;
76
    use ValidatingTrait;
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected $fillable = [
82
        'slug',
83
        'name',
84
        'description',
85
        'is_active',
86
        'price',
87
        'signup_fee',
88
        'currency',
89
        'trial_period',
90
        'trial_interval',
91
        'invoice_period',
92
        'invoice_interval',
93
        'grace_period',
94
        'grace_interval',
95
        'prorate_day',
96
        'prorate_period',
97
        'prorate_extend_due',
98
        'active_subscribers_limit',
99
        'sort_order',
100
    ];
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    protected $casts = [
106
        'slug' => 'string',
107
        'is_active' => 'boolean',
108
        'price' => 'float',
109
        'signup_fee' => 'float',
110
        'currency' => 'string',
111
        'trial_period' => 'integer',
112
        'trial_interval' => 'string',
113
        'invoice_period' => 'integer',
114
        'invoice_interval' => 'string',
115
        'grace_period' => 'integer',
116
        'grace_interval' => 'string',
117
        'prorate_day' => 'integer',
118
        'prorate_period' => 'integer',
119
        'prorate_extend_due' => 'integer',
120
        'active_subscribers_limit' => 'integer',
121
        'sort_order' => 'integer',
122
        'deleted_at' => 'datetime',
123
    ];
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    protected $observables = [
129
        'validating',
130
        'validated',
131
    ];
132
133
    /**
134
     * The attributes that are translatable.
135
     *
136
     * @var array
137
     */
138
    public $translatable = [
139
        'name',
140
        'description',
141
    ];
142
143
    /**
144
     * The sortable settings.
145
     *
146
     * @var array
147
     */
148
    public $sortable = [
149
        'order_column_name' => 'sort_order',
150
    ];
151
152
    /**
153
     * The default rules that the model will validate against.
154
     *
155
     * @var array
156
     */
157
    protected $rules = [];
158
159
    /**
160
     * Whether the model should throw a
161
     * ValidationException if it fails validation.
162
     *
163
     * @var bool
164
     */
165
    protected $throwValidationExceptions = true;
166
167
    /**
168
     * Create a new Eloquent model instance.
169
     *
170
     * @param array $attributes
171
     */
172
    public function __construct(array $attributes = [])
173
    {
174
        parent::__construct($attributes);
175
176
        $this->setTable(config('rinvex.subscriptions.tables.plans'));
177
        $this->setRules([
178
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.subscriptions.tables.plans').',slug',
179
            'name' => 'required|string|strip_tags|max:150',
180
            'description' => 'nullable|string|max:32768',
181
            'is_active' => 'sometimes|boolean',
182
            'price' => 'required|numeric',
183
            'signup_fee' => 'required|numeric',
184
            'currency' => 'required|alpha|size:3',
185
            'trial_period' => 'sometimes|integer|max:100000',
186
            'trial_interval' => 'sometimes|in:hour,day,week,month',
187
            'invoice_period' => 'sometimes|integer|max:100000',
188
            'invoice_interval' => 'sometimes|in:hour,day,week,month',
189
            'grace_period' => 'sometimes|integer|max:100000',
190
            'grace_interval' => 'sometimes|in:hour,day,week,month',
191
            'sort_order' => 'nullable|integer|max:100000',
192
            'prorate_day' => 'nullable|integer|max:150',
193
            'prorate_period' => 'nullable|integer|max:150',
194
            'prorate_extend_due' => 'nullable|integer|max:150',
195
            'active_subscribers_limit' => 'nullable|integer|max:100000',
196
        ]);
197
    }
198
199
    /**
200
     * Get the options for generating the slug.
201
     *
202
     * @return \Spatie\Sluggable\SlugOptions
203
     */
204
    public function getSlugOptions(): SlugOptions
205
    {
206
        return SlugOptions::create()
207
                          ->doNotGenerateSlugsOnUpdate()
208
                          ->generateSlugsFrom('name')
209
                          ->saveSlugsTo('slug');
210
    }
211
212
    /**
213
     * The plan may have many features.
214
     *
215
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
216
     */
217
    public function features(): HasMany
218
    {
219
        return $this->hasMany(config('rinvex.subscriptions.models.plan_feature'), 'plan_id', 'id');
220
    }
221
222
    /**
223
     * The plan may have many subscriptions.
224
     *
225
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
226
     */
227
    public function subscriptions(): HasMany
228
    {
229
        return $this->hasMany(config('rinvex.subscriptions.models.plan_subscription'), 'plan_id', 'id');
230
    }
231
232
    /**
233
     * Check if plan is free.
234
     *
235
     * @return bool
236
     */
237
    public function isFree(): bool
238
    {
239
        return (float) $this->price <= 0.00;
240
    }
241
242
    /**
243
     * Check if plan has trial.
244
     *
245
     * @return bool
246
     */
247
    public function hasTrial(): bool
248
    {
249
        return $this->trial_period && $this->trial_interval;
250
    }
251
252
    /**
253
     * Check if plan has grace.
254
     *
255
     * @return bool
256
     */
257
    public function hasGrace(): bool
258
    {
259
        return $this->grace_period && $this->grace_interval;
260
    }
261
262
    /**
263
     * Get plan feature by the given slug.
264
     *
265
     * @param string $featureSlug
266
     *
267
     * @return \Rinvex\Subscriptions\Models\PlanFeature|null
268
     */
269
    public function getFeatureBySlug(string $featureSlug): ?PlanFeature
270
    {
271
        return $this->features()->where('slug', $featureSlug)->first();
272
    }
273
274
    /**
275
     * Activate the plan.
276
     *
277
     * @return $this
278
     */
279
    public function activate()
280
    {
281
        $this->update(['is_active' => true]);
282
283
        return $this;
284
    }
285
286
    /**
287
     * Deactivate the plan.
288
     *
289
     * @return $this
290
     */
291
    public function deactivate()
292
    {
293
        $this->update(['is_active' => false]);
294
295
        return $this;
296
    }
297
}
298