PHP code example of goldnead / statamic-entitlements
1. Go to this page and download the library: Download goldnead/statamic-entitlements 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/ */
goldnead / statamic-entitlements example snippets
// routes/console.php or App\Console\Kernel
Schedule::command('entitlements:announce')->everyFifteenMinutes();
use Goldnead\Entitlements\Facades\Entitlements;
Entitlements::grant(
subject: $user, // any Eloquent model, or a SubjectReference
productSlug: 'chorleiter-kurs', // a free string; there is no catalogue
source: 'mollie', // a free string; see Sources
sourceRef: $payment->id, // the external id this grant came from
expiresAt: now()->addYear(),
);
Entitlements::grantPending($contact, 'warmup-guide', 'newsletter_optin', 'warmup-guide');
Entitlements::claimPending($grant); // true exactly once, under any concurrency
Entitlements::revoke($grant, 'Chargeback #1234', $actor);
Entitlements::restore($grant, $actor);
Entitlements::enterGracePeriod($grant, now()->addWeek());
Entitlements::setLimits('choir', [
'analyses' => ['value' => 50, 'period' => 'year'], // usage, counted here
'arrangements' => 25, // stock, counted by the app
'exports' => null, // unlimited
]);
// config/entitlements.php
'limits' => [
'keys' => ['analyses' => ['label' => 'Analyses', 'period' => 'year']],
'products' => ['free' => ['analyses' => 10, 'arrangements' => 3]],
'fallback_product' => 'free', // applies to everybody without a grant carrying the key
'period_anchor' => 'grant', // or 'calendar'
],
// Usage per period: counted here, reset with the term.
$receipt = Entitlements::consume($team, 'analyses'); // UsageReceipt, or null when refused
if (! $receipt) {
return response()->json(['message' => 'Analysis quota exceeded.'], 403);
}
$job->quota_receipt = $receipt->toArray(); // keep it with the job
// The job failed, maybe in the next period: give it back where it was booked.
Entitlements::release($team, 'analyses', receipt: $job->quota_receipt);
// Stock: the app counts, the addon decides.
if (! Entitlements::withinLimit($team, 'arrangements', $team->projects()->count())) {
return response()->json(['message' => 'Project limit reached.'], 403);
}
// A stock read without a count uses the last count withinLimit() was given.
Entitlements::limit($user, 'analyses'); // int|null (null = unlimited)
Entitlements::remaining($user, 'analyses'); // int|null
Entitlements::quota($user, 'analyses')->toArray(); // limit, used, remaining, period, holder, product…
Entitlements::quotasFor($user); // every limit, keyed by key
Entitlements::resetUsage($user, 'analyses', $actor);
Entitlements::extendSubjects(new TeamSubjects); // a Contracts\SubjectExpander
Entitlements::extendSubjects($teams); // any object with relatedSubjects(SubjectReference)
Entitlements::extendSubjects(fn ($subject, SubjectReference $ref) => [new SubjectReference('team', '7')]);
// or tag a class: app()->tag([TeamSubjects::class], 'entitlements.subject-expanders');