Skip to content
This repository has been archived by the owner on Aug 18, 2023. It is now read-only.

Commit

Permalink
imperative saving of a subscription with the correct mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Kummer committed Mar 4, 2015
1 parent 09ae9f7 commit cda83cf
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/Ipunkt/Subscriptions/Subscription/SubscriptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
class SubscriptionRepository
{
const MODE_CREATE = 'create';
const MODE_UPDATE = 'update';

/**
* subscription model
*
Expand Down Expand Up @@ -59,7 +62,7 @@ public function create(Plan $plan, PaymentOption $paymentOption, SubscriptionSub

$subscription->plan = $plan->id();

$this->subscription = $this->saveSubscription($subscription, $plan, $paymentOption, $startDate);
$this->subscription = $this->saveSubscription($subscription, $plan, $paymentOption, $startDate, self::MODE_CREATE);

return $this->subscription;
}
Expand Down Expand Up @@ -95,7 +98,7 @@ public function upgrade(Subscription $subscription, Plan $plan, PaymentOption $p

$newSubscription = Subscription::firstOrNew($subscriptionData);

return $this->saveSubscription($newSubscription, $plan, $paymentOption, $startDate);
return $this->saveSubscription($newSubscription, $plan, $paymentOption, $startDate, self::MODE_UPDATE);
}

/**
Expand All @@ -119,7 +122,7 @@ public function findBySubscriber(SubscriptionSubscriber $subscriber)
*
* @param \Ipunkt\Subscriptions\Subscription\Contracts\SubscriptionSubscriber $subscriber
*
* @return array|static[]|Subscription[]
* @return array|static[]|Subscription[]|Collection
*/
public function allBySubscriber(SubscriptionSubscriber $subscriber)
{
Expand All @@ -135,7 +138,7 @@ public function allBySubscriber(SubscriptionSubscriber $subscriber)
* @param SubscriptionSubscriber $subscriber
* @param array $plans
*
* @return array|Subscription[]|Collection
* @return array|static[]|Subscription[]|Collection
*/
public function allBySubscriberForPlans(SubscriptionSubscriber $subscriber, array $plans)
{
Expand All @@ -153,16 +156,14 @@ public function allBySubscriberForPlans(SubscriptionSubscriber $subscriber, arra
* @param \Ipunkt\Subscriptions\Plans\Plan $plan
* @param \Ipunkt\Subscriptions\Plans\PaymentOption $paymentOption
* @param \Carbon\Carbon $startDate
* @param string $mode
*
* @return \Ipunkt\Subscriptions\Subscription\Subscription
*/
private function saveSubscription(Subscription $subscription, Plan $plan, PaymentOption $paymentOption, Carbon $startDate = null)
private function saveSubscription(Subscription $subscription, Plan $plan, PaymentOption $paymentOption, Carbon $startDate = null, $mode)
{
$create = false;
if ($startDate === null) {
$create = true;
if ($startDate === null)
$startDate = Carbon::now();
}

$subscription->trial_ends_at = with(clone $startDate)->addDays($paymentOption->period());
$subscription->subscription_ends_at = with(clone $startDate)->addDays($paymentOption->days());
Expand All @@ -178,7 +179,7 @@ private function saveSubscription(Subscription $subscription, Plan $plan, Paymen
]);
$subscription->periods()->save($period);

$event = $create
$event = ($mode === self::MODE_CREATE)
? new SubscriptionWasCreated($subscription, $plan, $paymentOption)
: new SubscriptionWasUpdated($subscription, $plan, $paymentOption);

Expand Down

0 comments on commit cda83cf

Please sign in to comment.