Skip to content

Commit

Permalink
feat: fix slug generation and add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mckenziearts committed Nov 1, 2024
1 parent 98fd039 commit 01d7987
Show file tree
Hide file tree
Showing 23 changed files with 143 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
run: composer install --prefer-dist --no-interaction

- name: Run PHPStan
run: composer stan
run: composer types
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@
"spatie/laravel-translatable": "^6.5.0"
},
"require-dev": {
"laravel/pint": "^1.13",
"larastan/larastan": "^2.0",
"laravel/pint": "^1.13",
"orchestra/testbench": "^8.0|^9.0",
"pestphp/pest": "^2.18"
"pestphp/pest": "^2.18",
"spatie/test-time": "^1.3"
},
"autoload": {
"psr-4": {
Expand All @@ -59,9 +60,9 @@
}
},
"scripts": {
"pest": "./vendor/bin/pest",
"pint": "./vendor/bin/pint",
"stan": "./vendor/bin/phpstan analyse --memory-limit=2g"
"test": "./vendor/bin/pest",
"lint": "./vendor/bin/pint",
"types": "./vendor/bin/phpstan analyse --memory-limit=2g"
},
"config": {
"sort-packages": true,
Expand Down
3 changes: 2 additions & 1 deletion database/migrations/create_plan_features_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::create(config('laravel-subscriptions.tables.features'), function (Blueprint $table): void {
Expand Down
3 changes: 2 additions & 1 deletion database/migrations/create_plan_subscription_usage_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::create(config('laravel-subscriptions.tables.subscription_usage'), function (Blueprint $table): void {
Expand Down
3 changes: 2 additions & 1 deletion database/migrations/create_plan_subscriptions_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::create(config('laravel-subscriptions.tables.subscriptions'), function (Blueprint $table): void {
Expand Down
3 changes: 2 additions & 1 deletion database/migrations/create_plans_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Illuminate\Support\Facades\Schema;
use Laravelcm\Subscriptions\Interval;

return new class () extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::create(config('laravel-subscriptions.tables.plans'), function (Blueprint $table): void {
Expand Down
17 changes: 17 additions & 0 deletions database/migrations/remove_unique_slug_on_subscriptions_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table(config('laravel-subscriptions.tables.subscriptions'), function (Blueprint $table): void {
$table->dropUnique(config('laravel-subscriptions.tables.subscriptions') . '_slug_unique');
});
}
};
40 changes: 7 additions & 33 deletions pint.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,14 @@
{
"preset": "psr12",
"preset": "laravel",
"rules": {
"align_multiline_comment": true,
"concat_space": {
"spacing": "one"
},
"array_indentation": true,
"array_syntax": true,
"blank_line_after_namespace": true,
"blank_line_after_opening_tag": true,
"combine_consecutive_issets": true,
"combine_consecutive_unsets": true,
"concat_space": true,
"declare_parentheses": true,
"blank_line_before_statement": true,
"declare_strict_types": true,
"explicit_string_variable": true,
"final_internal_class": false,
"fully_qualified_strict_types": true,
"global_namespace_import": {
"import_classes": true,
"import_constants": true,
"import_functions": true
},
"is_null": true,
"lambda_not_used_import": true,
"logical_operators": true,
"mb_str_functions": true,
"method_chaining_indentation": true,
"modernize_strpos": true,
"new_with_braces": true,
"no_empty_comment": true,
"not_operator_with_space": true,
"ordered_traits": true,
"protected_to_private": true,
"simplified_if_return": true,
"strict_comparison": true,
"ternary_to_null_coalescing": true,
"trim_array_spaces": true,
"use_arrow_functions": true,
"void_return": true
"declare_parentheses": true,
"ordered_traits": true
}
}
12 changes: 5 additions & 7 deletions src/Models/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@
namespace Laravelcm\Subscriptions\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Laravelcm\Subscriptions\Traits\HasSlug;
use Laravelcm\Subscriptions\Traits\HasTranslations;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravelcm\Subscriptions\Services\Period;
use Laravelcm\Subscriptions\Traits\BelongsToPlan;
use Laravelcm\Subscriptions\Traits\HasSlug;
use Laravelcm\Subscriptions\Traits\HasTranslations;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
use Spatie\Sluggable\SlugOptions;

/**
* Laravelcm\Subscriptions\Models\PlanFeature.
*
* @property int $id
* @property-read int|string $id
* @property string $slug
* @property array $title
* @property array $description
Expand Down
26 changes: 6 additions & 20 deletions src/Models/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

namespace Laravelcm\Subscriptions\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravelcm\Subscriptions\Traits\HasSlug;
use Laravelcm\Subscriptions\Traits\HasTranslations;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
use Spatie\Sluggable\SlugOptions;
use Spatie\EloquentSortable\Sortable;

/**
* Laravelcm\Subscriptions\Models\Plan.
*
* @property int $id
* @property-read int|string $id
* @property string $slug
* @property array $name
* @property array $description
Expand Down Expand Up @@ -96,22 +94,9 @@ class Plan extends Model implements Sortable
];

protected $casts = [
'slug' => 'string',
'is_active' => 'boolean',
'price' => 'float',
'signup_fee' => 'float',
'currency' => 'string',
'trial_period' => 'integer',
'trial_interval' => 'string',
'invoice_period' => 'integer',
'invoice_interval' => 'string',
'grace_period' => 'integer',
'grace_interval' => 'string',
'prorate_day' => 'integer',
'prorate_period' => 'integer',
'prorate_extend_due' => 'integer',
'active_subscribers_limit' => 'integer',
'sort_order' => 'integer',
'deleted_at' => 'datetime',
];

Expand Down Expand Up @@ -149,7 +134,8 @@ public function getSlugOptions(): SlugOptions
return SlugOptions::create()
->doNotGenerateSlugsOnUpdate()
->generateSlugsFrom('name')
->saveSlugsTo('slug');
->saveSlugsTo('slug')
->allowDuplicateSlugs();
}

public function features(): HasMany
Expand Down
10 changes: 4 additions & 6 deletions src/Models/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
use Spatie\Sluggable\SlugOptions;

/**
* Laravelcm\Subscriptions\Models\Subscription.
*
* @property int $id
* @property-read int|string $id
* @property string $subscriber_type
* @property string $slug
* @property array $title
Expand Down Expand Up @@ -114,7 +112,7 @@ protected static function boot(): void
parent::boot();

static::creating(function (self $model): void {
if ( ! $model->starts_at || ! $model->ends_at) {
if (! $model->starts_at || ! $model->ends_at) {
$model->setNewPeriod();
}
});
Expand Down Expand Up @@ -367,7 +365,7 @@ public function canUseFeature(string $featureSlug): bool

// If the feature value is zero, let's return false since
// there's no uses available. (useful to disable countable features)
if ( ! $usage || $usage->expired() || $featureValue === null || $featureValue === '0' || $featureValue === 'false') {
if (! $usage || $usage->expired() || $featureValue === null || $featureValue === '0' || $featureValue === 'false') {
return false;
}

Expand All @@ -382,7 +380,7 @@ public function getFeatureUsage(string $featureSlug): int
{
$usage = $this->usage()->byFeatureSlug($featureSlug)->first();

return ( ! $usage || $usage->expired()) ? 0 : $usage->used;
return (! $usage || $usage->expired()) ? 0 : $usage->used;
}

/**
Expand Down
15 changes: 6 additions & 9 deletions src/Models/SubscriptionUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
namespace Laravelcm\Subscriptions\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* Laravelcm\Subscriptions\Models\SubscriptionUsage.
*
* @property int $id
* @property-read int|string $id
* @property int $used
* @property Carbon|null $valid_until
* @property Carbon|null $created_at
Expand All @@ -32,7 +30,6 @@
* @method static \Illuminate\Database\Eloquent\Builder|\Laravelcm\Subscriptions\Models\SubscriptionUsage whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Laravelcm\Subscriptions\Models\SubscriptionUsage whereUsed($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Laravelcm\Subscriptions\Models\SubscriptionUsage whereValidUntil($value)
*
*/
class SubscriptionUsage extends Model
{
Expand Down Expand Up @@ -70,14 +67,14 @@ public function subscription(): BelongsTo
public function scopeByFeatureSlug(Builder $builder, string $featureSlug): Builder
{
$model = config('laravel-subscriptions.models.feature', Feature::class);
$feature = tap(new $model())->where('slug', $featureSlug)->first();
$feature = $model::where('slug', $featureSlug)->first();

return $builder->where('feature_id', $feature ? $feature->getKey() : null);
}

public function expired(): bool
{
if ( ! $this->valid_until) {
if (! $this->valid_until) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Services/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public function __construct(string $interval = 'month', int $count = 1, ?Carbon

if (empty($start)) {
$this->start = Carbon::now();
} elseif ( ! $start instanceof Carbon) {
} elseif (! $start instanceof Carbon) {
$this->start = new Carbon($start);
} else {
$this->start = $start;
}

$this->period = $count;
$start = clone $this->start;
$method = 'add'.ucfirst($this->interval).'s';
$method = 'add' . ucfirst($this->interval) . 's';
$this->end = $start->{$method}($this->period);
}

Expand Down
1 change: 1 addition & 0 deletions src/SubscriptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function configurePackage(Package $package): void
'create_plan_features_table',
'create_plan_subscriptions_table',
'create_plan_subscription_usage_table',
'remove_unique_slug_on_subscriptions_table',
])
->hasInstallCommand(function (InstallCommand $command): void {
$command
Expand Down
6 changes: 2 additions & 4 deletions src/Traits/HasPlanSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Laravelcm\Subscriptions\Models\Subscription;
use Laravelcm\Subscriptions\Models\Plan;
use Laravelcm\Subscriptions\Models\Subscription;
use Laravelcm\Subscriptions\Services\Period;

trait HasPlanSubscriptions
Expand All @@ -22,8 +22,6 @@ protected static function bootHasSubscriptions(): void

/**
* The subscriber may have many plan subscriptions.
*
* @return MorphMany
*/
public function planSubscriptions(): MorphMany
{
Expand All @@ -42,7 +40,7 @@ public function activePlanSubscriptions(): Collection

public function planSubscription(string $subscriptionSlug): ?Subscription
{
return $this->planSubscriptions()->where('slug', $subscriptionSlug)->first();
return $this->planSubscriptions()->where('slug', 'like', '%' . $subscriptionSlug . '%')->first();
}

public function subscribedPlans(): Collection
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected static function bootHasSlug(): void
static::creating(function (Model $model): void {
if ($model->exists && $model->getSlugOptions()->generateSlugsOnUpdate) {
$model->generateSlugOnUpdate();
} elseif ( ! $model->exists && $model->getSlugOptions()->generateSlugsOnCreate) {
} elseif (! $model->exists && $model->getSlugOptions()->generateSlugsOnCreate) {
$model->generateSlugOnCreate();
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait HasTranslations

public function getAttributeValue(mixed $key): mixed
{
if ( ! $this->isTranslatableAttribute($key)) {
if (! $this->isTranslatableAttribute($key)) {
return parent::getAttributeValue($key);
}

Expand All @@ -37,11 +37,11 @@ public function getTranslations(?string $key = null): array
);

// Inject default translation if none supplied
if ( ! is_array($value)) {
if (! is_array($value)) {
$oldValue = $value;

if ($this->hasSetMutator($key)) {
$method = 'set'.Str::studly($key).'Attribute';
$method = 'set' . Str::studly($key) . 'Attribute';
$value = $this->{$method}($value);
}

Expand Down
Loading

0 comments on commit 01d7987

Please sign in to comment.