22digital /
laravel-cashier-fastspring
| Conditions | 2 |
| Paths | 2 |
| Total Lines | 7 |
| Code Lines | 3 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 4 |
| CRAP Score | 2 |
| Changes | 0 | ||
| Metric | Value |
|---|---|
| eloc | 3 |
| c | 0 |
| b | 0 |
| f | 0 |
| dl | 0 |
| loc | 7 |
| ccs | 4 |
| cts | 4 |
| cp | 1 |
| rs | 10 |
| cc | 2 |
| nc | 2 |
| nop | 0 |
| crap | 2 |
| 1 | <?php |
||||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||||
| 2 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 3 | * This file implements a Subscription. |
||||||
| 4 | * |
||||||
| 5 | * @author Bilal Gultekin <[email protected]> |
||||||
|
0 ignored issues
–
show
|
|||||||
| 6 | * @author Justin Hartman <[email protected]> |
||||||
|
0 ignored issues
–
show
|
|||||||
| 7 | * @copyright 2019 22 Digital |
||||||
|
0 ignored issues
–
show
|
|||||||
| 8 | * @license MIT |
||||||
|
0 ignored issues
–
show
|
|||||||
| 9 | * @since v0.1 |
||||||
|
0 ignored issues
–
show
|
|||||||
| 10 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 11 | |||||||
| 12 | namespace TwentyTwoDigital\CashierFastspring; |
||||||
| 13 | |||||||
| 14 | use Carbon\Carbon; |
||||||
| 15 | use Exception; |
||||||
| 16 | use Illuminate\Database\Eloquent\Model; |
||||||
| 17 | use LogicException; |
||||||
| 18 | use TwentyTwoDigital\CashierFastspring\Fastspring\Fastspring; |
||||||
| 19 | |||||||
| 20 | /** |
||||||
| 21 | * This class describes a subscription. |
||||||
| 22 | * |
||||||
| 23 | * {@inheritdoc} |
||||||
| 24 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 25 | class Subscription extends Model |
||||||
| 26 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 27 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 28 | * The attributes that are not mass assignable. |
||||||
| 29 | * |
||||||
| 30 | * @var array |
||||||
| 31 | */ |
||||||
| 32 | protected $guarded = []; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 33 | |||||||
| 34 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 35 | * The attributes that should be mutated to dates. |
||||||
| 36 | * |
||||||
| 37 | * @var array |
||||||
| 38 | */ |
||||||
| 39 | protected $dates = [ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 40 | 'created_at', 'updated_at', 'swap_at', |
||||||
|
0 ignored issues
–
show
|
|||||||
| 41 | ]; |
||||||
| 42 | |||||||
| 43 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 44 | * The date on which the billing cycle should be anchored. |
||||||
| 45 | * |
||||||
| 46 | * @var string|null |
||||||
| 47 | */ |
||||||
| 48 | protected $billingCycleAnchor = null; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 49 | |||||||
| 50 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 51 | * Get the user that owns the subscription. |
||||||
| 52 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 53 | 1 | public function user() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 54 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 55 | 1 | return $this->owner(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 56 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 57 | |||||||
| 58 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 59 | * Get periods of the subscription. |
||||||
| 60 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 61 | 11 | public function periods() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 62 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 63 | 11 | return $this->hasMany('TwentyTwoDigital\CashierFastspring\SubscriptionPeriod'); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 64 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 65 | |||||||
| 66 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 67 | * Get active period of the subscription. |
||||||
| 68 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 69 | 6 | public function activePeriod() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 70 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 71 | 6 | return $this->hasOne('TwentyTwoDigital\CashierFastspring\SubscriptionPeriod') |
|||||
|
0 ignored issues
–
show
|
|||||||
| 72 | 6 | ->where('start_date', '<=', Carbon::now()->format('Y-m-d H:i:s')) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 73 | 6 | ->where('end_date', '>=', Carbon::now()->format('Y-m-d H:i:s')) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 74 | 6 | ->where('type', $this->type()); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 75 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 76 | |||||||
| 77 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 78 | * Get active period or retrieve the active period from fastspring and create. |
||||||
|
0 ignored issues
–
show
|
|||||||
| 79 | * |
||||||
| 80 | * Note: This is not eloquent relation, it returns SubscriptionPeriod model directly. |
||||||
|
0 ignored issues
–
show
|
|||||||
| 81 | * |
||||||
| 82 | * @return \TwentyTwoDigital\CashierFastspring\SubscriptionPeriod |
||||||
|
0 ignored issues
–
show
|
|||||||
| 83 | */ |
||||||
| 84 | 10 | public function activePeriodOrCreate() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 85 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 86 | 10 | if ($this->isFastspring()) { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 87 | 5 | return $this->activeFastspringPeriodOrCreate(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 88 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 89 | |||||||
| 90 | 5 | return $this->activeLocalPeriodOrCreate(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 91 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 92 | |||||||
| 93 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 94 | * Get active fastspring period or retrieve the active period from fastspring and create. |
||||||
|
0 ignored issues
–
show
|
|||||||
| 95 | * |
||||||
| 96 | * @return \TwentyTwoDigital\CashierFastspring\SubscriptionPeriod |
||||||
|
0 ignored issues
–
show
|
|||||||
| 97 | */ |
||||||
| 98 | 5 | public function activeFastspringPeriodOrCreate() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 99 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 100 | // activePeriod is not used on purpose |
||||||
|
0 ignored issues
–
show
|
|||||||
| 101 | // because it caches and causes confusion |
||||||
|
0 ignored issues
–
show
|
|||||||
| 102 | // after this method is called |
||||||
|
0 ignored issues
–
show
|
|||||||
| 103 | 5 | $today = Carbon::today()->format('Y-m-d'); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 104 | |||||||
| 105 | 5 | $activePeriod = SubscriptionPeriod::where('subscription_id', $this->id) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 106 | 5 | ->where('start_date', '<=', $today) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 107 | 5 | ->where('end_date', '>=', $today) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 108 | 5 | ->where('type', 'fastspring') |
|||||
|
0 ignored issues
–
show
|
|||||||
| 109 | 5 | ->first(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 110 | |||||||
| 111 | // if there is any return it |
||||||
|
0 ignored issues
–
show
|
|||||||
| 112 | 5 | if ($activePeriod) { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 113 | 2 | return $activePeriod; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 114 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 115 | |||||||
| 116 | 4 | return $this->createPeriodFromFastspring(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 117 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 118 | |||||||
| 119 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 120 | * Get active local period or create. |
||||||
| 121 | * |
||||||
| 122 | * @return \TwentyTwoDigital\CashierFastspring\SubscriptionPeriod |
||||||
|
0 ignored issues
–
show
|
|||||||
| 123 | */ |
||||||
| 124 | 5 | public function activeLocalPeriodOrCreate() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 125 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 126 | // activePeriod is not used on purpose |
||||||
|
0 ignored issues
–
show
|
|||||||
| 127 | // because it caches and causes confusion |
||||||
|
0 ignored issues
–
show
|
|||||||
| 128 | // after this method is called |
||||||
|
0 ignored issues
–
show
|
|||||||
| 129 | 5 | $today = Carbon::today()->format('Y-m-d'); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 130 | |||||||
| 131 | 5 | $activePeriod = SubscriptionPeriod::where('subscription_id', $this->id) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 132 | 5 | ->where('start_date', '<=', $today) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 133 | 5 | ->where('end_date', '>=', $today) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 134 | 5 | ->where('type', 'local') |
|||||
|
0 ignored issues
–
show
|
|||||||
| 135 | 5 | ->first(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 136 | |||||||
| 137 | // if there is any return it |
||||||
|
0 ignored issues
–
show
|
|||||||
| 138 | 5 | if ($activePeriod) { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 139 | return $activePeriod; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 140 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 141 | |||||||
| 142 | 5 | return $this->createPeriodLocally(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 143 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 144 | |||||||
| 145 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 146 | * Create period with the information from fastspring. |
||||||
| 147 | * |
||||||
| 148 | * @return \TwentyTwoDigital\CashierFastspring\SubscriptionPeriod |
||||||
|
0 ignored issues
–
show
|
|||||||
| 149 | */ |
||||||
| 150 | 4 | protected function createPeriodFromFastspring() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 151 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 152 | 4 | $response = Fastspring::getSubscriptionsEntries([$this->fastspring_id]); |
|||||
|
0 ignored issues
–
show
The method
getSubscriptionsEntries() does not exist on TwentyTwoDigital\Cashier...g\Fastspring\Fastspring. Since you implemented __callStatic, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 153 | |||||||
| 154 | $period = [ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 155 | // there is no info related to type in the entries endpoint |
||||||
|
0 ignored issues
–
show
|
|||||||
| 156 | // so we assume it is regular type |
||||||
| 157 | // because we create first periods (especially including trial if there is any) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 158 | // at the subscription creation |
||||||
|
0 ignored issues
–
show
|
|||||||
| 159 | 4 | 'type' => 'fastspring', |
|||||
|
0 ignored issues
–
show
|
|||||||
| 160 | |||||||
| 161 | // dates |
||||||
|
0 ignored issues
–
show
|
|||||||
| 162 | 4 | 'start_date' => $response[0]->beginPeriodDate, |
|||||
|
0 ignored issues
–
show
|
|||||||
| 163 | 4 | 'end_date' => $response[0]->endPeriodDate, |
|||||
|
0 ignored issues
–
show
|
|||||||
| 164 | 4 | 'subscription_id' => $this->id, |
|||||
|
0 ignored issues
–
show
|
|||||||
| 165 | ]; |
||||||
| 166 | |||||||
| 167 | // try to find or create |
||||||
|
0 ignored issues
–
show
|
|||||||
| 168 | 4 | return SubscriptionPeriod::firstOrCreate($period); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 169 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 170 | |||||||
| 171 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 172 | * Create period for non-fastspring/local subscriptions. |
||||||
| 173 | * |
||||||
| 174 | * Simply finds latest and add its dates $interval_length * $interval_unit |
||||||
| 175 | * If there is no subscription period, it creates a subscription period started today |
||||||
|
0 ignored issues
–
show
|
|||||||
| 176 | * |
||||||
| 177 | * @throws \Exception |
||||||
|
0 ignored issues
–
show
|
|||||||
| 178 | * |
||||||
| 179 | * @return \TwentyTwoDigital\CashierFastspring\SubscriptionPeriod |
||||||
|
0 ignored issues
–
show
|
|||||||
| 180 | */ |
||||||
| 181 | 5 | protected function createPeriodLocally() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 182 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 183 | 5 | $lastPeriod = $this->periods()->orderBy('end_date', 'desc')->first(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 184 | 5 | $today = Carbon::today(); |
|||||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||||||
| 185 | |||||||
| 186 | // there may be times subscriptionperiods not created more than |
||||||
|
0 ignored issues
–
show
|
|||||||
| 187 | // interval_length * interval_unit |
||||||
|
0 ignored issues
–
show
|
|||||||
| 188 | // For this kind of situations, we should fill the blank (actually we dont |
||||||
|
0 ignored issues
–
show
|
|||||||
| 189 | // have to but while we are calculating it is nice to save them) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 190 | do { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 191 | // add interval value to it to create next start_date |
||||||
|
0 ignored issues
–
show
|
|||||||
| 192 | // and sub one day to get next end_date |
||||||
|
0 ignored issues
–
show
|
|||||||
| 193 | 5 | switch ($this->interval_unit) { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 194 | // fastspring adds month without overflow |
||||||
|
0 ignored issues
–
show
|
|||||||
| 195 | // so lets we do the same |
||||||
|
0 ignored issues
–
show
|
|||||||
| 196 | 5 | case 'month': |
|||||
|
0 ignored issues
–
show
|
|||||||
| 197 | 2 | $start_date = $lastPeriod |
|||||
|
0 ignored issues
–
show
|
|||||||
| 198 | 1 | ? $lastPeriod->start_date->addMonthsNoOverflow($this->interval_length) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 199 | 2 | : Carbon::now(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 200 | |||||||
| 201 | 2 | $end_date = $start_date->copy()->addMonthsNoOverflow($this->interval_length)->subDay(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 202 | 2 | break; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 203 | |||||||
| 204 | 3 | case 'week': |
|||||
|
0 ignored issues
–
show
|
|||||||
| 205 | 1 | $start_date = $lastPeriod |
|||||
|
0 ignored issues
–
show
|
|||||||
| 206 | 1 | ? $lastPeriod->start_date->addWeeks($this->interval_length) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 207 | 1 | : Carbon::now(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 208 | |||||||
| 209 | 1 | $end_date = $start_date->copy()->addWeeks($this->interval_length)->subDay(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 210 | 1 | break; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 211 | |||||||
| 212 | // probably same thing with the year |
||||||
|
0 ignored issues
–
show
|
|||||||
| 213 | 2 | case 'year': |
|||||
|
0 ignored issues
–
show
|
|||||||
| 214 | 1 | $start_date = $lastPeriod |
|||||
|
0 ignored issues
–
show
|
|||||||
| 215 | 1 | ? $lastPeriod->start_date->addYearsNoOverflow($this->interval_length) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 216 | 1 | : Carbon::now(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 217 | |||||||
| 218 | 1 | $end_date = $start_date->copy()->addYearsNoOverflow($this->interval_length)->subDay(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 219 | 1 | break; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 220 | |||||||
| 221 | default: |
||||||
|
0 ignored issues
–
show
|
|||||||
| 222 | 1 | throw new Exception('Unexcepted interval unit: ' . $subscription->interval_unit); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 223 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 224 | |||||||
| 225 | $subscriptionPeriodData = [ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 226 | 4 | 'type' => 'local', |
|||||
|
0 ignored issues
–
show
|
|||||||
| 227 | 4 | 'start_date' => $start_date->format('Y-m-d'), |
|||||
|
0 ignored issues
–
show
|
|||||||
| 228 | 4 | 'end_date' => $end_date->format('Y-m-d'), |
|||||
|
0 ignored issues
–
show
|
|||||||
| 229 | 4 | 'subscription_id' => $this->id, |
|||||
|
0 ignored issues
–
show
|
|||||||
| 230 | ]; |
||||||
| 231 | |||||||
| 232 | 4 | $lastPeriod = SubscriptionPeriod::firstOrCreate($subscriptionPeriodData); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 233 | 4 | } while (!($today->greaterThanOrEqualTo($lastPeriod->start_date) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 234 | 4 | && $today->lessThanOrEqualTo($lastPeriod->end_date) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 235 | )); |
||||||
| 236 | |||||||
| 237 | 4 | return $lastPeriod; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 238 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 239 | |||||||
| 240 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 241 | * Get the model related to the subscription. |
||||||
| 242 | * |
||||||
| 243 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||||||
|
0 ignored issues
–
show
|
|||||||
| 244 | */ |
||||||
| 245 | 1 | public function owner() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 246 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 247 | 1 | $model = getenv('FASTSPRING_MODEL') ?: config('services.fastspring.model', 'App\\User'); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 248 | |||||||
| 249 | 1 | $model = new $model(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 250 | |||||||
| 251 | 1 | return $this->belongsTo(get_class($model), $model->getForeignKey()); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 252 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 253 | |||||||
| 254 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 255 | * Determine if the subscription is valid. |
||||||
| 256 | * This includes following states on fastspring: active, trial, overdue, canceled. |
||||||
|
0 ignored issues
–
show
|
|||||||
| 257 | * The only state that you should stop serving is deactivated state. |
||||||
|
0 ignored issues
–
show
|
|||||||
| 258 | * |
||||||
| 259 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 260 | */ |
||||||
| 261 | 5 | public function valid() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 262 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 263 | 5 | return !$this->deactivated(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 264 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 265 | |||||||
| 266 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 267 | * Determine if the subscription is active. |
||||||
| 268 | * |
||||||
| 269 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 270 | */ |
||||||
| 271 | 4 | public function active() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 272 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 273 | 4 | return $this->state == 'active'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 274 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 275 | |||||||
| 276 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 277 | * Determine if the subscription is deactivated. |
||||||
| 278 | * |
||||||
| 279 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 280 | */ |
||||||
| 281 | 5 | public function deactivated() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 282 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 283 | 5 | return $this->state == 'deactivated'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 284 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 285 | |||||||
| 286 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 287 | * Determine if the subscription is not paid and in wait. |
||||||
| 288 | * |
||||||
| 289 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 290 | */ |
||||||
| 291 | 4 | public function overdue() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 292 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 293 | 4 | return $this->state == 'overdue'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 294 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 295 | |||||||
| 296 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 297 | * Determine if the subscription is on trial. |
||||||
| 298 | * |
||||||
| 299 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 300 | */ |
||||||
| 301 | 5 | public function trial() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 302 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 303 | 5 | return $this->state == 'trial'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 304 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 305 | |||||||
| 306 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 307 | * Determine if the subscription is cancelled. |
||||||
| 308 | * |
||||||
| 309 | * Note: That doesn't mean you should stop serving. This state means |
||||||
| 310 | * user ordered to cancel at end of the billing period. |
||||||
| 311 | * Subscription is converted into deactivated on the start of next payment period, |
||||||
|
0 ignored issues
–
show
|
|||||||
| 312 | * after cancelling it. |
||||||
| 313 | * |
||||||
| 314 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 315 | */ |
||||||
| 316 | 7 | public function canceled() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 317 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 318 | 7 | return $this->state == 'canceled'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 319 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 320 | |||||||
| 321 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 322 | * ALIASES. |
||||||
| 323 | */ |
||||||
| 324 | |||||||
| 325 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 326 | * Alias of canceled. |
||||||
| 327 | * |
||||||
| 328 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 329 | */ |
||||||
| 330 | 2 | public function cancelled() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 331 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 332 | 2 | return $this->canceled(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 333 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 334 | |||||||
| 335 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 336 | * Determine if the subscription is within its trial period. |
||||||
| 337 | * |
||||||
| 338 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 339 | */ |
||||||
| 340 | 5 | public function onTrial() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 341 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 342 | 5 | return $this->trial(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 343 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 344 | |||||||
| 345 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 346 | * Determine if the subscription is within its grace period after cancellation. |
||||||
|
0 ignored issues
–
show
|
|||||||
| 347 | * |
||||||
| 348 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 349 | */ |
||||||
| 350 | 7 | public function onGracePeriod() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 351 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 352 | 7 | return $this->canceled(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 353 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 354 | |||||||
| 355 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 356 | * Determine type of the subscription: fastspring, local. |
||||||
| 357 | * |
||||||
| 358 | * @return string |
||||||
|
0 ignored issues
–
show
|
|||||||
| 359 | */ |
||||||
| 360 | 11 | public function type() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 361 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 362 | 11 | return $this->fastspring_id ? 'fastspring' : 'local'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 363 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 364 | |||||||
| 365 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 366 | * Determine if the subscription is local. |
||||||
| 367 | * |
||||||
| 368 | * @return bool |
||||||
|
0 ignored issues
–
show
|
|||||||
| 369 | */ |
||||||
| 370 | 1 | public function isLocal() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 371 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 372 | 1 | return $this->type() == 'local'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 373 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 374 | |||||||
| 375 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 376 | * Determine if the subscription is fastspring. |
||||||
| 377 | * |
||||||
| 378 | * @return string |
||||||
|
0 ignored issues
–
show
|
|||||||
| 379 | */ |
||||||
| 380 | 11 | public function isFastspring() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 381 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 382 | 11 | return $this->type() == 'fastspring'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 383 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 384 | |||||||
| 385 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 386 | * Swap the subscription to a new Fastspring plan. |
||||||
| 387 | * |
||||||
| 388 | * @param string $plan New plan |
||||||
|
0 ignored issues
–
show
|
|||||||
| 389 | * @param bool $prorate Prorate |
||||||
|
0 ignored issues
–
show
|
|||||||
| 390 | * @param int $quantity Quantity of the product |
||||||
|
0 ignored issues
–
show
|
|||||||
| 391 | * @param array $coupons Coupons wanted to be applied |
||||||
|
0 ignored issues
–
show
|
|||||||
| 392 | * |
||||||
| 393 | * @throws \Exception |
||||||
|
0 ignored issues
–
show
|
|||||||
| 394 | * |
||||||
| 395 | * @return object Response of fastspring |
||||||
|
0 ignored issues
–
show
|
|||||||
| 396 | */ |
||||||
| 397 | 2 | public function swap($plan, $prorate, $quantity = 1, $coupons = []) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 398 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 399 | 2 | $response = Fastspring::swapSubscription($this->fastspring_id, $plan, $prorate, $quantity, $coupons); |
|||||
|
0 ignored issues
–
show
The method
swapSubscription() does not exist on TwentyTwoDigital\Cashier...g\Fastspring\Fastspring. Since you implemented __callStatic, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 400 | 2 | $status = $response->subscriptions[0]; |
|||||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||||||
| 401 | |||||||
| 402 | 2 | if ($status->result == 'success') { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 403 | // we update subscription |
||||||
|
0 ignored issues
–
show
|
|||||||
| 404 | // according to prorate value |
||||||
|
0 ignored issues
–
show
|
|||||||
| 405 | 2 | if ($prorate) { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 406 | // if prorate is true |
||||||
|
0 ignored issues
–
show
|
|||||||
| 407 | // the plan is changed immediately |
||||||
|
0 ignored issues
–
show
|
|||||||
| 408 | // no need to fill swap columns |
||||||
|
0 ignored issues
–
show
|
|||||||
| 409 | |||||||
| 410 | // if the plan is in the trial state |
||||||
|
0 ignored issues
–
show
|
|||||||
| 411 | // then delete the current period |
||||||
|
0 ignored issues
–
show
|
|||||||
| 412 | // because it will change immediately |
||||||
|
0 ignored issues
–
show
|
|||||||
| 413 | // but period won't update because it exists |
||||||
|
0 ignored issues
–
show
|
|||||||
| 414 | 1 | if ($this->state == 'trial') { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 415 | $activePeriod = $this->activePeriodOrCreate(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 416 | $activePeriod->delete(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 417 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 418 | |||||||
| 419 | 1 | $this->plan = $plan; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 420 | 1 | $this->save(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 421 | } else { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 422 | // if prorate is false |
||||||
|
0 ignored issues
–
show
|
|||||||
| 423 | // save plan swap_to |
||||||
|
0 ignored issues
–
show
|
|||||||
| 424 | // because the plan will change after a while |
||||||
|
0 ignored issues
–
show
|
|||||||
| 425 | 1 | $activePeriod = $this->activePeriodOrCreate(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 426 | |||||||
| 427 | 1 | $this->swap_to = $plan; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 428 | 1 | $this->swap_at = $activePeriod |
|||||
|
0 ignored issues
–
show
|
|||||||
| 429 | 1 | ? $activePeriod->end_date |
|||||
|
0 ignored issues
–
show
|
|||||||
| 430 | : null; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 431 | 1 | $this->save(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 432 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 433 | |||||||
| 434 | 2 | return $this; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 435 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 436 | |||||||
| 437 | throw new Exception('Swap operation failed. Response: ' . json_encode($response)); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 438 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 439 | |||||||
| 440 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 441 | * Cancel the subscription at the end of the billing period. |
||||||
| 442 | * |
||||||
| 443 | * @throws \Exception |
||||||
|
0 ignored issues
–
show
|
|||||||
| 444 | * |
||||||
| 445 | * @return object Response of fastspring |
||||||
|
0 ignored issues
–
show
|
|||||||
| 446 | */ |
||||||
| 447 | 2 | public function cancel() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 448 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 449 | 2 | $response = Fastspring::cancelSubscription($this->fastspring_id); |
|||||
|
0 ignored issues
–
show
The method
cancelSubscription() does not exist on TwentyTwoDigital\Cashier...g\Fastspring\Fastspring. Since you implemented __callStatic, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||||||
| 450 | 2 | $status = $response->subscriptions[0]; |
|||||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||||||
| 451 | 2 | $activePeriod = $this->activePeriodOrCreate(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 452 | |||||||
| 453 | 2 | if ($status->result == 'success') { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 454 | 1 | $this->state = 'canceled'; |
|||||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||||||
| 455 | 1 | $this->swap_at = $activePeriod |
|||||
|
0 ignored issues
–
show
|
|||||||
| 456 | 1 | ? $activePeriod->end_date |
|||||
|
0 ignored issues
–
show
|
|||||||
| 457 | : null; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 458 | 1 | $this->save(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 459 | |||||||
| 460 | 1 | return $this; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 461 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 462 | |||||||
| 463 | 1 | throw new Exception('Cancel operation failed. Response: ' . json_encode($response)); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 464 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 465 | |||||||
| 466 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 467 | * Cancel the subscription immediately. |
||||||
| 468 | * |
||||||
| 469 | * @throws \Exception |
||||||
|
0 ignored issues
–
show
|
|||||||
| 470 | * |
||||||
| 471 | * @return object Response of fastspring |
||||||
|
0 ignored issues
–
show
|
|||||||
| 472 | */ |
||||||
| 473 | 2 | public function cancelNow() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 474 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 475 | 2 | $response = Fastspring::cancelSubscription($this->fastspring_id, ['billing_period' => 0]); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 476 | 2 | $status = $response->subscriptions[0]; |
|||||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||||||
| 477 | |||||||
| 478 | 2 | if ($status->result == 'success') { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 479 | // if it is canceled now |
||||||
|
0 ignored issues
–
show
|
|||||||
| 480 | // state should be deactivated |
||||||
|
0 ignored issues
–
show
|
|||||||
| 481 | 1 | $this->state = 'deactivated'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 482 | 1 | $this->save(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 483 | |||||||
| 484 | 1 | return $this; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 485 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 486 | |||||||
| 487 | 1 | throw new Exception('CancelNow operation failed. Response: ' . json_encode($response)); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 488 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 489 | |||||||
| 490 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 491 | * Resume the cancelled subscription. |
||||||
| 492 | * |
||||||
| 493 | * @throws \LogicException |
||||||
|
0 ignored issues
–
show
|
|||||||
| 494 | * @throws \Exception |
||||||
|
0 ignored issues
–
show
|
|||||||
| 495 | * |
||||||
| 496 | * @return object Response of fastspring |
||||||
|
0 ignored issues
–
show
|
|||||||
| 497 | */ |
||||||
| 498 | 3 | public function resume() |
|||||
|
0 ignored issues
–
show
|
|||||||
| 499 | { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 500 | 3 | if (!$this->onGracePeriod()) { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 501 | 1 | throw new LogicException('Unable to resume subscription that is not within grace period or not canceled.'); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 502 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 503 | |||||||
| 504 | 2 | $response = Fastspring::uncancelSubscription($this->fastspring_id); |
|||||
|
0 ignored issues
–
show
The method
uncancelSubscription() does not exist on TwentyTwoDigital\Cashier...g\Fastspring\Fastspring. Since you implemented __callStatic, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 505 | 2 | $status = $response->subscriptions[0]; |
|||||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||||||
| 506 | |||||||
| 507 | 2 | if ($status->result == 'success') { |
|||||
|
0 ignored issues
–
show
|
|||||||
| 508 | 1 | $this->state = 'active'; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 509 | |||||||
| 510 | // set null swap columns |
||||||
|
0 ignored issues
–
show
|
|||||||
| 511 | 1 | $this->swap_at = null; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 512 | 1 | $this->swap_to = null; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 513 | |||||||
| 514 | 1 | $this->save(); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 515 | |||||||
| 516 | 1 | return $this; |
|||||
|
0 ignored issues
–
show
|
|||||||
| 517 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 518 | |||||||
| 519 | 1 | throw new Exception('Resume operation failed. Response: ' . json_encode($response)); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 520 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 521 | } |
||||||
|
0 ignored issues
–
show
|
|||||||
| 522 |