Skip to content

Commit

Permalink
Use ServiceDefinition class for ToolProxy service
Browse files Browse the repository at this point in the history
  • Loading branch information
spvickers committed Sep 13, 2023
1 parent 7471fa5 commit d65a062
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/MediaType/SecurityContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions src/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
18 changes: 11 additions & 7 deletions src/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit d65a062

Please sign in to comment.