Last active
September 21, 2016 17:14
-
-
Save victorjonsson/11aabb6dbd9ee592b7716aff992b1e23 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace Routing\Auth; | |
abstract class AuthChecker { | |
const SKIPPED; | |
const SUCCESS; | |
const AUTHENTICATION_FAILED; | |
const AUTHORIZATION_FAILED; | |
/** | |
* @var AuthChecker|null | |
*/ | |
private $next; | |
abstract protected validatRequest(Request $request, $controllerClass); | |
public function setNext(AuthChecker $authChecker) | |
{ | |
$this->next = $authChecker; | |
} | |
public function invoke(Request $request, $controllerClass) | |
{ | |
$result = $this->validateRequest($request, $controllerClass); | |
if ($result == self::SKIPPED && $this->next) { | |
return $this->next->invoke($request, $controllerClass); | |
} | |
return $result; | |
} | |
} | |
class AuthChainProvider { | |
/** | |
* @var AuthChecker | |
*/ | |
private $firstInChain; | |
static function getInstance() | |
{ | |
static $self = null; | |
if ($self === null) { | |
$self = new self; | |
} | |
return $self; | |
} | |
private function __construct() {} | |
public function chain(AuthChecker $authChecker) | |
{ | |
if ($this->firstInChain) { | |
$authChecker->setNext($this->firstInChain); | |
} | |
$this->firstInChain = $authChecker; | |
} | |
public function invoke(Request $request, $controllerClass) | |
{ | |
if ($this->firstInChain) { | |
return $this->firstInChain->invoke($request, $controllerClass); | |
} | |
return null; | |
} | |
} | |
/* | |
* EXAMPLE... | |
*/ | |
$authChainProvider = AuthChainProvider::getInstance(); | |
$authChainProvider->chain(new MainAuthChecker()); | |
$authChainProvider->chain(new WikiDocsAuthChecker()); | |
$authChainProvider->chain(new RestApiAuthChecker()); | |
$authChainProvider->invoke($request, $controllerClass); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment