Skip to content

Commit

Permalink
Merge pull request #23 from mostafamaklad/v1.2
Browse files Browse the repository at this point in the history
Recreate Exceptions
  • Loading branch information
mostafamaklad committed Sep 7, 2017
2 parents 10038bf + cb63cb2 commit 7ef323f
Show file tree
Hide file tree
Showing 21 changed files with 384 additions and 188 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: php

php:
- 7.1
- 7.2

matrix:
fast_finish: true
Expand All @@ -12,11 +11,6 @@ env:
- COMPOSER_FLAGS="--prefer-lowest"
- COMPOSER_FLAGS=""

matrix:
exclude:
- php: 7.2
env: COMPOSER_FLAGS="--prefer-lowest"

sudo: false

services:
Expand Down
24 changes: 11 additions & 13 deletions src/Exceptions/GuardDoesNotMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

namespace Maklad\Permission\Exceptions;

use InvalidArgumentException;
use Illuminate\Support\Collection;
use Throwable;

class GuardDoesNotMatch extends InvalidArgumentException
class GuardDoesNotMatch extends MakladException
{
public static function create(string $givenGuard, Collection $expectedGuards)
/**
* GuardDoesNotMatch constructor.
*
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
$expect = $expectedGuards->implode(', ');
$message = new static("The given role or permission should use guard `{$expect}` instead of `{$givenGuard}`.");

if (config('permission.log_registration_exception')) {
$logger = app('log');
$logger->alert($message);
}

return $message;
parent::__construct($message, $code, $previous);
}
}
32 changes: 32 additions & 0 deletions src/Exceptions/MakladException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: mostafamaklad
* Date: 9/6/17
* Time: 9:59 PM
*/

namespace Maklad\Permission\Exceptions;

use InvalidArgumentException;
use Throwable;

class MakladException extends InvalidArgumentException
{
/**
* MakladException constructor.
*
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);

if (config('permission.log_registration_exception')) {
$logger = app('log');
$logger->alert($message);
}
}
}
22 changes: 11 additions & 11 deletions src/Exceptions/PermissionAlreadyExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Maklad\Permission\Exceptions;

use InvalidArgumentException;
use Throwable;

class PermissionAlreadyExists extends InvalidArgumentException
class PermissionAlreadyExists extends MakladException
{
public static function create(string $permissionName, string $guardName)
/**
* PermissionAlreadyExists constructor.
*
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
$message = new static("A permission `{$permissionName}` already exists for guard `{$guardName}`.");

if (config('permission.log_registration_exception')) {
$logger = app('log');
$logger->alert($message);
}

return $message;
parent::__construct($message, $code, $previous);
}
}
22 changes: 11 additions & 11 deletions src/Exceptions/PermissionDoesNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Maklad\Permission\Exceptions;

use InvalidArgumentException;
use Throwable;

class PermissionDoesNotExist extends InvalidArgumentException
class PermissionDoesNotExist extends MakladException
{
public static function create(string $permissionName, string $guardName = '')
/**
* PermissionDoesNotExist constructor.
*
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
$message = new static("There is no permission named `{$permissionName}` for guard `{$guardName}`.");

if (config('permission.log_registration_exception')) {
$logger = app('log');
$logger->alert($message);
}

return $message;
parent::__construct($message, $code, $previous);
}
}
22 changes: 11 additions & 11 deletions src/Exceptions/RoleAlreadyExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Maklad\Permission\Exceptions;

use InvalidArgumentException;
use Throwable;

class RoleAlreadyExists extends InvalidArgumentException
class RoleAlreadyExists extends MakladException
{
public static function create(string $roleName, string $guardName)
/**
* RoleAlreadyExists constructor.
*
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
$message = new static("A role `{$roleName}` already exists for guard `{$guardName}`.");

if (config('permission.log_registration_exception')) {
$logger = app('log');
$logger->alert($message);
}

return $message;
parent::__construct($message, $code, $previous);
}
}
22 changes: 11 additions & 11 deletions src/Exceptions/RoleDoesNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Maklad\Permission\Exceptions;

use InvalidArgumentException;
use Throwable;

class RoleDoesNotExist extends InvalidArgumentException
class RoleDoesNotExist extends MakladException
{
public static function create(string $roleName)
/**
* RoleDoesNotExist constructor.
*
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
$message = new static("There is no role named `{$roleName}`.");

if (config('permission.log_registration_exception')) {
$logger = app('log');
$logger->alert($message);
}

return $message;
parent::__construct($message, $code, $previous);
}
}
68 changes: 65 additions & 3 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,80 @@

namespace Maklad\Permission;

use Illuminate\Support\Collection;

/**
* Class Helpers
* @package Maklad\Permission
*/
class Helpers
{
/**
* @param string $guard
*
* @return string|null
*/
public static function getModelForGuard(string $guard)
public function getModelForGuard(string $guard)
{
return collect(config('auth.guards'))
return \collect(\config('auth.guards'))
->map(function ($guard) {
return config("auth.providers.{$guard['provider']}.model");
return \config("auth.providers.{$guard['provider']}.model");
})->get($guard);
}

/**
* @param Collection $expected
* @param string $given
*
* @return string
*/
public function getGuardDoesNotMatchMessage(Collection $expected, string $given): string
{
return "The given role or permission should use guard `{$expected->implode(', ')}` instead of `{$given}`.";
}

/**
* @param string $name
* @param string $guardName
*
* @return string
*/
public function getPermissionAlreadyExistsMessage(string $name, string $guardName): string
{
return "A permission `{$name}` already exists for guard `{$guardName}`.";
}

/**
* @param string $name
* @param string $guardName
*
* @return string
*/
public function getPermissionDoesNotExistMessage(string $name, string $guardName): string
{
return "There is no permission named `{$name}` for guard `{$guardName}`.";
}

/**
* @param string $name
* @param string $guardName
*
* @return string
*/
public function getRoleAlreadyExistsMessage(string $name, string $guardName): string
{
return "A role `{$name}` already exists for guard `{$guardName}`.";
}

/**
* @param string $name
*
* @param string $guardName
*
* @return string
*/
public function getRoleDoesNotExistMessage(string $name, string $guardName): string
{
return "There is no role named `{$name}` for guard `{$guardName}`.";
}
}
42 changes: 32 additions & 10 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,43 @@ class Permission extends Model implements PermissionInterface
use RefreshesPermissionCache;

public $guarded = ['id'];
protected $helpers;

/**
* Permission constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

parent::__construct($attributes);

$this->setTable(config('permission.table_names.permissions'));
$this->helpers = new Helpers();

$this->setTable(\config('permission.table_names.permissions'));
}

/**
* Create new Permission
* @param array $attributes
*
* @return $this|\Illuminate\Database\Eloquent\Model
* @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists
*/
public static function create(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

if (static::getPermissions()->where('name', $attributes['name'])->where(
'guard_name',
$attributes['guard_name']
)->first()) {
throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
$name = $attributes['name'];
$guardName = $attributes['guard_name'];
$helpers = new Helpers();
throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guardName));
}

return static::query()->create($attributes);
Expand All @@ -48,17 +66,18 @@ public static function create(array $attributes = [])
public function roles(): BelongsToMany
{
return $this->belongsToMany(
config('permission.models.role'),
config('permission.table_names.role_has_permissions')
\config('permission.models.role'),
\config('permission.table_names.role_has_permissions')
);
}

/**
* A permission belongs to some users of the model associated with its guard.
* @return BelongsToMany
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(Helpers::getModelForGuard($this->attributes['guard_name']));
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
}

/**
Expand All @@ -68,25 +87,28 @@ public function users(): BelongsToMany
* @param string|null $guardName
*
* @return PermissionInterface
* @throws PermissionDoesNotExist
*/
public static function findByName(string $name, $guardName = null): PermissionInterface
{
$guardName = $guardName ?? config('auth.defaults.guard');
$guardName = $guardName ?? \config('auth.defaults.guard');

$permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();

if (! $permission) {
throw PermissionDoesNotExist::create($name, $guardName);
$helpers = new Helpers();
throw new PermissionDoesNotExist($helpers->getPermissionDoesNotExistMessage($name, $guardName));
}

return $permission;
}

/**
* Get the current cached permissions.
* @return Collection
*/
protected static function getPermissions(): Collection
{
return app(PermissionRegistrar::class)->getPermissions();
return \app(PermissionRegistrar::class)->getPermissions();
}
}
Loading

0 comments on commit 7ef323f

Please sign in to comment.