PlanFeature::getResetDate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Subscriptions\Models;
6
7
use Carbon\Carbon;
8
use Spatie\Sluggable\SlugOptions;
9
use Rinvex\Support\Traits\HasSlug;
10
use Spatie\EloquentSortable\Sortable;
11
use Illuminate\Database\Eloquent\Model;
12
use Rinvex\Subscriptions\Services\Period;
13
use Rinvex\Support\Traits\HasTranslations;
14
use Rinvex\Support\Traits\ValidatingTrait;
15
use Spatie\EloquentSortable\SortableTrait;
16
use Illuminate\Database\Eloquent\SoftDeletes;
17
use Rinvex\Subscriptions\Traits\BelongsToPlan;
18
use Illuminate\Database\Eloquent\Relations\HasMany;
19
20
/**
21
 * Rinvex\Subscriptions\Models\PlanFeature.
22
 *
23
 * @property int                 $id
24
 * @property int                 $plan_id
25
 * @property string              $slug
26
 * @property array               $title
27
 * @property array               $description
28
 * @property string              $value
29
 * @property int                 $resettable_period
30
 * @property string              $resettable_interval
31
 * @property int                 $sort_order
32
 * @property \Carbon\Carbon|null $created_at
33
 * @property \Carbon\Carbon|null $updated_at
34
 * @property \Carbon\Carbon|null $deleted_at
35
 * @property-read \Rinvex\Subscriptions\Models\Plan                                                             $plan
36
 * @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage[] $usage
37
 *
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature byPlanId($planId)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature ordered($direction = 'asc')
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereCreatedAt($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereDeletedAt($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereDescription($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereId($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereTitle($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature wherePlanId($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereResettableInterval($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereResettablePeriod($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereSlug($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereSortOrder($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereUpdatedAt($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereValue($value)
52
 * @mixin \Eloquent
53
 */
54
class PlanFeature extends Model implements Sortable
55
{
56
    use HasSlug;
57
    use SoftDeletes;
58
    use BelongsToPlan;
59
    use SortableTrait;
60
    use HasTranslations;
61
    use ValidatingTrait;
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected $fillable = [
67
        'plan_id',
68
        'slug',
69
        'name',
70
        'description',
71
        'value',
72
        'resettable_period',
73
        'resettable_interval',
74
        'sort_order',
75
    ];
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    protected $casts = [
81
        'plan_id' => 'integer',
82
        'slug' => 'string',
83
        'value' => 'string',
84
        'resettable_period' => 'integer',
85
        'resettable_interval' => 'string',
86
        'sort_order' => 'integer',
87
        'deleted_at' => 'datetime',
88
    ];
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected $observables = [
94
        'validating',
95
        'validated',
96
    ];
97
98
    /**
99
     * The attributes that are translatable.
100
     *
101
     * @var array
102
     */
103
    public $translatable = [
104
        'name',
105
        'description',
106
    ];
107
108
    /**
109
     * The sortable settings.
110
     *
111
     * @var array
112
     */
113
    public $sortable = [
114
        'order_column_name' => 'sort_order',
115
    ];
116
117
    /**
118
     * The default rules that the model will validate against.
119
     *
120
     * @var array
121
     */
122
    protected $rules = [];
123
124
    /**
125
     * Whether the model should throw a
126
     * ValidationException if it fails validation.
127
     *
128
     * @var bool
129
     */
130
    protected $throwValidationExceptions = true;
131
132
    /**
133
     * Create a new Eloquent model instance.
134
     *
135
     * @param array $attributes
136
     */
137
    public function __construct(array $attributes = [])
138
    {
139
        parent::__construct($attributes);
140
141
        $this->setTable(config('rinvex.subscriptions.tables.plan_features'));
142
        $this->setRules([
143
            'plan_id' => 'required|integer|exists:'.config('rinvex.subscriptions.tables.plans').',id',
144
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.subscriptions.tables.plan_features').',slug',
145
            'name' => 'required|string|strip_tags|max:150',
146
            'description' => 'nullable|string|max:32768',
147
            'value' => 'required|string',
148
            'resettable_period' => 'sometimes|integer',
149
            'resettable_interval' => 'sometimes|in:hour,day,week,month',
150
            'sort_order' => 'nullable|integer|max:100000',
151
        ]);
152
    }
153
154
    /**
155
     * Get the options for generating the slug.
156
     *
157
     * @return \Spatie\Sluggable\SlugOptions
158
     */
159
    public function getSlugOptions(): SlugOptions
160
    {
161
        return SlugOptions::create()
162
                          ->doNotGenerateSlugsOnUpdate()
163
                          ->generateSlugsFrom('name')
164
                          ->saveSlugsTo('slug');
165
    }
166
167
    /**
168
     * The plan feature may have many subscription usage.
169
     *
170
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
171
     */
172
    public function usage(): HasMany
173
    {
174
        return $this->hasMany(config('rinvex.subscriptions.models.plan_subscription_usage'), 'feature_id', 'id');
175
    }
176
177
    /**
178
     * Get feature's reset date.
179
     *
180
     * @param string $dateFrom
0 ignored issues
show
Documentation introduced by
Should the type for parameter $dateFrom not be Carbon?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
181
     *
182
     * @return \Carbon\Carbon
183
     */
184
    public function getResetDate(Carbon $dateFrom): Carbon
185
    {
186
        $period = new Period($this->resettable_interval, $this->resettable_period, $dateFrom ?? now());
187
188
        return $period->getEndDate();
189
    }
190
}
191