Skip to content

Commit

Permalink
Merge pull request #6 from sagitarius29/develop
Browse files Browse the repository at this point in the history
Fixed bugs and refactored code
  • Loading branch information
sagitarius29 committed Nov 2, 2019
2 parents 0c7343a + 67acd95 commit 2c4859e
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 93 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
[![Quality Score](https://img.shields.io/scrutinizer/g/sagitarius29/laravel-subscriptions.svg?style=flat-square)](https://scrutinizer-ci.com/g/sagitarius29/laravel-subscriptions)
[![Total Downloads](https://img.shields.io/packagist/dt/sagitarius29/laravel-subscriptions.svg?style=flat-square)](https://packagist.org/packages/sagitarius29/laravel-subscriptions)

A simple laravel package for Subscriptions
A simple laravel package for Subscriptions.

All ideas are welcome, please send your issue in: [Send Your Issue or Questions](https://github.com/sagitarius29/laravel-subscriptions/issues)

## Installation

Expand All @@ -31,7 +33,7 @@ Add `Sagitarius29\LaravelSubscriptions\LaravelSubscriptionsServiceProvider::clas
#### Config file and migrations
Publish package config file and migrations with the following command:
```cmd
php artisan vendor:publish --provider="Sagitarius29\LaravelSubscriptions\LaravelSubscriptionsServiceProvider::class"
php artisan vendor:publish --provider="Sagitarius29\LaravelSubscriptions\LaravelSubscriptionsServiceProvider"
```

Then run migrations:
Expand Down Expand Up @@ -87,7 +89,7 @@ $plan->features()->saveMany($features);
$plan->isFree(); // return true;

// adding interval of price
$interval = PlanInterval::make(PlanInterval::$MONTH, 1, 4.90);
$interval = PlanInterval::make(PlanInterval::MONTH, 1, 4.90);
$plan->setInterval($interval);

$plan->isFree(); // return false;
Expand Down Expand Up @@ -129,10 +131,10 @@ $user->changePlanTo($secondPlan);
$user = \Auth::user();

// the subscription is will end in the expiration date
$user->cancelSubscription();
$user->unsubscribe();

// the subscription end now
$user->forceCancelSubscription();
$user->forceUnsubscribe();
````

### Testing
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"illuminate/support": "5.7.*"
},
"require-dev": {
"mockery/mockery": "^1.2",
"orchestra/testbench": "3.7.*",
"phpunit/phpunit": "^7.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function up()
$table->increments('id');
$table->integer('plan_id')->unsigned();
$table->decimal('price');
$table->enum('interval', ['day', 'month', 'year'])->nullable();
$table->enum('interval', ['day', 'month', 'year'])->nullable();
$table->integer('interval_unit')->nullable();

$table->foreign('plan_id')
Expand Down Expand Up @@ -65,7 +65,7 @@ public function up()
$table->increments('id');
$table->integer('plan_id')->unsigned();
$table->string('subscriber_type')->nullable();
$table->timestamp('subscriber_id')->nullable();
$table->integer('subscriber_id')->nullable();
$table->timestamp('start_at')->nullable();
$table->timestamp('end_at')->nullable();
$table->timestamp('cancelled_at')->nullable();
Expand All @@ -87,7 +87,7 @@ public function up()
$table->integer('used')->nullable();

$table->foreign('plan_feature_id')
->references('id')->on('plans_features');
->references('id')->on('plan_features');

$table->timestamps();
});
Expand All @@ -103,7 +103,7 @@ public function down()
Schema::dropIfExists('subscriber_consumables');
Schema::dropIfExists('subscriptions');
Schema::dropIfExists('plan_features');
Schema::dropIfExists('plan_prices');
Schema::dropIfExists('plan_intervals');
Schema::dropIfExists('plans');
}
}
4 changes: 4 additions & 0 deletions src/Contracts/PlanIntervalContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

interface PlanIntervalContract
{
const DAY = 'day';
const MONTH = 'month';
const YEAR = 'year';

public static function make($type, int $unit, float $price): self;

public static function makeInfinite(float $price): self;
Expand Down
5 changes: 1 addition & 4 deletions src/Entities/PlanInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class PlanInterval extends Model implements PlanIntervalContract
{
public static $DAY = 'day';
public static $MONTH = 'month';
public static $YEAR = 'year';
protected $table = 'plan_intervals';
protected $fillable = [
'price', 'interval', 'interval_unit',
Expand All @@ -32,7 +29,7 @@ public static function make($type, int $unit, float $price): PlanIntervalContrac
protected static function checkIfIntervalExists(string $interval)
{
$intervals = [
self::$DAY, self::$MONTH, self::$YEAR,
self::DAY, self::MONTH, self::YEAR,
];
if (! in_array($interval, $intervals)) {
throw new IntervalErrorException(
Expand Down
21 changes: 0 additions & 21 deletions src/LaravelSubscriptionsFacade.php

This file was deleted.

28 changes: 5 additions & 23 deletions src/LaravelSubscriptionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,11 @@ public function boot()
// $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
// $this->loadRoutesFrom(__DIR__.'/routes.php');

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('subscriptions.php'),
], 'config');

// Publishing the views.
/*$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/laravel-subscriptions'),
], 'views');*/

// Publishing assets.
/*$this->publishes([
__DIR__.'/../resources/assets' => public_path('vendor/laravel-subscriptions'),
], 'assets');*/

// Publishing the translation files.
/*$this->publishes([
__DIR__.'/../resources/lang' => resource_path('lang/vendor/laravel-subscriptions'),
], 'lang');*/

// Registering package commands.
// $this->commands([]);
}
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

$this->publishes([
__DIR__.'/../config/config.php' => config_path('subscriptions.php'),
]);
}

/**
Expand Down
35 changes: 22 additions & 13 deletions src/Traits/HasSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Sagitarius29\LaravelSubscriptions\Traits;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Sagitarius29\LaravelSubscriptions\Entities\PlanInterval;
use Sagitarius29\LaravelSubscriptions\Entities\Subscription;
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
Expand All @@ -14,9 +16,9 @@ trait HasSubscriptions
{
/**
* @param PlanContract|PlanIntervalContract $planOrInterval
* @return \Illuminate\Database\Eloquent\Model
* @return Model|SubscriptionContact
*/
public function subscribeTo($planOrInterval)
public function subscribeTo($planOrInterval): SubscriptionContact
{
if ($planOrInterval instanceof PlanContract) {
return $this->subscribeToPlan($planOrInterval);
Expand All @@ -25,7 +27,12 @@ public function subscribeTo($planOrInterval)
return $this->subscribeToInterval($planOrInterval);
}

public function subscribeToPlan(PlanContract $plan)
/**
* @param PlanContract $plan
* @return Model|SubscriptionContact
* @throws SubscriptionErrorException
*/
public function subscribeToPlan(PlanContract $plan): SubscriptionContact
{
if ($plan->hasManyIntervals()) {
throw new SubscriptionErrorException(
Expand All @@ -46,7 +53,7 @@ public function subscribeToPlan(PlanContract $plan)
if ($plan->isFree()) {
$end_at = null;
} else {
$end_at = $this->calculateExpireDate($start_at, $plan->intervals()->first());
$end_at = $this->calculateExpireDate($start_at, optional($plan->intervals())->first());
}

$subscription = Subscription::make($plan, $start_at, $end_at);
Expand All @@ -62,7 +69,7 @@ public function getActiveSubscription(): ?SubscriptionContact
->first();
}

public function subscriptions()
public function subscriptions(): MorphMany
{
return $this->morphMany(config('subscriptions.entities.plan_subscription'), 'subscriber');
}
Expand All @@ -72,17 +79,19 @@ private function calculateExpireDate(Carbon $start_at, PlanIntervalContract $int
$end_at = Carbon::createFromTimestamp($start_at->timestamp);

switch ($interval->getType()) {
case PlanInterval::$DAY:
return $end_at->days($interval->getUnit());
case PlanInterval::DAY:
return $end_at->addDays($interval->getUnit());
break;
case PlanInterval::$MONTH:
case PlanInterval::MONTH:
return $end_at->addMonths($interval->getUnit());
break;
case PlanInterval::$YEAR:
case PlanInterval::YEAR:
return $end_at->addYears($interval->getUnit());
break;
default:
//TODO error exception
throw new SubscriptionErrorException(
'The interval \''.$interval->getType().'\' selected is not available.'
);
break;
}
}
Expand Down Expand Up @@ -151,12 +160,12 @@ protected function upgradeTo(PlanIntervalContract $interval): SubscriptionContac
throw new SubscriptionErrorException('You need a subscription for upgrade to other.');
}

$this->forceCancelSubscription();
$this->forceUnsubscribe();

return $this->subscribeToInterval($interval);
}

public function forceCancelSubscription()
public function forceUnsubscribe()
{
$currentSubscription = $this->getActiveSubscription();
$currentSubscription->end_at = now()->subSecond();
Expand Down Expand Up @@ -197,7 +206,7 @@ public function renewSubscription(PlanIntervalContract $interval = null)
return $currentSubscription;
}

public function cancelSubscription()
public function unsubscribe()
{
$currentSubscription = $this->getActiveSubscription();
$currentSubscription->cancelled_at = now();
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/PlanIntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ public function it_can_create_interval_for_plans()
{
// Make Interval
$interval = PlanInterval::make(
PlanInterval::$DAY,
PlanInterval::DAY,
30,
4.99
);
$plan = factory(Plan::class)->create();
$plan->setInterval($interval);

$this->assertEquals($plan->id, $interval->plan->id);
$this->assertEquals(PlanInterval::$DAY, $interval->getType());
$this->assertEquals(PlanInterval::DAY, $interval->getType());
$this->assertEquals(30, $interval->getUnit());
$this->assertEquals(4.99, $interval->getPrice());
$this->assertNotTrue($interval->isInfinite());
$this->assertTrue($interval->isNotFree());

// Interval Free
$interval = PlanInterval::make(
PlanInterval::$DAY,
PlanInterval::DAY,
30,
0
);
Expand Down
18 changes: 9 additions & 9 deletions tests/Feature/PlansTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function it_can_set_and_change_interval_of_a_plan()
$this->assertTrue(in_array(HasSingleInterval::class, class_uses($plan)));

// Interval is not free
$firstInterval = PlanInterval::make(PlanInterval::$MONTH, 1, 10.50);
$firstInterval = PlanInterval::make(PlanInterval::MONTH, 1, 10.50);

$plan->setInterval($firstInterval);

Expand All @@ -106,14 +106,14 @@ public function it_can_set_and_change_interval_of_a_plan()
$this->assertTrue($plan->isNotFree());

// it can change interval
$otherInterval = PlanInterval::make(PlanInterval::$DAY, 15, 50.00);
$otherInterval = PlanInterval::make(PlanInterval::DAY, 15, 50.00);

$plan->setInterval($otherInterval);

$this->assertDatabaseMissing((new PlanInterval())->getTable(), $firstInterval->toArray());
$this->assertDatabaseHas((new PlanInterval())->getTable(), $otherInterval->toArray());

$this->assertEquals(PlanInterval::$DAY, $plan->getInterval()->getType());
$this->assertEquals(PlanInterval::DAY, $plan->getInterval()->getType());
$this->assertEquals(15, $plan->getInterval()->getUnit());
$this->assertEquals(50.00, $plan->getInterval()->getPrice());

Expand All @@ -125,7 +125,7 @@ public function it_can_set_and_change_interval_of_a_plan()
$this->assertDatabaseMissing((new PlanInterval())->getTable(), $otherInterval->toArray());

//the interval price is zero
$intervalWithoutPrice = PlanInterval::make(PlanInterval::$DAY, 15, 0.00);
$intervalWithoutPrice = PlanInterval::make(PlanInterval::DAY, 15, 0.00);

$plan->setInterval($intervalWithoutPrice);

Expand Down Expand Up @@ -154,9 +154,9 @@ public function a_plan_may_has_many_intervals()
$this->assertTrue(in_array(HasManyIntervals::class, class_uses($plan)));

$intervals = [
PlanInterval::make(PlanInterval::$MONTH, 1, 4.90),
PlanInterval::make(PlanInterval::$MONTH, 3, 11.90),
PlanInterval::make(PlanInterval::$YEAR, 1, 49.90),
PlanInterval::make(PlanInterval::MONTH, 1, 4.90),
PlanInterval::make(PlanInterval::MONTH, 3, 11.90),
PlanInterval::make(PlanInterval::YEAR, 1, 49.90),
];

$plan->setIntervals($intervals);
Expand All @@ -177,10 +177,10 @@ public function a_plan_may_has_many_intervals()
1
);

$firstInterval = PlanInterval::make(PlanInterval::$MONTH, 1, 4.90);
$firstInterval = PlanInterval::make(PlanInterval::MONTH, 1, 4.90);
$otherPlan->addInterval($firstInterval);

$secondInterval = PlanInterval::make(PlanInterval::$YEAR, 1, 49.90);
$secondInterval = PlanInterval::make(PlanInterval::YEAR, 1, 49.90);
$otherPlan->addInterval($secondInterval);

$this->assertDatabaseHas($intervalsTable, $firstInterval->toArray());
Expand Down
Loading

0 comments on commit 2c4859e

Please sign in to comment.