From 7147302773e3d5c0a04f2c7a78547e15f1862997 Mon Sep 17 00:00:00 2001 From: Murtaza Sultani Date: Thu, 5 Mar 2026 17:11:43 +0100 Subject: Refactor --- app/controllers/lti/1p3/ags/line_item.php | 34 ++++++++--- app/controllers/lti/1p3/token.php | 74 +++++++++++++---------- lib/classes/Lti/LTI1p3/KeyManager.php | 2 +- lib/classes/Lti/LTI1p3/ResourceLinkRepository.php | 3 +- 4 files changed, 71 insertions(+), 42 deletions(-) diff --git a/app/controllers/lti/1p3/ags/line_item.php b/app/controllers/lti/1p3/ags/line_item.php index 6432bb0..61cfcaa 100644 --- a/app/controllers/lti/1p3/ags/line_item.php +++ b/app/controllers/lti/1p3/ags/line_item.php @@ -28,14 +28,32 @@ final class Lti_1p3_Ags_LineItemController extends AuthenticatedController public function index_action(): void { - $requestHandler = match (Request::method()) { - 'PUT' => new UpdateLineItemServiceServerRequestHandler($this->lineItemRepo), - 'DELETE' => new DeleteLineItemServiceServerRequestHandler($this->lineItemRepo), - 'GET' => new GetLineItemServiceServerRequestHandler($this->lineItemRepo), - default => throw new MethodNotAllowedException() - }; - - $this->renderAgsResponse($requestHandler); + try { + $requestHandler = match (Request::method()) { + 'PUT' => new UpdateLineItemServiceServerRequestHandler($this->lineItemRepo), + 'DELETE' => new DeleteLineItemServiceServerRequestHandler($this->lineItemRepo), + 'GET' => new GetLineItemServiceServerRequestHandler($this->lineItemRepo), + default => throw new MethodNotAllowedException() + }; + + $this->renderAgsResponse($requestHandler); + + } catch (\Throwable $e) { + $requestBody = $this->getPsrRequest()->getBody()->getContents(); + + $response = new \Nyholm\Psr7\Response( + 500, + ['Content-Type' => 'application/json'], + json_encode([ + 'message' => $e->getMessage(), + 'request_body' => $requestBody, + ]) + ); + + $this->renderPsrResponse($response); + } + + $this->set_layout(null); } public function results_action(): void diff --git a/app/controllers/lti/1p3/token.php b/app/controllers/lti/1p3/token.php index 6cc0faf..baea5db 100644 --- a/app/controllers/lti/1p3/token.php +++ b/app/controllers/lti/1p3/token.php @@ -20,39 +20,49 @@ final class Lti_1p3_TokenController extends AuthenticatedController protected $with_session = false; use NegotiatesWithPsr7; - public function __construct( - protected Dispatcher $dispatcher, - protected OidcAuthenticationRequestHandler $oidcLoginHandler - ) - { - parent::__construct($dispatcher); - } - public function index_action(): void { - $platformEncryptionKey = PlatformManager::getPrivateKey()->getContent(); - $responseGenerator = new AccessTokenResponseGenerator( - new KeyManager(), - new AuthorizationServerFactory( - new ClientRepository(new RegistrationManager()), - new AccessTokenRepository(Factory::getCache()), - new ScopeRepository( - [ - new ScopeEntity('https://purl.imsglobal.org/spec/lti-ags/scope/lineitem'), - new ScopeEntity('https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly'), - new ScopeEntity('https://purl.imsglobal.org/spec/lti-ags/scope/score') - ] - ), - $platformEncryptionKey - ) - ); - - $response = $responseGenerator->generate( - $this->getPsrRequest(), - $this->getPsrResponse(), - '1' - ); - - $this->renderPsrResponse($response); + try { + $platformEncryptionKey = PlatformManager::getPrivateKey()->getContent(); + $responseGenerator = new AccessTokenResponseGenerator( + new KeyManager(), + new AuthorizationServerFactory( + new ClientRepository(new RegistrationManager()), + new AccessTokenRepository(Factory::getCache()), + new ScopeRepository( + [ + new ScopeEntity('https://purl.imsglobal.org/spec/lti-ags/scope/lineitem'), + new ScopeEntity('https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly'), + new ScopeEntity('https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly'), + new ScopeEntity('https://purl.imsglobal.org/spec/lti-ags/scope/score') + ] + ), + $platformEncryptionKey + ) + ); + + $response = $responseGenerator->generate( + $this->getPsrRequest(), + $this->getPsrResponse(), + '1' + ); + + $this->renderPsrResponse($response); + } catch (\Throwable $e) { + $requestBody = $this->getPsrRequest()->getBody()->getContents(); + + $response = new \Nyholm\Psr7\Response( + 500, + ['Content-Type' => 'application/json'], + json_encode([ + 'message' => $e->getMessage(), + 'request_body' => $requestBody, + ]) + ); + + $this->renderPsrResponse($response); + } + + $this->set_layout(null); } } diff --git a/lib/classes/Lti/LTI1p3/KeyManager.php b/lib/classes/Lti/LTI1p3/KeyManager.php index 5d7e8d8..e38e4b9 100644 --- a/lib/classes/Lti/LTI1p3/KeyManager.php +++ b/lib/classes/Lti/LTI1p3/KeyManager.php @@ -9,7 +9,7 @@ final class KeyManager implements KeyChainRepositoryInterface { public function find(string $identifier): ?KeyChainInterface { - return Keyring::findOneByRange_id($identifier)?->toKeyChain(); + return Keyring::find($identifier)?->toKeyChain(); } public function findByKeySetName(string $keySetName): array diff --git a/lib/classes/Lti/LTI1p3/ResourceLinkRepository.php b/lib/classes/Lti/LTI1p3/ResourceLinkRepository.php index 6bb2f90..a188eac 100644 --- a/lib/classes/Lti/LTI1p3/ResourceLinkRepository.php +++ b/lib/classes/Lti/LTI1p3/ResourceLinkRepository.php @@ -131,8 +131,9 @@ final class ResourceLinkRepository implements LtiResourceLinkInterface { $lineItemsContainerUrl = URLHelper::getURL('dispatch.php/lti/1p3/ags/line_items', ['resource_link_id' => $this->resourceLink->id], true); $lineItemURL = URLHelper::getURL('dispatch.php/lti/1p3/ags/line_item', ['resource_link_id' => $this->resourceLink->id], true); + $gradeSynchronization = (int) $this->resourceLink->getConfigValues()['grade_synchronization']; - return match ((int) $this->resourceLink->getConfigValues()['grade_synchronization']) { + return match ($gradeSynchronization) { GradeSynchronization::GradeSyncOnly->value => new AgsClaim( [ 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly', -- cgit v1.0