PHP code example of cellard / throttle

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

    

cellard / throttle example snippets


try {
    throttle('sms')
        ->subject('+70001234567')
        ->try();

    // Your code to send SMS here

} catch (\Cellard\Throttle\ThrottlingException $exception) {
    // No, You Don't
}

'providers' => [
    /**
     * Third Party Service Providers...
     */
    Cellard\Throttle\ThrottleServiceProvider::class
],

class ThrottleSms extends Cellard\Throttle\ThrottleService
{
    public function rules()
    {
        return [
            '1:60', // one sms per minute
            '3:300', // 3 sms per 5 minutes
            '5:86400', // maximum 5 sms every day
        ];
    }
}

class ThrottleSms extends Cellard\Throttle\ThrottleService
{
    public function rules()
    {
        return [
            $this->everyMinute(),
            $this->everyFiveMinutes(3),
            $this->daily(5)
        ];
    }
}

return [
    'events' => [
        'sms' => ThrottleSms::class
    ]
];

class ThrottleSms extends Cellard\Throttle\ThrottleService
{
    public function rules()
    {
        return [
            '1:60' => 'You may send only one SMS per minute',
            '3:300' => 'You may send no more than three SMS in five minutes'
        ];
    }
}

$throttle = Throttle::event('sms')->subject('+70001234567');

if ($throttle->allow()) {

    // Your code to send SMS here


    // Do not forget to register an event
    $throttle->hit();

} else {

    // Show error message
    $throttle->error();

    // Show the time, next sms is allowed
    $throttle->next();

}

try {

    Throttle::event('sms')
        ->subject('+70001234567')
        ->try();

    // Your code to send SMS here

} catch (\Cellard\Throttle\ThrottlingException $exception) {

    // Show error message
    $exception->getMessage();

    // Show the time, next sms is allowed
    $exception->getNext();
}

throttle('sms')
    ->subject('+70001234567')
    ->try();

Throttle::event('sms')->try();

Throttle::event('sms')->subject('+70001234567')->try();