-
Notifications
You must be signed in to change notification settings - Fork 2
/
BasicOutcomeServiceServerRequestHandler.php
162 lines (141 loc) · 6.26 KB
/
BasicOutcomeServiceServerRequestHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA;
*/
declare(strict_types=1);
namespace OAT\Library\Lti1p3BasicOutcome\Service\Server\Handler;
use InvalidArgumentException;
use Nyholm\Psr7\Response;
use OAT\Library\Lti1p3BasicOutcome\Factory\Response\BasicOutcomeResponseFactory;
use OAT\Library\Lti1p3BasicOutcome\Factory\Response\BasicOutcomeResponseFactoryInterface;
use OAT\Library\Lti1p3BasicOutcome\Message\BasicOutcomeMessageInterface;
use OAT\Library\Lti1p3BasicOutcome\Serializer\Request\BasicOutcomeRequestSerializer;
use OAT\Library\Lti1p3BasicOutcome\Serializer\Request\BasicOutcomeRequestSerializerInterface;
use OAT\Library\Lti1p3BasicOutcome\Serializer\Response\BasicOutcomeResponseSerializer;
use OAT\Library\Lti1p3BasicOutcome\Serializer\Response\BasicOutcomeResponseSerializerInterface;
use OAT\Library\Lti1p3BasicOutcome\Service\BasicOutcomeServiceInterface;
use OAT\Library\Lti1p3BasicOutcome\Service\Server\Processor\BasicOutcomeServiceServerProcessorInterface;
use OAT\Library\Lti1p3Core\Security\OAuth2\Validator\Result\RequestAccessTokenValidationResultInterface;
use OAT\Library\Lti1p3Core\Service\Server\Handler\LtiServiceServerRequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Throwable;
class BasicOutcomeServiceServerRequestHandler implements LtiServiceServerRequestHandlerInterface, BasicOutcomeServiceInterface
{
/** @var BasicOutcomeServiceServerProcessorInterface */
private $processor;
/** @var BasicOutcomeRequestSerializerInterface */
private $basicOutcomeRequestSerializer;
/** @var BasicOutcomeResponseSerializerInterface */
private $basicOutcomeResponseSerializer;
/** @var BasicOutcomeResponseFactoryInterface */
private $basicOutcomeResponseFactory;
/** @var LoggerInterface */
private $logger;
public function __construct(
BasicOutcomeServiceServerProcessorInterface $processor,
?BasicOutcomeRequestSerializerInterface $basicOutcomeRequestSerializer = null,
?BasicOutcomeResponseSerializerInterface $basicOutcomeResponseSerializer = null,
?BasicOutcomeResponseFactoryInterface $basicOutcomeResponseFactory = null,
?LoggerInterface $logger = null
) {
$this->processor = $processor;
$this->basicOutcomeRequestSerializer = $basicOutcomeRequestSerializer ?? new BasicOutcomeRequestSerializer();
$this->basicOutcomeResponseSerializer = $basicOutcomeResponseSerializer ?? new BasicOutcomeResponseSerializer();
$this->basicOutcomeResponseFactory = $basicOutcomeResponseFactory ?? new BasicOutcomeResponseFactory();
$this->logger = $logger ?? new NullLogger();
}
public function getServiceName(): string
{
return static::NAME;
}
public function getAllowedContentType(): ?string
{
return static::CONTENT_TYPE_BASIC_OUTCOME;
}
public function getAllowedMethods(): array
{
return [
'POST',
];
}
public function getAllowedScopes(): array
{
return [
static::AUTHORIZATION_SCOPE_BASIC_OUTCOME,
];
}
public function handleValidatedServiceRequest(
RequestAccessTokenValidationResultInterface $validationResult,
ServerRequestInterface $request,
array $options = []
): ResponseInterface {
$registration = $validationResult->getRegistration();
try {
$basicOutcomeRequest = $this->basicOutcomeRequestSerializer->deserialize((string)$request->getBody());
} catch (Throwable $exception) {
$this->logger->error($exception->getMessage());
return new Response(400, [], $exception->getMessage());
}
switch ($basicOutcomeRequest->getType()) {
case BasicOutcomeMessageInterface::TYPE_REPLACE_RESULT:
$result = $this->processor->processReplaceResult(
$registration,
$basicOutcomeRequest->getSourcedId(),
$basicOutcomeRequest->getScore(),
$basicOutcomeRequest->getLanguage()
);
break;
case BasicOutcomeMessageInterface::TYPE_READ_RESULT:
$result = $this->processor->processReadResult(
$registration,
$basicOutcomeRequest->getSourcedId()
);
break;
case BasicOutcomeMessageInterface::TYPE_DELETE_RESULT:
$result = $this->processor->processDeleteResult(
$registration,
$basicOutcomeRequest->getSourcedId()
);
break;
default:
throw new InvalidArgumentException(
sprintf('unsupported basic outcome type %s', $basicOutcomeRequest->getType())
);
}
$basicOutcomeResponse = $this->basicOutcomeResponseFactory->create(
$basicOutcomeRequest->getType(),
$result->isSuccess(),
$basicOutcomeRequest->getIdentifier(),
$basicOutcomeRequest->getType(),
$result->getMessage(),
$result->getScore(),
$result->getLanguage()
);
$responseBody = $this->basicOutcomeResponseSerializer->serialize($basicOutcomeResponse);
return new Response(
200,
[
'Content-Type' => static::CONTENT_TYPE_BASIC_OUTCOME,
'Content-Length' => strlen($responseBody),
],
$responseBody
);
}
}