|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sagitarius29\LaravelSubscriptions; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\GroupContract; |
|
8
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract; |
|
9
|
|
|
use Sagitarius29\LaravelSubscriptions\Entities\Group; |
|
10
|
|
|
use Sagitarius29\LaravelSubscriptions\Exceptions\PlanErrorException; |
|
11
|
|
|
use Sagitarius29\LaravelSubscriptions\Traits\HasFeatures; |
|
12
|
|
|
|
|
13
|
|
|
abstract class Plan extends Model implements PlanContract |
|
14
|
|
|
{ |
|
15
|
|
|
use HasFeatures; |
|
16
|
|
|
|
|
17
|
|
|
protected $table = 'plans'; |
|
18
|
|
|
|
|
19
|
|
|
protected $fillable = [ |
|
20
|
|
|
'name', 'description', 'free_days', 'sort_order', 'is_enabled', 'is_default', 'group', |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $name |
|
25
|
|
|
* @param string $description |
|
26
|
|
|
* @param int $free_days |
|
|
|
|
|
|
27
|
|
|
* @param int $sort_order |
|
28
|
|
|
* @param bool $is_enabled |
|
29
|
|
|
* @param bool $is_default |
|
30
|
|
|
* @param GroupContract|null $group |
|
31
|
|
|
* @return Model|PlanContract |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function create( |
|
34
|
|
|
string $name, |
|
35
|
|
|
string $description, |
|
36
|
|
|
int $sort_order, |
|
37
|
|
|
bool $is_enabled = false, |
|
38
|
|
|
bool $is_default = false, |
|
39
|
|
|
GroupContract $group = null |
|
40
|
|
|
): PlanContract { |
|
41
|
|
|
$attributes = [ |
|
42
|
|
|
'name' => $name, |
|
43
|
|
|
'description' => $description, |
|
44
|
|
|
'sort_order' => $sort_order, |
|
45
|
|
|
'is_enabled' => $is_enabled, |
|
46
|
|
|
'is_default' => $is_default, |
|
47
|
|
|
'group' => $group, |
|
48
|
|
|
]; |
|
49
|
|
|
$calledClass = get_called_class(); |
|
50
|
|
|
|
|
51
|
|
|
if (! self::defaultExists($group)) { |
|
52
|
|
|
$attributes['is_default'] = true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$plan = new $calledClass($attributes); |
|
56
|
|
|
$plan->save(); |
|
57
|
|
|
|
|
58
|
|
|
return $plan; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private static function defaultExists(GroupContract $group = null): bool |
|
62
|
|
|
{ |
|
63
|
|
|
$calledClass = get_called_class(); |
|
64
|
|
|
|
|
65
|
|
|
return $calledClass::query() |
|
66
|
|
|
->byGroup($group) |
|
67
|
|
|
->isDefault() |
|
68
|
|
|
->exists(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function scopeByGroup(Builder $q, GroupContract $group = null) |
|
72
|
|
|
{ |
|
73
|
|
|
return $q->where('group', $group); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function scopeIsDefault(Builder $q) |
|
77
|
|
|
{ |
|
78
|
|
|
return $q->where('is_default', 1); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function scopeEnabled(Builder $q) |
|
82
|
|
|
{ |
|
83
|
|
|
return $q->where('is_enabled', 1); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function scopeDisabled(Builder $q) |
|
87
|
|
|
{ |
|
88
|
|
|
return $q->where('is_enabled', 1); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function isDefault(): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->is_default; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function isEnabled(): bool |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->is_enabled; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function isDisabled(): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return ! $this->is_enabled; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function isFree(): bool |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->intervals()->count() == 0 || $this->intervals()->first()->price == 0; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function intervals() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->hasMany(config('subscriptions.entities.plan_interval'), 'plan_id') |
|
114
|
|
|
->orderBy('price'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function isNotFree(): bool |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->intervals()->count() > 0 && $this->intervals()->first()->price > 0; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function hasManyIntervals(): bool |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->intervals()->count() > 1; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function setFree() |
|
128
|
|
|
{ |
|
129
|
|
|
$this->intervals()->delete(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function setDefault() |
|
133
|
|
|
{ |
|
134
|
|
|
$myGroup = $this->myGroup(); |
|
135
|
|
|
$calledClass = get_called_class(); |
|
136
|
|
|
|
|
137
|
|
|
$currentDefaults = $calledClass::query() |
|
138
|
|
|
->byGroup($myGroup) |
|
139
|
|
|
->isDefault() |
|
140
|
|
|
->get(); |
|
141
|
|
|
|
|
142
|
|
|
$currentDefaults->each(function ($plan) { |
|
143
|
|
|
$plan->is_default = false; |
|
144
|
|
|
$plan->save(); |
|
145
|
|
|
}); |
|
146
|
|
|
|
|
147
|
|
|
$this->is_default = true; |
|
148
|
|
|
$this->save(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function myGroup(): ?GroupContract |
|
152
|
|
|
{ |
|
153
|
|
|
return empty($this->group) ? null : new Group($this->group); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function changeToGroup(GroupContract $group): void |
|
157
|
|
|
{ |
|
158
|
|
|
$this->group = $group; |
|
159
|
|
|
|
|
160
|
|
|
if (! self::defaultExists($group)) { |
|
161
|
|
|
$this->is_default = true; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$this->save(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function getName() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->name; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function getDescription() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->description; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function delete() |
|
178
|
|
|
{ |
|
179
|
|
|
if ($this->subscriptions()->exists() > 0) { |
|
180
|
|
|
throw new PlanErrorException('You cannot delete this plan because this has subscriptions.'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return parent::delete(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function subscriptions() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->hasMany(config('subscriptions.entities.plan_subscription')); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.