PHP code example of keloola / quota

1. Go to this page and download the library: Download keloola/quota 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/ */

    

keloola / quota example snippets


'references' => [
    'app'          => ['table' => 'apps',          'column' => 'id', 'type' => 'unsignedBigInteger'],
    'app_plan'     => ['table' => 'app_plans',     'column' => 'id', 'type' => 'unsignedBigInteger'],
    'organization' => ['table' => 'organizations', 'column' => 'id', 'type' => 'uuid'],
    'constrained'  => false, // set true bila tabel referensi ada di DB yang sama
],

use Keloola\Quota\Facades\Quota;

// Cek apakah masih bisa menambah transaksi
$can = Quota::canConsume('transactions', 1);

// Catat 1 transaksi (counter — akan reset bulan depan)
Quota::increment('transactions', 1, ['ref' => $invoice->id]);

// Tambah user (snapshot — naik turun, tidak di-reset)
Quota::increment('users');

// User dihapus
Quota::decrement('users');

// Set absolut (cocok untuk storage yang dihitung ulang dari file)
Quota::set('storage_mb', 4096);

// Sisa & laporan
$sisa   = Quota::remaining('storage_mb'); // null = unlimited
$report = Quota::report();

use Keloola\Quota\Exceptions\QuotaExceededException;

try {
    Quota::increment('transactions');
} catch (QuotaExceededException $e) {
    return response()->json(['message' => 'Kuota transaksi habis untuk bulan ini'], 429);
}

// di rute aplikasi satelit Anda
Route::middleware(['keloola.quota.context'])->group(function () {
    Route::post('/transactions', function () {
        // Konteks app, org, dan plan sudah di-set oleh middleware
        // sehingga Anda cukup memanggil metriknya saja:
        Quota::increment('transactions');
    });
});

// Cek apakah masih memiliki kuota transactions (amount: 1)
Route::post('/transactions', [TransactionController::class, 'store'])
    ->middleware(['keloola.quota.context', 'keloola.quota.check:transactions']);

// Cek dengan amount yang spesifik (misal butuh 5 kuota)
Route::post('/bulk-transactions', [TransactionController::class, 'bulkStore'])
    ->middleware(['keloola.quota.context', 'keloola.quota.check:transactions,5']);

use Illuminate\Support\Facades\Schedule;

Schedule::command('keloola-quota:reset-counters')->monthlyOn(1, '00:05');

// Saat organization install app
app(QuotaProvisioningDispatcher::class)->pushPlan($appPlan);

// Otomatis saat limit diubah, lewat observer AppPlanQuota
app(QuotaProvisioningDispatcher::class)->pushPlan($quota->appPlan);

use Keloola\Quota\Services\QuotaCheckService;

$service = app(QuotaCheckService::class);
$response = $service->checkQuota('https://app.keloola.in', 'storage_space', $jwtToken);

if ($response && $response['status'] === 'ok') {
    $remaining = $response['data']['remaining'];
    // ... logic
}

'storage' => [
    'api_url'     => env('KELOOLA_FILE_API_URL', 'https://file.keloola.in'),
    'metric_code' => env('KELOOLA_FILE_METRIC_CODE', 'storage_space'),
],

$apiUrl = config('keloola-quota.storage.api_url');
$metricCode = config('keloola-quota.storage.metric_code');

$response = app(QuotaCheckService::class)->checkQuota($apiUrl, $metricCode, $jwtToken);
bash
php artisan vendor:publish --tag=keloola-quota-config
php artisan vendor:publish --tag=keloola-quota-migrations
php artisan migrate
bash
php artisan db:seed --class="Keloola\Quota\Database\Seeders\QuotaSeeder"