dev-think-one /
laravel-cashier-subscription-pause
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace CashierSubscriptionPause\Eloquent; |
||||||
| 4 | |||||||
| 5 | use Carbon\Carbon; |
||||||
| 6 | use Illuminate\Database\Eloquent\Builder; |
||||||
| 7 | use LogicException; |
||||||
| 8 | |||||||
| 9 | /** |
||||||
| 10 | * @property array|null $pause_collection |
||||||
| 11 | * @extends WithPauseCollection |
||||||
| 12 | */ |
||||||
| 13 | trait UsesPauseCollection |
||||||
| 14 | { |
||||||
| 15 | /** |
||||||
| 16 | * @inerhitDoc |
||||||
| 17 | */ |
||||||
| 18 | 2 | public function pause(string $behavior, ?Carbon $resumesAt = null): static |
|||||
| 19 | { |
||||||
| 20 | 2 | if ($this->canceled()) { |
|||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 21 | 1 | throw new LogicException('Unable to pause subscription that is canceled.'); |
|||||
| 22 | } |
||||||
| 23 | |||||||
| 24 | 1 | $payload = [ 'behavior' => $behavior ]; |
|||||
| 25 | 1 | if ($resumesAt) { |
|||||
| 26 | 1 | $payload['resumes_at'] = $resumesAt->timestamp; |
|||||
| 27 | } |
||||||
| 28 | 1 | $stripeSubscription = $this->updateStripeSubscription([ 'pause_collection' => $payload, ]); |
|||||
|
0 ignored issues
–
show
It seems like
updateStripeSubscription() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 29 | |||||||
| 30 | 1 | $this->fill([ |
|||||
|
0 ignored issues
–
show
It seems like
fill() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 31 | 1 | 'pause_collection' => $stripeSubscription->pause_collection, |
|||||
| 32 | 1 | ])->save(); |
|||||
| 33 | |||||||
| 34 | 1 | return $this; |
|||||
| 35 | } |
||||||
| 36 | |||||||
| 37 | /** |
||||||
| 38 | * @inerhitDoc |
||||||
| 39 | */ |
||||||
| 40 | 1 | public function pauseBehaviorMarkUncollectible(?Carbon $resumesAt = null): static |
|||||
| 41 | { |
||||||
| 42 | 1 | return $this->pause(WithPauseCollection::BEHAVIOR_MARK_UNCOLLECTIBLE, $resumesAt); |
|||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | /** |
||||||
| 46 | * @inerhitDoc |
||||||
| 47 | */ |
||||||
| 48 | 2 | public function pauseBehaviorKeepAsDraft(?Carbon $resumesAt = null): static |
|||||
| 49 | { |
||||||
| 50 | 2 | return $this->pause(WithPauseCollection::BEHAVIOR_KEEP_AS_DRAFT, $resumesAt); |
|||||
| 51 | } |
||||||
| 52 | |||||||
| 53 | /** |
||||||
| 54 | * @inerhitDoc |
||||||
| 55 | */ |
||||||
| 56 | 1 | public function pauseBehaviorVoid(?Carbon $resumesAt = null): static |
|||||
| 57 | { |
||||||
| 58 | 1 | return $this->pause(WithPauseCollection::BEHAVIOR_VOID, $resumesAt); |
|||||
| 59 | } |
||||||
| 60 | |||||||
| 61 | /** |
||||||
| 62 | * @inerhitDoc |
||||||
| 63 | */ |
||||||
| 64 | 2 | public function unpause(): static |
|||||
| 65 | { |
||||||
| 66 | 2 | if (!$this->paused()) { |
|||||
| 67 | 1 | throw new LogicException('Unable to unpause subscription that is not paused.'); |
|||||
| 68 | } |
||||||
| 69 | |||||||
| 70 | 1 | $stripeSubscription = $this->updateStripeSubscription([ 'pause_collection' => '', ]); |
|||||
| 71 | |||||||
| 72 | 1 | $this->fill([ |
|||||
| 73 | 1 | 'pause_collection' => $stripeSubscription->pause_collection, |
|||||
| 74 | 1 | ])->save(); |
|||||
| 75 | |||||||
| 76 | 1 | return $this; |
|||||
| 77 | } |
||||||
| 78 | |||||||
| 79 | /** |
||||||
| 80 | * @inerhitDoc |
||||||
| 81 | */ |
||||||
| 82 | 1 | public function syncStripePauseCollection(): static |
|||||
| 83 | { |
||||||
| 84 | 1 | if($this->owner) { |
|||||
| 85 | 1 | $subscription = $this->asStripeSubscription(); |
|||||
|
0 ignored issues
–
show
It seems like
asStripeSubscription() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 86 | |||||||
| 87 | 1 | $this->pause_collection = $subscription->pause_collection?->toArray(); |
|||||
| 88 | 1 | $this->save(); |
|||||
|
0 ignored issues
–
show
It seems like
save() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 89 | } |
||||||
| 90 | |||||||
| 91 | 1 | return $this; |
|||||
| 92 | } |
||||||
| 93 | |||||||
| 94 | /** |
||||||
| 95 | * @inerhitDoc |
||||||
| 96 | */ |
||||||
| 97 | 3 | public function paused(?string $behavior = null): bool |
|||||
| 98 | { |
||||||
| 99 | 3 | $hasBehavior = is_array($this->pause_collection) && !empty($this->pause_collection['behavior']); |
|||||
| 100 | 3 | if ($behavior) { |
|||||
| 101 | 2 | return $hasBehavior && $this->pause_collection['behavior'] === $behavior; |
|||||
| 102 | } |
||||||
| 103 | |||||||
| 104 | 3 | return $hasBehavior; |
|||||
| 105 | } |
||||||
| 106 | |||||||
| 107 | /** |
||||||
| 108 | * Filter query by pause_collection behavior. |
||||||
| 109 | * |
||||||
| 110 | * @param Builder $query |
||||||
| 111 | * @param string|null $behavior |
||||||
| 112 | * |
||||||
| 113 | * @return Builder |
||||||
| 114 | */ |
||||||
| 115 | 1 | public function scopePaused($query, ?string $behavior = null): Builder |
|||||
| 116 | { |
||||||
| 117 | 1 | if (!$behavior) { |
|||||
| 118 | 1 | return $query->whereNotNull('pause_collection') |
|||||
| 119 | 1 | ->where('pause_collection->behavior', '!=', '') |
|||||
| 120 | 1 | ->whereNotNull('pause_collection->behavior'); |
|||||
| 121 | } |
||||||
| 122 | |||||||
| 123 | 1 | return $query->where('pause_collection->behavior', '=', $behavior); |
|||||
| 124 | } |
||||||
| 125 | |||||||
| 126 | /** |
||||||
| 127 | * @inerhitDoc |
||||||
| 128 | */ |
||||||
| 129 | 1 | public function notPaused(?string $behavior = null): bool |
|||||
| 130 | { |
||||||
| 131 | 1 | return !$this->paused($behavior); |
|||||
| 132 | } |
||||||
| 133 | |||||||
| 134 | /** |
||||||
| 135 | * Filter query by not paused payment collection. |
||||||
| 136 | * |
||||||
| 137 | * @param Builder $query |
||||||
| 138 | * @param string|null $behavior |
||||||
| 139 | * |
||||||
| 140 | * @return Builder |
||||||
| 141 | */ |
||||||
| 142 | 1 | public function scopeNotPaused($query, ?string $behavior = null): Builder |
|||||
| 143 | { |
||||||
| 144 | 1 | if (!$behavior) { |
|||||
| 145 | 1 | return $query->where(function ($query) { |
|||||
| 146 | 1 | $query->whereNull('pause_collection') |
|||||
| 147 | 1 | ->orWhere('pause_collection->behavior', '=', '') |
|||||
| 148 | 1 | ->orWhereNull('pause_collection->behavior'); |
|||||
| 149 | 1 | }); |
|||||
| 150 | } |
||||||
| 151 | |||||||
| 152 | 1 | return $query->where(function ($query) use ($behavior) { |
|||||
| 153 | 1 | $query->where('pause_collection->behavior', '!=', $behavior) |
|||||
| 154 | 1 | ->orWhereNull('pause_collection') |
|||||
| 155 | 1 | ->orWhereNull('pause_collection->behavior'); |
|||||
| 156 | 1 | }); |
|||||
| 157 | } |
||||||
| 158 | |||||||
| 159 | /** |
||||||
| 160 | * @inerhitDoc |
||||||
| 161 | */ |
||||||
| 162 | 2 | public function pauseResumesAtTimestamp(?string $behavior = null): ?string |
|||||
| 163 | { |
||||||
| 164 | 2 | if (!$this->paused($behavior) || empty($this->pause_collection['resumes_at'])) { |
|||||
| 165 | 2 | return null; |
|||||
| 166 | } |
||||||
| 167 | |||||||
| 168 | 1 | return $this->pause_collection['resumes_at']; |
|||||
| 169 | } |
||||||
| 170 | |||||||
| 171 | /** |
||||||
| 172 | * @inerhitDoc |
||||||
| 173 | */ |
||||||
| 174 | 2 | public function pauseResumesAt(?string $behavior = null): ?Carbon |
|||||
| 175 | { |
||||||
| 176 | 2 | if ($timestamp = $this->pauseResumesAtTimestamp($behavior)) { |
|||||
| 177 | 1 | return Carbon::createFromTimestamp($timestamp); |
|||||
| 178 | } |
||||||
| 179 | |||||||
| 180 | 2 | return null; |
|||||
| 181 | } |
||||||
| 182 | } |
||||||
| 183 |