Skip to content

Commit

Permalink
Fix trait detection in purge command
Browse files Browse the repository at this point in the history
  • Loading branch information
alajusticia committed Oct 6, 2021
1 parent 7fee8fa commit d6f90a6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ By default the package adds an attribute named `expires_at` on your model.
You can change this name by setting the `EXPIRES_AT` constant (don't forget to set the same name for the column in
the migration, [see below](#prepare-your-migration)).

For example, let's say that we have a `App\Subscription` model and we want the attribute to be `ends_at`:
For example, let's say that we have a `Subscription` model and we want the attribute to be `ends_at`:

```php
use ALajusticia\Expirable\Traits\Expirable;
Expand Down Expand Up @@ -122,11 +122,11 @@ class Subscription extends Model

```php
// Create a new subscription which will expire in six months (using default expiration date)
$usbscription = new App\Subscription;
$usbscription = new Subscription;
$subscription->save();

// Create a new subscription which will expire in one year (overwrite the default expiration date)
$usbscription = new App\Subscription;
$usbscription = new Subscription;
$subscription->expiresAt(Carbon::now()->addYear());
$subscription->save();
```
Expand Down Expand Up @@ -203,7 +203,7 @@ For example:

```php
// Get only valid and current subscriptions and exclude expired ones
$subscriptions = App\Subscription::all();
$subscriptions = Subscription::all();
```

This, and all the next examples, work as well with relationships:
Expand All @@ -219,7 +219,7 @@ To disable the default filtering and retrieve all the models, ignoring their exp

```php
// Get all subscriptions
$subscriptions = App\Subscription::withExpired()->get();
$subscriptions = Subscription::withExpired()->get();
```

#### Retrieving only expired models
Expand All @@ -228,7 +228,7 @@ Use the onlyExpired scope:

```php
// Get expired subscriptions
$subscriptions = App\Subscription::onlyExpired()->get();
$subscriptions = Subscription::onlyExpired()->get();
```

#### Retrieving only eternal models
Expand All @@ -237,7 +237,7 @@ To get only the models which do not expire (with expiration date attribute to `n

```php
// Get unlimited subscriptions
$subscriptions = App\Subscription::onlyEternal()->get();
$subscriptions = Subscription::onlyEternal()->get();
```

#### Retrieving expired models since
Expand All @@ -253,7 +253,7 @@ the query will be:

```php
// Delete expired models since one year or more
App\Subscription::expiredSince('1 year')->delete();
Subscription::expiredSince('1 year')->delete();
```

### Get the expiration date
Expand All @@ -273,7 +273,7 @@ with a date object (or `null` for eternal):

```php
// Create a new subscription valid for one month
$subscription = new App\Subscription();
$subscription = new Subscription();
$subscription->ends_at = Carbon::now()->addMonth();
$subscription->save();
```
Expand All @@ -283,7 +283,7 @@ Of course it also works with mass assignment, but don't forget to add the attrib

```php
// Create a new subscription valid for one month
$subscription = App\Subscription::create([
$subscription = Subscription::create([
'plan' => 'premium',
'ends_at' => Carbon::now()->addMonth(),
]);
Expand All @@ -297,13 +297,13 @@ On an Eloquent query the changes will be directly saved in the database. On a si

```php
// Create a new subscription valid for one month
$subscription = new App\Subscription();
$subscription = new Subscription();
$subscription->expiresAt(Carbon::now()->addMonth());
$subscription->save();
```
```php
// Set multiple subscriptions valid for one year
App\Subscription::whereKey([1, 2, 3])->expiresAt(Carbon::now()->addYear());
Subscription::whereKey([1, 2, 3])->expiresAt(Carbon::now()->addYear());
```

#### Using lifetime()
Expand All @@ -313,7 +313,7 @@ The parameter must be a string and the syntax is the same as the syntax accepted

```php
// Create a new subscription valid for one month
$subscription = new App\Subscription();
$subscription = new Subscription();
$subscription->lifetime('1 month');
$subscription->save();
```
Expand All @@ -335,20 +335,20 @@ In addition to a single primary key as its argument, the `expireByKey` method wi
an array of primary keys, or a collection of primary keys:

```php
App\Subscription::expireByKey(1);
Subscription::expireByKey(1);

App\Subscription::expireByKey(1, 2, 3);
Subscription::expireByKey(1, 2, 3);

App\Subscription::expireByKey([1, 2, 3]);
Subscription::expireByKey([1, 2, 3]);

App\Subscription::expireByKey(collect([1, 2, 3]));
Subscription::expireByKey(collect([1, 2, 3]));
```

#### Expire models by query

You can also run an expire statement on a set of models:
```php
App\Subscription::where('plan', 'basic')->expire();
Subscription::where('plan', 'basic')->expire();
```

### Revive expired models
Expand All @@ -370,7 +370,7 @@ Sure, it also works with queries:

```php
// Revive by query
App\Subscription::where('plan', 'plus')->revive();
Subscription::where('plan', 'plus')->revive();
```

### Make existing models eternal
Expand All @@ -384,7 +384,7 @@ $subscription->makeEternal();
```
```php
// Make eternal by query
App\Subscription::where('plan', 'business')->makeEternal();
Subscription::where('plan', 'business')->makeEternal();
```

### Extend model lifetime
Expand Down Expand Up @@ -430,7 +430,7 @@ the configuration file:

```php
'purge' => [
\App\Subscription::class,
\App\Models\Subscription::class,
],
```

Expand Down
55 changes: 42 additions & 13 deletions src/Commands/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ALajusticia\Expirable\Commands;

use ALajusticia\Expirable\Traits\Expirable;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
Expand All @@ -25,35 +26,63 @@ class PurgeCommand extends Command
/**
* Execute the console command.
*
* @return mixed
* @return void
*/
public function handle()
{
$this->comment('Deleting expired records...');
$this->line('');
$models = Config::get('expirable.purge', []);

foreach (Config::get('expirable.purge', []) as $purgeable) {
if (count($models)) {

if (in_array('ALajusticia\Expirable\Traits\Expirable', class_uses($purgeable))) {
$this->line('');
$this->comment('Deleting expired records...');
$this->line('');

$total = call_user_func($purgeable.'::onlyExpired')->forceDelete();
foreach (Config::get('expirable.purge', []) as $purgeable) {

$this->line($purgeable . ': ');

if ($total > 0) {
$this->info($total . ' ' . Str::plural('record', $total) . ' deleted.');
if ($this->usesExpirable($purgeable)) {

$total = call_user_func($purgeable . '::onlyExpired')->forceDelete();

if ($total > 0) {
$this->info($total . ' ' . Str::plural('record', $total) . ' deleted.');
} else {
$this->comment('Nothing to delete.');
}

} else {
$this->comment('Nothing to delete.');
$this->error('This model is not expirable! (Expirable trait not found)');
}

} else {

$this->error($purgeable.': this model is not expirable! (Expirable trait not found)');
$this->line('');
}

$this->info('Purge completed!');
$this->line('');
} else {
$this->comment('There is no model in the purge array.');
$this->comment('Add models you want to purge in the expirable.php configuration file.');
}
}

/**
* Determine if a class uses the Expirable trait.
*
* @param string $class
* @return bool
*/
protected function usesExpirable(string $class): bool
{
$traits = array_values(class_uses($class));

foreach (class_parents($class) as $parentClass) {
foreach (class_uses($parentClass) as $trait) {
$traits[] = $trait;
}
}

$this->info('Purge completed!');
return in_array(Expirable::class, $traits);
}
}

0 comments on commit d6f90a6

Please sign in to comment.