wiki.techinc.nl/includes/Rest/BasicAccess/CompoundAuthorizer.php
Tim Starling 580ec48e5b Fix more PHPStorm inspections (#2)
* Illegal string offset and invalid argument supplied to foreach, due to incorrect type information
* Array internal pointer reset is unnecessary
* $hookData unused since MW 1.35 due to incomplete revert
* array_push() with single element
* Unnecessary sprintf()
* for loop can be replaced with str_repeat()
* preg_replace() can be replaced with rtrim()
* array_values() call is redundant
* Unnecessary cast to string
* Unnecessary ternary. Often the result relies on short-circuit evaluation, but I find it more readable nonetheless.

Change-Id: I4c45bdb59b51b243fa96286bec8b58deb097d707
2023-03-25 00:19:58 +00:00

51 lines
1.2 KiB
PHP

<?php
namespace MediaWiki\Rest\BasicAccess;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\RequestInterface;
/**
* Wraps an array of BasicAuthorizerInterface and checks them
* all to authorize the request
* @internal
* @package MediaWiki\Rest\BasicAccess
*/
class CompoundAuthorizer implements BasicAuthorizerInterface {
/** @var BasicAuthorizerInterface[] */
private $authorizers;
/**
* @param array $authorizers
*/
public function __construct( array $authorizers = [] ) {
$this->authorizers = $authorizers;
}
/**
* Adds a BasicAuthorizerInterface to the chain of authorizers.
* @param BasicAuthorizerInterface $authorizer
* @return CompoundAuthorizer
*/
public function addAuthorizer( BasicAuthorizerInterface $authorizer ): CompoundAuthorizer {
$this->authorizers[] = $authorizer;
return $this;
}
/**
* Checks all registered authorizers and returns the first encountered error.
* @param RequestInterface $request
* @param Handler $handler
* @return string|null
*/
public function authorize( RequestInterface $request, Handler $handler ) {
foreach ( $this->authorizers as $authorizer ) {
$result = $authorizer->authorize( $request, $handler );
if ( $result ) {
return $result;
}
}
return null;
}
}