-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from koriym/InvokeRequestInterface
Invoke request interface
- Loading branch information
Showing
9 changed files
with
115 additions
and
51 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
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\Resource; | ||
|
||
interface InvokeRequestInterface | ||
{ | ||
/** | ||
* Invokes a request using the given invoker and request objects. | ||
* | ||
* @return ResourceObject The resulting resource object returned from the request invocation. | ||
*/ | ||
public function _invokeRequest(InvokerInterface $invoker, AbstractRequest $request): ResourceObject; | ||
} |
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
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,51 @@ | ||
<?php | ||
// phpcs:ignoreFile | ||
|
||
namespace BEAR\Resource; | ||
|
||
use BEAR\Resource\Exception\BadRequestException; | ||
use Throwable; | ||
use function call_user_func_array; | ||
use function is_callable; | ||
use function ucfirst; | ||
|
||
final class PhpClassInvoker implements InvokerInterface | ||
{ | ||
public function __construct( | ||
private NamedParameterInterface $params, | ||
private ExtraMethodInvoker $extraMethod, | ||
private LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function invoke(AbstractRequest $request): ResourceObject | ||
{ | ||
$callable = [$request->resourceObject, 'on' . ucfirst($request->method)]; | ||
if (! is_callable($callable)) { | ||
// OPTIONS or HEAD | ||
return ($this->extraMethod)($request, $this); | ||
} | ||
|
||
$params = $this->params->getParameters($callable, $request->query); | ||
/** @psalm-suppress MixedAssignment */ | ||
try { | ||
$response = call_user_func_array($callable, $params); | ||
} catch (Throwable $e) { | ||
if ($e::class === \TypeError::class) { | ||
throw new BadRequestException('Invalid parameter type', Code::BAD_REQUEST, $e); | ||
} | ||
throw $e; | ||
} | ||
if (! $response instanceof ResourceObject) { | ||
$request->resourceObject->body = $response; | ||
$response = $request->resourceObject; | ||
} | ||
|
||
($this->logger)($response); | ||
|
||
return $response; | ||
} | ||
} |
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