PHP code example of kaveraa / data-lifecycle

1. Go to this page and download the library: Download kaveraa/data-lifecycle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kaveraa / data-lifecycle example snippets


#[KeepFor('3 years')]             // kept 3 years after the last sign of life
#[WarnBefore('30 days')]          // an email 30 days before the deadline
#[DisableFirst('30 days')]        // disabled, then 30 days to come back
#[ThenAnonymise('email', 'name')] // after that the row stays, but anonymous
class User
{
}

return [
    // ...
    Kaveraa\DataLifecycle\Symfony\DataLifecycleBundle::class => ['all' => true],
];

use Kaveraa\DataLifecycle\Attribute\DisableFirst;
use Kaveraa\DataLifecycle\Attribute\KeepFor;
use Kaveraa\DataLifecycle\Attribute\ThenAnonymise;
use Kaveraa\DataLifecycle\Attribute\ThenDelete;
use Kaveraa\DataLifecycle\Attribute\WarnBefore;
use Kaveraa\DataLifecycle\Strategy;

#[KeepFor('3 years')]
#[WarnBefore('30 days')]
#[WarnBefore('7 days')]
#[DisableFirst('30 days')]
#[ThenAnonymise('email', 'name', ['birth_date' => Strategy::YearOnly])]
class User extends Authenticatable
{
}

#[KeepFor('90 days', since: 'sent_at')]
#[ThenDelete]
class Invitation
{
}

// config/data-lifecycle.php
'discover' => [
    App\Models\User::class,
    App\Models\Invitation::class,
],

// config/data-lifecycle.php
'subjects' => [
    App\Models\User::class => [
        'keep_for' => '3 years',
        'warn_before' => ['30 days', '7 days'],
        'grace' => '30 days',
        'anonymise' => ['email' => 'email', 'name' => 'text'],
    ],

    App\Models\Invitation::class => [
        'keep_for' => '90 days',
        'fields' => ['since' => 'sent_at'],
        'delete' => true,
    ],
],

'fields' => ['since' => 'seen_at', 'disabled_at' => 'blocked_at'],

// routes/console.php
Schedule::command('lifecycle:run')->dailyAt('03:30');

use Kaveraa\DataLifecycle\Event\SubjectWarned;

Event::listen(function (SubjectWarned $event): void {
    $user = $event->entity();

    Mail::to($user)->send(new AccountExpiring(
        dueAt: $event->dueAt,        // date of the disabling
        reminder: $event->warnIndex, // 0 for the first reminder, 1 for the next one
    ));
});

use Kaveraa\DataLifecycle\Lifecycle;

public function login(Request $request, Lifecycle $lifecycle)
{
    // ...
    $lifecycle->reactivate($user); // no more reminder, no more disabling, clock back to zero
}

#[ThenAnonymise('email', 'name')]

$middleware->web(append: [
    \Kaveraa\DataLifecycle\Laravel\Middleware\TrackActivity::class,
]);

$lifecycle->stageOf($user);  // Stage::Active, Warned, Disabled or Erased
$lifecycle->dueAt($user);    // date of the coming disabling

use Kaveraa\DataLifecycle\Laravel\Concerns\HasLifecycle;

class User extends Authenticatable
{
    use HasLifecycle;
}

User::active()->count();
User::disabled()->get();
$user->lifecycleStage();
$user->lifecycleDueAt();
$user->reactivate();
bash
php artisan lifecycle:install
bash
php artisan lifecycle:run                       # everything, for real
php artisan lifecycle:run --dry-run             # without writing anything
php artisan lifecycle:run --subject="App\Models\User"
php artisan lifecycle:run --step=warn           # only the reminders
php artisan lifecycle:run --limit=500           # at most 500 rows per step
bash
php artisan lifecycle:run --step=warn --limit=200   # for 30 days
php artisan lifecycle:run                           # after that
bash
php artisan lifecycle:report