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

Commit

Permalink
Merge branch 'release/v0.0.4'
Browse files Browse the repository at this point in the history
* release/v0.0.4:
  Bump version
  Rename subscription model activation and deactivation methods
  Update PHPUnit options
  Apply fixes from StyleCI
  Update StyleCI options
  Prepare and tweak testing configuration
  Update composer dependencies
  Drop StyleCI multi-language support (paid feature now!)
  Tweak validation rules
  Convert timestamps into datetime fields and add timezone
  Enforce consistency
  Revert titles to names again! SORRY!!
  Revert names to slugs again! SORRY!!
  Fix subscription title field naming
  Rename slug field into name
  Fix fully qualified booking unit methods (fix #20)
  Define polymorphic relationship parameters explicitly
  Rename subscription, plan, and feature "slug" to "name"
  Rename subscription, plan, and feature "name" to "title"
  Update travis php versions
  • Loading branch information
Omranic committed Sep 21, 2018
2 parents e4f576a + e688b50 commit f9bb8db
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 66 deletions.
73 changes: 42 additions & 31 deletions .styleci.yml
@@ -1,33 +1,44 @@
php:
preset: recommended
preset: recommended

enabled:
- dir_constant
- ereg_to_preg
- mb_str_functions
- no_short_echo_tag
- strict_comparison
- phpdoc_link_to_see
- no_php4_constructor
- declare_strict_types
- unalign_double_arrow
- length_ordered_imports
- modernize_types_casting
- return_type_declaration
- not_operator_with_successor_space
enabled:
- dir_constant
- ereg_to_preg
- no_useless_else
- mb_str_functions
- short_list_syntax
- no_short_echo_tag
- strict_comparison
- phpdoc_link_to_see
- property_separation
- no_php4_constructor
- php_unit_namespaced
- random_api_migration
- declare_strict_types
- unalign_double_arrow
- no_alternative_syntax
- standardize_increment
- length_ordered_imports
- modernize_types_casting
- non_printable_character
- return_type_declaration
- explicit_string_variable
- no_unneeded_final_method
- no_unneeded_curly_braces
- blank_line_before_declare
- compact_nullable_typehint
- combine_consecutive_issets
- combine_consecutive_unsets
- explicit_indirect_variable
- ternary_to_null_coalescing
- escape_implicit_backslashes
- fully_qualified_strict_types
- phpdoc_return_self_reference
- no_null_property_initialization
- multiline_comment_opening_closing
- not_operator_with_successor_space
- phpdoc_add_missing_param_annotation
- php_unit_set_up_tear_down_visibility

disabled:
- align_double_arrow
- alpha_ordered_imports

js:
tab-width: 4
print-width: 120

ts:
tab-width: 4
print-width: 120

css:
tab-width: 4
print-width: 120
disabled:
- align_double_arrow
- alpha_ordered_imports
6 changes: 5 additions & 1 deletion .travis.yml
@@ -1,14 +1,18 @@
language: php

php:
- 7.0
- 7.1
- 7.2

env:
matrix:
- COMPOSER_FLAGS="--prefer-lowest"
- COMPOSER_FLAGS=""

cache:
directories:
- $HOME/.composer/cache

before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](CONTRIBUTING.md).


## [v0.0.4] - 2018-09-21
- Update travis php versions
- Define polymorphic relationship parameters explicitly
- Fix fully qualified booking unit methods (fix #20)
- Convert timestamps into datetime fields and add timezone
- Tweak validation rules
- Drop StyleCI multi-language support (paid feature now!)
- Update composer dependencies
- Prepare and tweak testing configuration
- Update StyleCI options
- Update PHPUnit options
- Rename subscription model activation and deactivation methods

## [v0.0.3] - 2018-02-18
- Add PublishCommand to artisan
- Move slug auto generation to the custom HasSlug trait
Expand Down Expand Up @@ -34,5 +47,6 @@ This project adheres to [Semantic Versioning](CONTRIBUTING.md).
## v0.0.1 - 2017-06-29
- Tag first release

[v0.0.4]: https://github.com/rinvex/subscriptions/compare/v0.0.3...v0.0.4
[v0.0.3]: https://github.com/rinvex/subscriptions/compare/v0.0.2...v0.0.3
[v0.0.2]: https://github.com/rinvex/subscriptions/compare/v0.0.1...v0.0.2
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -99,18 +99,18 @@ $plan->hasTrial();
$plan->hasGrace();
```

Both `$plan->features` and `$plan->subscriptions` are collections, driven from relationships, and thus you can query these relations as any normal Eloquent relationship. E.g. `$plan->features()->where('slug', 'listing_title_bold')->first()`.
Both `$plan->features` and `$plan->subscriptions` are collections, driven from relationships, and thus you can query these relations as any normal Eloquent relationship. E.g. `$plan->features()->where('name', 'listing_title_bold')->first()`.

### Get Feature Value

Say you want to show the value of the feature _pictures_per_listing_ from above. You can do so in many ways:

```php
// Use the plan instance to get feature's value
$amountOfPictures = $plan->getFeatureBySlug('pictures_per_listing')->value;
$amountOfPictures = $plan->getFeatureByName('pictures_per_listing')->value;

// Query the feature itself directly
$amountOfPictures = app('rinvex.subscriptions.plan_feature')->where('slug', 'pictures_per_listing')->first()->value;
$amountOfPictures = app('rinvex.subscriptions.plan_feature')->where('name', 'pictures_per_listing')->first()->value;

// Get feature value through the subscription instance
$amountOfPictures = app('rinvex.subscriptions.plan_subscription')->find(1)->getFeatureValue('pictures_per_listing');
Expand All @@ -127,7 +127,7 @@ $plan = app('rinvex.subscriptions.plan')->find(1);
$user->newSubscription('main', $plan);
```

The first argument passed to `newSubscription` method should be the name of the subscription. If your application offer a single subscription, you might call this `main` or `primary`. The second argument is the plan instance your user is subscribing to.
The first argument passed to `newSubscription` method should be the title of the subscription. If your application offer a single subscription, you might call this `main` or `primary`. The second argument is the plan instance your user is subscribing to.

### Change the Plan

Expand All @@ -149,7 +149,7 @@ Plan features are great for fine tuning subscriptions, you can topup certain fea

```php
// Find plan feature
$feature = app('rinvex.subscriptions.plan_feature')->where('slug', 'listing_duration_days')->first();
$feature = app('rinvex.subscriptions.plan_feature')->where('name', 'listing_duration_days')->first();

// Get feature reset date
$feature->getResetDate(new \Carbon\Carbon());
Expand Down Expand Up @@ -185,7 +185,7 @@ In order to effectively use the ability methods you will need to keep track of e
$user->subscription('main')->recordFeatureUsage('listings');
```

The `recordFeatureUsage` method accept 3 parameters: the first one is the feature's slug, the second one is the quantity of uses to add (default is `1`), and the third one indicates if the addition should be incremental (default behavior), when disabled the usage will be override by the quantity provided. E.g.:
The `recordFeatureUsage` method accept 3 parameters: the first one is the feature's name, the second one is the quantity of uses to add (default is `1`), and the third one indicates if the addition should be incremental (default behavior), when disabled the usage will be override by the quantity provided. E.g.:

```php
// Increment by 2
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Expand Up @@ -49,7 +49,7 @@
"watson/validating": "^3.1.0"
},
"require-dev": {
"codedungeon/phpunit-result-printer": "^0.6.0",
"codedungeon/phpunit-result-printer": "^0.19.0",
"illuminate/container": "~5.6.0",
"phpunit/phpunit": "^7.0.0"
},
Expand All @@ -61,6 +61,11 @@
"Rinvex\\Subscriptions\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Rinvex\\Subscriptions\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true,
"preferred-install": "dist",
Expand Down
Expand Up @@ -21,11 +21,12 @@ public function up(): void
$table->string('slug');
$table->{$this->jsonable()}('name');
$table->{$this->jsonable()}('description')->nullable();
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('starts_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamp('cancels_at')->nullable();
$table->timestamp('canceled_at')->nullable();
$table->dateTime('trial_ends_at')->nullable();
$table->dateTime('starts_at')->nullable();
$table->dateTime('ends_at')->nullable();
$table->dateTime('cancels_at')->nullable();
$table->dateTime('canceled_at')->nullable();
$table->string('timezone')->nullable();
$table->timestamps();
$table->softDeletes();

Expand Down
Expand Up @@ -19,7 +19,8 @@ public function up(): void
$table->integer('subscription_id')->unsigned();
$table->integer('feature_id')->unsigned();
$table->smallInteger('used')->unsigned();
$table->timestamp('valid_until')->nullable();
$table->dateTime('valid_until')->nullable();
$table->string('timezone')->nullable();
$table->timestamps();
$table->softDeletes();

Expand Down
15 changes: 13 additions & 2 deletions phpunit.xml.dist
Expand Up @@ -15,12 +15,15 @@
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer">
<testsuites>
<testsuite name="Rinvex Subscriptions Unit Test Suite">
<directory>tests</directory>
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Rinvex Subscriptions Feature Test Suite">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
Expand All @@ -30,4 +33,12 @@
<log type="coverage-text" target="build/coverage.txt" />
<log type="coverage-clover" target="build/logs/clover.xml" />
</logging>
<php>
<env name="APP_ENV" value="testing" />
<env name="CACHE_DRIVER" value="array" />
<env name="SESSION_DRIVER" value="array" />
<env name="QUEUE_DRIVER" value="sync" />
<env name="DB_CONNECTION" value="sqlite" />
<env name="DB_DATABASE" value=":memory:" />
</php>
</phpunit>
6 changes: 3 additions & 3 deletions src/Models/Plan.php
Expand Up @@ -183,11 +183,11 @@ public function __construct(array $attributes = [])
'signup_fee' => 'required|numeric',
'currency' => 'required|alpha|size:3',
'trial_period' => 'sometimes|integer|max:10000',
'trial_interval' => 'sometimes|string|in:hour,day,week,month',
'trial_interval' => 'sometimes|in:hour,day,week,month',
'invoice_period' => 'sometimes|integer|max:10000',
'invoice_interval' => 'sometimes|string|in:hour,day,week,month',
'invoice_interval' => 'sometimes|in:hour,day,week,month',
'grace_period' => 'sometimes|integer|max:10000',
'grace_interval' => 'sometimes|string|in:hour,day,week,month',
'grace_interval' => 'sometimes|in:hour,day,week,month',
'sort_order' => 'nullable|integer|max:10000000',
'prorate_day' => 'nullable|integer|max:150',
'prorate_period' => 'nullable|integer|max:150',
Expand Down
6 changes: 3 additions & 3 deletions src/Models/PlanFeature.php
Expand Up @@ -23,7 +23,7 @@
* @property int $id
* @property int $plan_id
* @property string $slug
* @property array $name
* @property array $title
* @property array $description
* @property string $value
* @property int $resettable_period
Expand All @@ -41,7 +41,7 @@
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature wherePlanId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereResettableInterval($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereResettablePeriod($value)
Expand Down Expand Up @@ -146,7 +146,7 @@ public function __construct(array $attributes = [])
'description' => 'nullable|string|max:10000',
'value' => 'required|string',
'resettable_period' => 'sometimes|integer',
'resettable_interval' => 'sometimes|string|in:hour,day,week,month',
'resettable_interval' => 'sometimes|in:hour,day,week,month',
'sort_order' => 'nullable|integer|max:10000000',
]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Models/PlanSubscription.php
Expand Up @@ -26,7 +26,7 @@
* @property string $user_type
* @property int $plan_id
* @property string $slug
* @property array $name
* @property array $title
* @property array $description
* @property \Carbon\Carbon $trial_ends_at
* @property \Carbon\Carbon $starts_at
Expand All @@ -53,7 +53,7 @@
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscription whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscription whereEndsAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscription whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscription whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscription whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscription wherePlanId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscription whereSlug($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscription whereStartsAt($value)
Expand Down Expand Up @@ -196,7 +196,7 @@ public function getSlugOptions(): SlugOptions
*/
public function user(): MorphTo
{
return $this->morphTo();
return $this->morphTo('user', 'user_type', 'user_id');
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Models/PlanSubscriptionUsage.php
Expand Up @@ -24,7 +24,7 @@
* @property-read \Rinvex\Subscriptions\Models\PlanFeature $feature
* @property-read \Rinvex\Subscriptions\Models\PlanSubscription $subscription
*
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage byFeatureSlug($featureSlug)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage byFeatureName($featureName)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage whereFeatureId($value)
Expand Down Expand Up @@ -123,16 +123,16 @@ public function subscription(): BelongsTo
}

/**
* Scope subscription usage by feature slug.
* Scope subscription usage by feature name.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param string $featureSlug
* @param string $featureName
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeByFeatureSlug(Builder $builder, string $featureSlug): Builder
public function scopeByFeatureName(Builder $builder, string $featureName): Builder
{
$feature = PlanFeature::where('slug', $featureSlug)->first();
$feature = PlanFeature::where('name', $featureName)->first();

return $builder->where('feature_id', $feature->getKey() ?? null);
}
Expand Down
8 changes: 3 additions & 5 deletions src/Services/Period.php
Expand Up @@ -45,7 +45,7 @@ class Period
*
* @return void
*/
public function __construct($interval = 'm', $count = 1, $start = '')
public function __construct($interval = 'month', $count = 1, $start = '')
{
if (empty($start)) {
$this->start = now();
Expand All @@ -55,15 +55,13 @@ public function __construct($interval = 'm', $count = 1, $start = '')
$this->start = $start;
}

$this->interval = in_array($interval, ['d', 'w', 'm', 'y']) ? $interval : 'm';

if ($count > 0) {
$this->period = $count;
}

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

/**
Expand Down
Empty file added tests/Feature/.gitkeep
Empty file.
Empty file added tests/Unit/.gitkeep
Empty file.

0 comments on commit f9bb8db

Please sign in to comment.