1. Go to this page and download the library: Download moirei/saas-metre 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/ */
use MOIREI\Metre\Objects\MetreInput;
use MOIREI\Metre\Objects\Measure;
...
$metre = Metre::make(
new MetreInput(
startOfPeriods: now()->unix(),
measures: [
new Measure(
name: 'user_accounts',
type: MeasureType::fromNative('VOLUME'),
limit: 100,
defaultTags: ['publisher'],
),
]
),
);
$metre = new Metre();
// create a measure to limit the amount of "user accounts"
$metre->addMeasure('user_accounts');
$metre->setMeasureType('user_accounts', MeasureType::fromNative('VOLUME')); // set volume since it defaults to "METERED"
$metre->setMeasureLimit('user_accounts', 5);
$metre->addMeasure('orders'
type: MeasureType::fromNative('METERED'),
defaultTags: ['sales'],
);
$metre->canUse('user_accounts');
$metre->canUse('user_accounts', count: 2); // If usable by 2 counts
$metre->canUse('user_accounts', tags: [...], period: $period); // Check against tags and period
$usage = $metre->usage('user_accounts');
$usage->count; // amount used
$usage->limit; // measure/usage limit
$usage->entries; // amount of entries
$usage->percentage(); // get percentage used. Returns null if no limit is set
$usage = $metre->usage('user_accounts',
period: now()->subHours(12)->unix(),
);
$usage = $metre->usage('user_accounts',
period: [
now()->subDays(2)->unix(), // get usage from this time and ignore measure type
now()->unix(),
],
);
$metre->clear(); // clear all
$metre->clear('user_accounts'); // clear "user_accounts"
$metre->clear('user_accounts', 'orders'); // clear "user_accounts" and "orders"
$metre->clear(['user_accounts', 'orders']); // clear "user_accounts" and "orders"
$metre->clear('unknown_measure'); // fails, "unknown_measure" doesnt exist
$metre->newPeriod();
// or start a new period for 2 hours from now
$metre->newPeriod(now()->addHours(2));
...
use MOIREI\Metre\MetreCaster;
class Subscription extends Model
{
...
/**
* The attributes that should be casted.
*
* @var array
*/
protected $casts = [
...
'usage' => MetreCaster::class,
];
...
}