-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Php\Support\Laravel\Traits\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Php\Support\Exceptions\MethodNotAllowedException; | ||
|
||
/** | ||
* @mixin Model | ||
*/ | ||
trait AllowToExecute | ||
{ | ||
protected static array $disallowMethodsMap = []; | ||
protected array $allowList = []; | ||
|
||
public function addMethodToAllowList(string $method): void | ||
{ | ||
$this->allowList[$method] = true; | ||
} | ||
|
||
private function rejectMethodFromAllowList(string $method): void | ||
{ | ||
unset($this->allowList[$method]); | ||
} | ||
|
||
protected function isAllowToExecute(string $method): bool | ||
{ | ||
return !array_key_exists($method, static::$disallowMethodsMap) || isset($this->allowList[$method]); | ||
} | ||
|
||
protected function checkPossibilityAndExecute(string $method, ...$arguments): mixed | ||
{ | ||
if ($this->isAllowToExecute($method)) { | ||
return parent::$method(...$arguments); | ||
} | ||
$hint = static::$disallowMethodsMap[$method]; | ||
|
||
if ($hint instanceof \Closure) { | ||
return $hint($this); | ||
} | ||
|
||
if (is_string($hint)) { | ||
$text = "You should call this method like this: $hint"; | ||
} | ||
|
||
throw new MethodNotAllowedException($text ?? 'You don\'t allow to execute this method!'); | ||
} | ||
|
||
protected static function addMethodToDisallowMap(string $method, string|\Closure $hint = null): void | ||
{ | ||
static::$disallowMethodsMap[$method] = $hint; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Php\Support\Laravel\Tests\Traits\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Php\Support\Exceptions\MethodNotAllowedException; | ||
use Php\Support\Laravel\Tests\AbstractTestCase; | ||
use Php\Support\Laravel\Traits\Models\AllowToExecute; | ||
|
||
class AllowToExecuteTest extends AbstractTestCase | ||
{ | ||
|
||
/** @test */ | ||
public function callBase() | ||
{ | ||
$class = $this->buildClass(); | ||
|
||
$this->expectException(MethodNotAllowedException::class); | ||
$this->expectExceptionMessage( | ||
'Method Not Allowed: You should call this method like this: Remover::exec($model)' | ||
); | ||
|
||
$class->delete(); | ||
} | ||
|
||
/** @test */ | ||
public function callWithDisablePossibility() | ||
{ | ||
$class = $this->buildClass(); | ||
|
||
$this->expectException(MethodNotAllowedException::class); | ||
$this->expectExceptionMessage('Method Not Allowed: You should call this method like this: new Query($model)'); | ||
|
||
$class->newQuery(); | ||
} | ||
|
||
/** @test */ | ||
public function callWithPossibility() | ||
{ | ||
$class = $this->buildClass(); | ||
$class->addMethodToAllowList('newQuery'); | ||
|
||
$class->newQuery(); | ||
|
||
static::assertNotNull($class); | ||
} | ||
|
||
/** @test */ | ||
public function callFn() | ||
{ | ||
$class = $this->buildClass(); | ||
|
||
$this->expectException(MethodNotAllowedException::class); | ||
$this->expectExceptionMessage("QQ"); | ||
$class->handle(1, 2); | ||
} | ||
|
||
private function buildClass(): Model | ||
{ | ||
return new class extends Model { | ||
use AllowToExecute; | ||
|
||
protected static function booting(): void | ||
{ | ||
static::addMethodToDisallowMap('delete', 'Remover::exec($model)'); | ||
static::addMethodToDisallowMap('newQuery', 'new Query($model)'); | ||
static::addMethodToDisallowMap( | ||
'handle', | ||
static fn() => throw new MethodNotAllowedException("QQ") | ||
); | ||
} | ||
|
||
public function delete() | ||
{ | ||
return $this->checkPossibilityAndExecute(__FUNCTION__); | ||
} | ||
|
||
public function handle($value, $value2) | ||
{ | ||
return $this->checkPossibilityAndExecute(__FUNCTION__, $value, $value2); | ||
} | ||
|
||
public function newQuery() | ||
{ | ||
return $this->checkPossibilityAndExecute(__FUNCTION__); | ||
} | ||
}; | ||
} | ||
} |