From d65a06238fdec12a49e4be7083733ad594814e68 Mon Sep 17 00:00:00 2001 From: Stephen Vickers Date: Wed, 13 Sep 2023 22:04:23 +0100 Subject: [PATCH] Use ServiceDefinition class for ToolProxy service --- src/MediaType/SecurityContract.php | 12 ++++++------ src/System.php | 11 ++++++----- src/Tool.php | 18 +++++++++++------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/MediaType/SecurityContract.php b/src/MediaType/SecurityContract.php index 08bbf35..58b1a09 100644 --- a/src/MediaType/SecurityContract.php +++ b/src/MediaType/SecurityContract.php @@ -49,15 +49,15 @@ function __construct(Tool $tool, string $secret) foreach ($tool->requiredServices as $requiredService) { foreach ($requiredService->formats as $format) { $service = $tool->findService($format, $requiredService->actions); - if (($service !== false) && !array_key_exists($service->{'@id'}, $toolServices)) { - $id = $service->{'@id'}; + if (($service !== false) && !array_key_exists($service->id, $toolServices)) { + $id = $service->id; $parts = explode(':', $id, 2); if (count($parts) > 1) { if (array_key_exists($parts[0], $tcContexts)) { $id = "{$tcContexts[$parts[0]]}{$parts[1]}"; } } - $toolServices[$service->{'@id'}] = (object) [ + $toolServices[$service->id] = (object) [ '@type' => 'RestServiceProfile', 'service' => $id, 'action' => $requiredService->actions @@ -68,15 +68,15 @@ function __construct(Tool $tool, string $secret) foreach ($tool->optionalServices as $optionalService) { foreach ($optionalService->formats as $format) { $service = $tool->findService($format, $optionalService->actions); - if (($service !== false) && !array_key_exists($service->{'@id'}, $toolServices)) { - $id = $service->{'@id'}; + if (($service !== false) && !array_key_exists($service->id, $toolServices)) { + $id = $service->id; $parts = explode(':', $id, 2); if (count($parts) > 1) { if (array_key_exists($parts[0], $tcContexts)) { $id = "{$tcContexts[$parts[0]]}{$parts[1]}"; } } - $toolServices[$service->{'@id'}] = (object) [ + $toolServices[$service->id] = (object) [ '@type' => 'RestServiceProfile', 'service' => $id, 'action' => $optionalService->actions diff --git a/src/System.php b/src/System.php index 8ff5df6..ebe6834 100644 --- a/src/System.php +++ b/src/System.php @@ -10,6 +10,7 @@ use ceLTIc\LTI\Jwt\Jwt; use ceLTIc\LTI\Jwt\ClientInterface; use ceLTIc\LTI\Tool; +use ceLTIc\LTI\Profile\ServiceDefinition; use ceLTIc\LTI\Enum\LtiVersion; use ceLTIc\LTI\Enum\IdScope; use ceLTIc\LTI\Util; @@ -990,14 +991,14 @@ public function signServiceRequest(string $url, string $method, string $type, ar /** * Perform a service request * - * @param object $service Service object to be executed - * @param string $method HTTP action - * @param string $format Media type - * @param array|string $data Array of parameters or body string + * @param ServiceDefinition $service Service object to be executed + * @param string $method HTTP action + * @param string $format Media type + * @param array|string $data Array of parameters or body string * * @return HttpMessage HTTP object containing request and response details */ - public function doServiceRequest(object $service, string $method, string $format, array|string $data): HttpMessage + public function doServiceRequest(ServiceDefinition $service, string $method, string $format, array|string $data): HttpMessage { $header = $this->addSignature($service->endpoint, $data, $method, $format); diff --git a/src/Tool.php b/src/Tool.php index 5b7db86..4a4a089 100644 --- a/src/Tool.php +++ b/src/Tool.php @@ -6,6 +6,7 @@ use ceLTIc\LTI\DataConnector\DataConnector; use ceLTIc\LTI\MediaType; use ceLTIc\LTI\Profile; +use ceLTIc\LTI\Profile\ServiceDefinition; use ceLTIc\LTI\Content\Item; use ceLTIc\LTI\Jwt\Jwt; use ceLTIc\LTI\Http\HttpMessage; @@ -524,16 +525,14 @@ public function getPlatforms(): array * @param string $format Media type required * @param array $methods Array of HTTP actions required * - * @return object|bool The service object if found, otherwise false + * @return ServiceDefinition|bool The service object if found, otherwise false */ - public function findService(string $format, array $methods): object|bool + public function findService(string $format, array $methods): ServiceDefinition|bool { $found = false; $services = $this->platform->profile->service_offered; if (is_array($services)) { - $n = -1; foreach ($services as $service) { - $n++; if (!is_array($service->format) || !in_array($format, $service->format)) { continue; } @@ -543,9 +542,14 @@ public function findService(string $format, array $methods): object|bool $missing[] = $method; } } - $methods = $missing; - if (count($methods) <= 0) { - $found = $service; + if (count($missing) <= 0) { + $found = new ServiceDefinition($service->format, $service->action); + if (!empty($service->{'@id'})) { + $found->id = $service->{'@id'}; + } + if (!empty($service->endpoint)) { + $found->endpoint = $service->endpoint; + } break; } }