Skip to content

Commit

Permalink
Merge pull request #8 from SolumDeSignum/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Faks authored Mar 20, 2024
2 parents 79dd2f2 + 16a5e9a commit 11d6b12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
],
"homepage": "https://github.com/solumdesignum/scenarios",
"require": {
"php": "^8.0|^8.1|^8.2",
"illuminate/support": "~9|~10"
"php": "^8.1|^8.2|^8.3",
"illuminate/support": "~10|~11"
},
"require-dev": {
},
Expand Down
55 changes: 19 additions & 36 deletions src/Scenarios.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace SolumDeSignum\Scenarios;

use Exception;
use Illuminate\Support\Facades\Route;
use function config;
use function is_bool;

trait Scenarios
{
Expand All @@ -21,69 +20,61 @@ trait Scenarios
*
* Scenarios constructor.
*
* @throws \Exception
* @throws Exception
*/
public function __construct()
{
// Set Config
$this->setMethodFromController = config(
'scenarios.features.setMethodFromController',
true
);
$this->setMethodFromUrl = config(
'scenarios.features.setMethodFromUrlSegment',
false
);
$this->setMethodFromController = config('scenarios.features.setMethodFromController', true);
$this->setMethodFromUrl = config('scenarios.features.setMethodFromUrlSegment', false);

// Detect package abuse
$this->exceptionOneSetMethod();
$this->exceptionOnlyOneSetMethod();

// setMethod based on Controller function
if ($this->setMethodFromController) {
$this->scenario = $this->patternFilter($this->currentControllerMethod());
}

// setMethod based on Request segment based on $controllerMethodPattern
if ($this->setMethodFromUrl) {
$this->scenario = $this->patternFilter($this->currentRequestUri());
}
}

private function exceptionOneSetMethod(): void
/**
* @throws Exception
*/
public function exceptionOneSetMethod(): void
{
if (
!is_bool($this->setMethodFromController) ||
!is_bool($this->setMethodFromUrl) ||
($this->setMethodFromController === false && $this->setMethodFromUrl === false)
) {
throw new \Exception(
throw new Exception(
'Please enable at least one setMethod function, LIKE RIGHT NOW !!!'
);
}
}

private function exceptionOnlyOneSetMethod(): void
/**
* @throws Exception
*/
public function exceptionOnlyOneSetMethod(): void
{
if (
!is_bool($this->setMethodFromController) ||
!is_bool($this->setMethodFromUrl) ||
($this->setMethodFromController === true && $this->setMethodFromUrl === true)
) {
throw new \Exception(
throw new Exception(
'Please enable only one setMethod function, LIKE RIGHT NOW !!!'
);
}
}

/**
* @param $method
*
* @return string
* @throws \Exception
*
* @throws Exception
*/
public function patternFilter($method): string
public function patternFilter(string $method): string
{
preg_match_all(
config('scenarios.methods.pattern'),
Expand All @@ -97,32 +88,24 @@ public function patternFilter($method): string
}

/**
* @param mixed $matches
*
* @throws \Exception
* @throws Exception
*/
private function exceptionScenarioPattern($matches): void
public function exceptionScenarioPattern(mixed $matches): void
{
if (!isset($matches)) {
throw new \Exception(
throw new Exception(
'Scenarios patternFilter failed finding match, check $scenarioPattern , LIKE RIGHT NOW !!!'
);
}
}

/**
* @return string|null
*/
public function currentControllerMethod(): ?string
{
return Route::current() !== null ?
Route::current()->getActionMethod() :
null;
}

/**
* @return string|null
*/
public function currentRequestUri(): ?string
{
return Route::getCurrentRequest() !== null ?
Expand Down

0 comments on commit 11d6b12

Please sign in to comment.