Skip to content

Commit

Permalink
change some methods to getters (where it made more sense)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Baettig committed Nov 22, 2019
1 parent 89645bd commit dcc472f
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 121 deletions.
19 changes: 11 additions & 8 deletions src/Models/HasPlans.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ public function subscriptions(): MorphMany
return $this->morphMany(config('subscriptions.models.subscription'), 'model');
}

/**
* @return Subscription | null
*/
public function activeSubscription(): ?Subscription
private function activeSubscription()
{
/** @noinspection PhpUndefinedMethodInspection */
return $this->subscriptions()->active()->first();
}

public function getActiveSubscriptionAttribute()
{
return $this->activeSubscription();
}

/**
* @return Subscription | null
*/
Expand Down Expand Up @@ -158,7 +160,7 @@ public function migrateSubscriptionTo(Plan $plan,
throw new SubscriptionException('no active subscription found');
}

if (false === $immediate && $previousSubscription->isTesting()) {
if (false === $immediate && $previousSubscription->is_testing) {
throw new SubscriptionException('can only migrate a subscription in the test phase immediately');
}

Expand Down Expand Up @@ -292,23 +294,24 @@ public function cancelSubscription(bool $immediate = false): Subscription
*/
public function renewExpiringSubscription(bool $markAsPaid = false): Subscription
{
/** @var Subscription $activeSubscription */
if (!$activeSubscription = $this->activeSubscription()) {
throw new SubscriptionException('No active subscription found');
}

if (!$activeSubscription->isPaid()) {
if (!$activeSubscription->is_paid) {
throw new SubscriptionException('Renewing is not possible if currently active Subscription is not paid');
}

if (!$activeSubscription->isExpiring()) {
if (!$activeSubscription->is_expiring) {
throw new SubscriptionException('Renewing is not possible if subscription is expiring earlyer than tomorrow midnight');
}

if (!$activeSubscription->is_recurring) {
throw new SubscriptionException('Renewing a non-recurring subscription is not possible');
}

if ($activeSubscription->isPendingCancellation()) {
if ($activeSubscription->is_pending_cancellation) {
throw new SubscriptionException('Renewing a subscription that is pending cancellation is not possible');
}

Expand Down
87 changes: 73 additions & 14 deletions src/Models/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
*
* @property Plan plan
* @property HasMany features
* @property boolean has_started
* @property boolean is_testing
* @property boolean is_paid
* @property boolean is_pending_cancellation
* @property boolean is_cancelled
* @property boolean is_renewed
* @property boolean is_active
* @property boolean is_refunded
* @property boolean is_within_payment_tolerance_time
*
* @method static Builder active
* @method static Builder expiring
Expand Down Expand Up @@ -188,68 +197,118 @@ public function scopeWithinPaymentTolerance($query): Builder
return $query->where('payment_tolerance_ends_at', '>', Carbon::now());
}

public function hasStarted(): bool
private function hasStarted(): bool
{
return Carbon::now()->greaterThanOrEqualTo(Carbon::parse($this->starts_at)->startOfDay());
}

public function isTesting(): bool
public function getHasStartedAttribute(): bool
{
return $this->hasStarted();
}

private function isTesting(): bool
{
return (null !== $this->test_ends_at && Carbon::now()->lessThan(Carbon::parse($this->test_ends_at)));
}

public function isUpcoming(): bool
public function getIsTestingAttribute(): bool
{
return $this->isTesting();
}

private function isUpcoming(): bool
{
return $this->starts_at > Carbon::now();
}

public function isWithinPaymentToleranceTime(): bool
public function getIsUpcomingAttribute(): bool
{
return $this->isUpcoming();
}

private function isWithinPaymentToleranceTime(): bool
{
return (Carbon::now()->lessThan(Carbon::parse($this->payment_tolerance_ends_at)));
}

public function isPaid(): bool
public function getIsWithinPaymentToleranceTimeAttribute(): bool
{
return $this->isWithinPaymentToleranceTime();
}

private function isPaid(): bool
{
return $this->paid_at !== null;
}

public function isCancelled(): bool
public function getIsPaidAttribute(): bool
{
return $this->isPaid();
}

private function isCancelled(): bool
{
return ($this->cancelled_at !== null && $this->cancelled_at <= Carbon::now());
}

public function isPendingCancellation(): bool
public function getIsCancelledAttribute(): bool
{
return $this->isCancelled();
}

private function isPendingCancellation(): bool
{
return ($this->cancelled_at !== null && $this->cancelled_at >= Carbon::now());
}

public function isRecurring(): bool
public function getIsPendingCancellationAttribute(): bool
{
return ($this->is_recurring === true);
return $this->isPendingCancellation();
}

public function isRefunded(): bool
private function isRefunded(): bool
{
return ($this->refunded_at !== null);
}

public function isRenewed(): bool
public function getIsRefundedAttribute(): bool
{
return $this->isRefunded();
}

private function isRenewed(): bool
{
return ($this->renewed_at !== null);
}

public function isExpiring(): bool
public function getIsRenewedAttribute(): bool
{
return $this->isRenewed();
}

private function isExpiring(): bool
{
return ($this->expires_at > Carbon::tomorrow()->endOfDay()->subSecond() &&
$this->expires_at < Carbon::tomorrow()->endOfDay()->addSecond());
}

public function hasExpired(): bool
public function getIsExpiringAttribute(): bool
{
return $this->isExpiring();
}

private function hasExpired(): bool
{
return Carbon::now()->greaterThan(Carbon::parse($this->expires_at));
}

public function isActive(): bool
public function getHasExpiredAttribute(): bool
{
return $this->hasExpired();
}

private function isActive(): bool
{
if ($this->isTesting()) {
return true;
Expand Down
26 changes: 13 additions & 13 deletions tests/feature/CancelSubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function can_cancel_a_subscription_immediately(): void
{
$subscription = $this->user->subscribeTo($this->plan, false);
$subscription->markAsPaid();
$this->assertTrue($subscription->isActive());
$this->assertTrue($subscription->is_active);

Event::fake();
$this->user->cancelSubscription(true);
$this->assertFalse($subscription->fresh()->isActive());
$this->assertTrue($subscription->fresh()->isCancelled());
$this->assertFalse($subscription->fresh()->is_active);
$this->assertTrue($subscription->fresh()->is_cancelled);
/** @noinspection PhpUndefinedMethodInspection */
Event::assertDispatched(SubscriptionCancelled::class);
}
Expand All @@ -58,13 +58,13 @@ public function can_cancel_a_subscription_on_expiration_date(): void
{
$subscription = $this->user->subscribeTo($this->plan, false);
$subscription->markAsPaid();
$this->assertTrue($subscription->isActive());
$this->assertTrue($subscription->is_active);

Event::fake();
$this->user->cancelSubscription();
$this->assertTrue($subscription->fresh()->isActive());
$this->assertFalse($subscription->fresh()->isCancelled());
$this->assertTrue($subscription->fresh()->isPendingCancellation());
$this->assertTrue($subscription->fresh()->is_active);
$this->assertFalse($subscription->fresh()->is_cancelled);
$this->assertTrue($subscription->fresh()->is_pending_cancellation);
$this->assertEquals($subscription->fresh()->expires_at, $subscription->fresh()->cancelled_at);
/** @noinspection PhpUndefinedMethodInspection */
Event::assertDispatched(SubscriptionCancelled::class);
Expand All @@ -81,7 +81,7 @@ public function can_only_cancel_active_subscriptions(): void
try{
$this->user->cancelSubscription(true);
} catch (SubscriptionException $e) {
$this->assertFalse($subscription->fresh()->isCancelled());
$this->assertFalse($subscription->fresh()->is_cancelled);
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(SubscriptionCancelled::class);
return;
Expand All @@ -97,7 +97,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat
{
$oldSubscription = $this->user->subscribeTo($this->plan, false);
$oldSubscription->markAsPaid();
$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertEquals('yearly', $activeSubscription->plan->type);
Event::fake();

Expand All @@ -106,7 +106,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat
$newSubscription = $this->user->migrateSubscriptionTo($durationPlan, false, true, 30);
$newSubscription->markAsPaid();

$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertTrue($activeSubscription->is($newSubscription));
/** @noinspection PhpUndefinedMethodInspection */
Event::assertDispatched(SubscriptionMigrated::class);
Expand All @@ -119,7 +119,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_
{
$oldSubscription = $this->user->subscribeTo($this->plan, false);
$oldSubscription->markAsPaid();
$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertEquals('yearly', $activeSubscription->plan->type);
$durationPlan = factory(Plan::class)->states('active', 'duration')->create();
Event::fake();
Expand All @@ -142,7 +142,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_
public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void
{
$this->user->subscribeTo($this->plan, false, 30);
$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertEquals('yearly', $activeSubscription->plan->type);
Event::fake();

Expand All @@ -153,7 +153,7 @@ public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void
$newSubscription->markAsPaid();

} catch (SubscriptionException $e) {
$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertTrue($activeSubscription->is($activeSubscription));
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(SubscriptionMigrated::class);
Expand Down
18 changes: 9 additions & 9 deletions tests/feature/ExtendSubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function can_extend_an_existing_subscription(): void
Event::fake();
$this->user->extendSubscription(10);

$subscription = $this->user->activeSubscription();
$subscription = $this->user->active_subscription;
$this->assertTrue($subscription->is($activeSubscription));
/** @noinspection PhpUndefinedMethodInspection */
/** @noinspection ArgumentEqualsDefaultValueInspection */
Expand Down Expand Up @@ -93,7 +93,7 @@ public function can_extend_an_existing_subscription_to_a_certain_date(): void
Event::fake();
$this->user->extendSubscriptionTo(Carbon::parse('+ 2 weeks'));

$subscription = $this->user->activeSubscription();
$subscription = $this->user->active_subscription;
$this->assertTrue($subscription->is($activeSubscription));
/** @noinspection PhpUndefinedMethodInspection */
$this->assertEqualsWithDelta(Carbon::now()->addWeeks(2)->endOfDay(), $subscription->expires_at, 1);
Expand Down Expand Up @@ -132,7 +132,7 @@ public function can_migrate_a_yearly_plan_to_a_monthly_plan_on_the_expiry_date()
{
$oldSubscription = $this->user->subscribeTo($this->plan, false);
$oldSubscription->markAsPaid();
$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertEquals('yearly', $activeSubscription->plan->type);
sleep(1);

Expand All @@ -141,7 +141,7 @@ public function can_migrate_a_yearly_plan_to_a_monthly_plan_on_the_expiry_date()
$newSubscription = $this->user->migrateSubscriptionTo($monthlyPlan, true, false);
$newSubscription->markAsPaid();

$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertTrue($activeSubscription->is($oldSubscription));
$latestSubscription = $this->user->latestSubscription();
$this->assertEquals('monthly', $latestSubscription->plan->type);
Expand All @@ -155,7 +155,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat
{
$oldSubscription = $this->user->subscribeTo($this->plan, false);
$oldSubscription->markAsPaid();
$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertEquals('yearly', $activeSubscription->plan->type);
Event::fake();

Expand All @@ -164,7 +164,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat
$newSubscription = $this->user->migrateSubscriptionTo($durationPlan, false, true, 30);
$newSubscription->markAsPaid();

$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertTrue($activeSubscription->is($newSubscription));
/** @noinspection PhpUndefinedMethodInspection */
Event::assertDispatched(SubscriptionMigrated::class);
Expand All @@ -177,7 +177,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_
{
$oldSubscription = $this->user->subscribeTo($this->plan, false);
$oldSubscription->markAsPaid();
$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertEquals('yearly', $activeSubscription->plan->type);
$durationPlan = factory(Plan::class)->states('active', 'duration')->create();
Event::fake();
Expand All @@ -200,7 +200,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_
public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void
{
$this->user->subscribeTo($this->plan, false, 30);
$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertEquals('yearly', $activeSubscription->plan->type);
Event::fake();

Expand All @@ -211,7 +211,7 @@ public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void
$newSubscription->markAsPaid();

} catch (SubscriptionException $e) {
$activeSubscription = $this->user->activeSubscription();
$activeSubscription = $this->user->active_subscription;
$this->assertTrue($activeSubscription->is($activeSubscription));
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(SubscriptionMigrated::class);
Expand Down
Loading

0 comments on commit dcc472f

Please sign in to comment.