Skip to content

Commit d65a062

Browse files
committed
Use ServiceDefinition class for ToolProxy service
1 parent 7471fa5 commit d65a062

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/MediaType/SecurityContract.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ function __construct(Tool $tool, string $secret)
4949
foreach ($tool->requiredServices as $requiredService) {
5050
foreach ($requiredService->formats as $format) {
5151
$service = $tool->findService($format, $requiredService->actions);
52-
if (($service !== false) && !array_key_exists($service->{'@id'}, $toolServices)) {
53-
$id = $service->{'@id'};
52+
if (($service !== false) && !array_key_exists($service->id, $toolServices)) {
53+
$id = $service->id;
5454
$parts = explode(':', $id, 2);
5555
if (count($parts) > 1) {
5656
if (array_key_exists($parts[0], $tcContexts)) {
5757
$id = "{$tcContexts[$parts[0]]}{$parts[1]}";
5858
}
5959
}
60-
$toolServices[$service->{'@id'}] = (object) [
60+
$toolServices[$service->id] = (object) [
6161
'@type' => 'RestServiceProfile',
6262
'service' => $id,
6363
'action' => $requiredService->actions
@@ -68,15 +68,15 @@ function __construct(Tool $tool, string $secret)
6868
foreach ($tool->optionalServices as $optionalService) {
6969
foreach ($optionalService->formats as $format) {
7070
$service = $tool->findService($format, $optionalService->actions);
71-
if (($service !== false) && !array_key_exists($service->{'@id'}, $toolServices)) {
72-
$id = $service->{'@id'};
71+
if (($service !== false) && !array_key_exists($service->id, $toolServices)) {
72+
$id = $service->id;
7373
$parts = explode(':', $id, 2);
7474
if (count($parts) > 1) {
7575
if (array_key_exists($parts[0], $tcContexts)) {
7676
$id = "{$tcContexts[$parts[0]]}{$parts[1]}";
7777
}
7878
}
79-
$toolServices[$service->{'@id'}] = (object) [
79+
$toolServices[$service->id] = (object) [
8080
'@type' => 'RestServiceProfile',
8181
'service' => $id,
8282
'action' => $optionalService->actions

src/System.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ceLTIc\LTI\Jwt\Jwt;
1111
use ceLTIc\LTI\Jwt\ClientInterface;
1212
use ceLTIc\LTI\Tool;
13+
use ceLTIc\LTI\Profile\ServiceDefinition;
1314
use ceLTIc\LTI\Enum\LtiVersion;
1415
use ceLTIc\LTI\Enum\IdScope;
1516
use ceLTIc\LTI\Util;
@@ -990,14 +991,14 @@ public function signServiceRequest(string $url, string $method, string $type, ar
990991
/**
991992
* Perform a service request
992993
*
993-
* @param object $service Service object to be executed
994-
* @param string $method HTTP action
995-
* @param string $format Media type
996-
* @param array|string $data Array of parameters or body string
994+
* @param ServiceDefinition $service Service object to be executed
995+
* @param string $method HTTP action
996+
* @param string $format Media type
997+
* @param array|string $data Array of parameters or body string
997998
*
998999
* @return HttpMessage HTTP object containing request and response details
9991000
*/
1000-
public function doServiceRequest(object $service, string $method, string $format, array|string $data): HttpMessage
1001+
public function doServiceRequest(ServiceDefinition $service, string $method, string $format, array|string $data): HttpMessage
10011002
{
10021003
$header = $this->addSignature($service->endpoint, $data, $method, $format);
10031004

src/Tool.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ceLTIc\LTI\DataConnector\DataConnector;
77
use ceLTIc\LTI\MediaType;
88
use ceLTIc\LTI\Profile;
9+
use ceLTIc\LTI\Profile\ServiceDefinition;
910
use ceLTIc\LTI\Content\Item;
1011
use ceLTIc\LTI\Jwt\Jwt;
1112
use ceLTIc\LTI\Http\HttpMessage;
@@ -524,16 +525,14 @@ public function getPlatforms(): array
524525
* @param string $format Media type required
525526
* @param array $methods Array of HTTP actions required
526527
*
527-
* @return object|bool The service object if found, otherwise false
528+
* @return ServiceDefinition|bool The service object if found, otherwise false
528529
*/
529-
public function findService(string $format, array $methods): object|bool
530+
public function findService(string $format, array $methods): ServiceDefinition|bool
530531
{
531532
$found = false;
532533
$services = $this->platform->profile->service_offered;
533534
if (is_array($services)) {
534-
$n = -1;
535535
foreach ($services as $service) {
536-
$n++;
537536
if (!is_array($service->format) || !in_array($format, $service->format)) {
538537
continue;
539538
}
@@ -543,9 +542,14 @@ public function findService(string $format, array $methods): object|bool
543542
$missing[] = $method;
544543
}
545544
}
546-
$methods = $missing;
547-
if (count($methods) <= 0) {
548-
$found = $service;
545+
if (count($missing) <= 0) {
546+
$found = new ServiceDefinition($service->format, $service->action);
547+
if (!empty($service->{'@id'})) {
548+
$found->id = $service->{'@id'};
549+
}
550+
if (!empty($service->endpoint)) {
551+
$found->endpoint = $service->endpoint;
552+
}
549553
break;
550554
}
551555
}

0 commit comments

Comments
 (0)