faustbrian/laravel-doorkeeper

Compare the amount of relations with specified rules.

1.2.0 2018-11-10 06:51 UTC

README

Build Status PHP from Packagist Latest Version License

This package counts the amount of records on a relationship and compares them with a given set of limits. This can be useful to determine if a user has reached the limit of files he can upload or something similar.

Installation

Require this package, with Composer, in the root directory of your project.

$ composer require faustbrian/laravel-doorkeeper

Usage

Model

<?php
namespace App;

use BrianFaust\Doorkeeper\Traits\Doorkeeper;
use BrianFaust\Doorkeeper\Contracts\DoorkeeperContract;

class User extends Model implements DoorkeeperContract
{
    use Doorkeeper;


    public $limits = [
        'posts' => 5,
        'files' => 10,
    ];

    public function posts() {
        return $this->hasMany('App\Post');
    }

    public function files() {
        return $this->hasMany('App\File');
    }
}

Middleware

<?php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class ReachedLimits
{

    public function handle($request, Closure $next)
    {
        $user = Auth::check();

        if ( $user->limits($user->subscription->limits)->fails() ) {
            return redirect()->route('billing');
        }

        return $next($request);
    }
}

Controller

<?php
namespace App\Http\Controllers;

class DashboardController
{

    public function __construct()
    {
        $this->middleware('reachedLimits');
    }


    public function index()
    {
        return view('dashboard');
    }
}

Methods

Perform checks with custom limits.

$user->limits($user->subscription->limits)->passes();
$user->limits($user->subscription->limits)->fails();

Perform a check and see if it passes.

$user->passes();

Perform a check and see if it fails.

$user->fails();

Get all limits.

$user->allowed();

Get a specific limit.

$user->allowed('posts');

Get the current counter.

$user->current();

Get a specific counter.

$user->current('posts');

Check if the overall limit has been reached.

$user->maxed();

Check if a specific limit has been reached.

$user->maxed('posts');

Testing

$ phpunit

Security

If you discover a security vulnerability within this package, please send an e-mail to hello@brianfaust.me. All security vulnerabilities will be promptly addressed.

Credits

License

MIT © Brian Faust