Skip to content

Commit

Permalink
Merge branch 'master' of github.com:gerardojbaez/laraplans
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardojbaez committed Nov 17, 2017
2 parents 8646e2e + 399665d commit 5b6d8f6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.phpintel
composer.phar
*.sublime-workspace
/.idea
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SaaS style recurring plans for Laravel 5.
- [Traits and Contracts](#traits-and-contracts)
- [Usage](#usage)
- [Create a Plan](#create-a-plan)
- [Get the value of Feature](#get-the-value-of-feature)
- [Creating subscriptions](#creating-subscriptions)
- [Subscription Ability](#subscription-ability)
- [Record Feature Usage](#record-feature-usage)
Expand Down Expand Up @@ -134,6 +135,14 @@ $plan->features()->saveMany([
]);
```

### Get the value of Feature

Say you want to show the value of the feature _pictures_per_listing_ from above. You can use `getFeatureByCode()`.

```php
$amountOfPictures = $plan->getFeatureByCode('pictures_per_listing')->value
```

### Creating subscriptions

You can subscribe a user to a plan by using the `newSubscription()` function available in the `PlanSubscriber` trait. First, retrieve an instance of your subscriber model, which typically will be your user model and an instance of the plan your user is subscribing to. Once you have retrieved the model instance, you may use the `newSubscription` method to create the model's subscription.
Expand Down
21 changes: 21 additions & 0 deletions src/Laraplans/Models/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Gerardojbaez\Laraplans\Period;
use Illuminate\Database\Eloquent\Model;
use Gerardojbaez\Laraplans\Contracts\PlanInterface;
use Gerardojbaez\LaraPlans\Exceptions\InvalidPlanFeatureException;

class Plan extends Model implements PlanInterface
{
Expand Down Expand Up @@ -112,4 +113,24 @@ public function hasTrial()
{
return (is_numeric($this->trial_period_days) and $this->trial_period_days > 0);
}

/**
* Returns the demanded feature
*
* @param String $code
* @return PlanFeature
* @throws InvalidPlanFeatureException
*/
public function getFeatureByCode($code)
{
$feature = $this->features()->getEager()->first(function($item) use ($code) {
return $item->code === $code;
});

if (is_null($feature)) {
throw new InvalidPlanFeatureException($code);
}

return $feature;
}
}
9 changes: 7 additions & 2 deletions src/Laraplans/Models/PlanSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class PlanSubscription extends Model implements PlanSubscriptionInterface
/**
* Subscription statuses
*/
const STATUS_ACTIVE = 'active';
const STATUS_CANCELED = 'canceled';
const STATUS_ENDED = 'ended';
const STATUS_ACTIVE = 'active';
const STATUS_CANCELED = 'canceled';

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -122,6 +123,10 @@ public function getStatusAttribute()
if ($this->isCanceled()) {
return self::STATUS_CANCELED;
}

if ($this->isEnded()) {
return self::STATUS_ENDED;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Laraplans/SubscriptionUsageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ class SubscriptionUsageManager
/**
* Subscription model instance.
*
* @var \Illuminate\Database\Eloqunet\Model
* @var \Illuminate\Database\Eloquent\Model
*/
protected $subscription;

/**
* Create new Subscription Usage Manager instance.
*
* @param \Illuminate\Database\Eloqunet\Model $subscription
* @param \Illuminate\Database\Eloquent\Model $subscription
*/
public function __construct($subscription)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Laraplans/Traits/PlanSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait PlanSubscriber
* Get a subscription by name.
*
* @param string $name
* @return \Gerardojbaez\Laraplans\Models\Subscription|null
* @return \Gerardojbaez\Laraplans\Models\PlanSubscription|null
*/
public function subscription($name = 'default')
{
Expand Down

0 comments on commit 5b6d8f6

Please sign in to comment.