Skip to content

Commit c2264d0

Browse files
committed
FORMS-984: Code cleanup
1 parent 2cc6943 commit c2264d0

File tree

2 files changed

+37
-48
lines changed

2 files changed

+37
-48
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ adding query parameters to the URL:
136136
| Name | Value | Example |
137137
|-------------|----------------------|--------------|
138138
| `starttime` | [PHP Date and Time Formats](https://www.php.net/manual/en/datetime.formats.php) | `yesterday` |
139-
| `endtime` | PHP DateTime formats | `2023-10-23` |
139+
| `endtime` | [PHP Date and Time Formats](https://www.php.net/manual/en/datetime.formats.php) | `2023-10-23` |
140140

141141
If left out, filtering upon the left out parameter will not be done.
142142

143-
This example requests all submissions after on or after October 1st, 2023:
143+
This example requests all submissions on or after October 1st, 2023:
144144

145145
Request:
146146

@@ -158,7 +158,7 @@ Response:
158158
"123": "https://127.0.0.1:8000/da/webform_rest/some_webform_id/submission/44b1fe1b-ee96-481e-b941-d1219d1dcb55",
159159
"124": "https://127.0.0.1:8000/da/webform_rest/some_webform_id/submission/3652836d-3dab-4919-b880-e82cbbf3c24c"
160160
}
161-
}```
161+
}
162162
```
163163

164164
## Custom access control

src/Plugin/rest/resource/WebformAllFormSubmissions.php

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
66
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
7-
use Drupal\Core\Entity\Query\QueryInterface;
87
use Drupal\Core\Url;
98
use Drupal\os2forms_rest_api\WebformHelper;
109
use Drupal\rest\ModifiedResourceResponse;
1110
use Drupal\rest\Plugin\ResourceBase;
1211
use Symfony\Component\DependencyInjection\ContainerInterface;
13-
use Symfony\Component\HttpFoundation\Request;
1412
use Symfony\Component\HttpFoundation\Response;
1513

1614
/**
@@ -131,68 +129,59 @@ public function get(string $webform_id): ModifiedResourceResponse {
131129
$submissionQuery = $submissionEntityStorage->getQuery()
132130
->condition('webform_id', $webform_id);
133131

134-
foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) {
135-
$errors = $this->updateSubmissionQuery($this->currentRequest, $submissionQuery, $param, $operator, $result);
132+
$requestQuery = $this->currentRequest->query;
136133

137-
if (isset($errors['error'])) {
138-
return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST);
134+
foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) {
135+
$value = $requestQuery->get($param);
136+
137+
if (!empty($value)) {
138+
try {
139+
$dateTime = new \DateTimeImmutable($value);
140+
$submissionQuery->condition('created', $dateTime->getTimestamp(), $operator);
141+
$result[$param] = $value;
142+
}
143+
catch (\Exception $e) {
144+
$errors = [
145+
'error' => [
146+
'message' => $this->t('Could not generate DateTime from :time', [':time' => $value]),
147+
],
148+
];
149+
150+
return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST);
151+
}
139152
}
140153
}
141154

142155
// Complete query.
143156
$submissionQuery->accessCheck(FALSE);
144157
$sids = $submissionQuery->execute();
145158

146-
$submissionData = [];
147-
148-
if (!empty($sids)) {
149-
$submissions = $submissionEntityStorage->loadMultiple($sids);
150-
151-
foreach ($submissions as $submission) {
152-
$url = Url::fromRoute(
159+
// Generate submission URLs.
160+
try {
161+
$result['submissions'] = array_map(
162+
static fn($submission) => Url::fromRoute(
153163
'rest.webform_rest_submission.GET',
154164
[
155165
'webform_id' => $webform_id,
156166
'uuid' => $submission->uuid(),
157167
]
158168
)
159169
->setAbsolute()
160-
->toString(TRUE)->getGeneratedUrl();
161-
162-
$submissionData[$submission->id()] = $url;
163-
}
170+
->toString(TRUE)->getGeneratedUrl(),
171+
$submissionEntityStorage->loadMultiple($sids ?: [])
172+
);
164173
}
174+
catch (\Exception $e) {
175+
$errors = [
176+
'error' => [
177+
'message' => $this->t('Could not generate submission URLs'),
178+
],
179+
];
165180

166-
$result['submissions'] = $submissionData;
167-
168-
return new ModifiedResourceResponse($result);
169-
}
170-
171-
/**
172-
* Updates submission query with request query parameters.
173-
*
174-
* @phpstan-param array<string, mixed> $result
175-
* @phpstan-return array<string, mixed>
176-
*/
177-
private function updateSubmissionQuery(Request $request, QueryInterface $submissionQuery, string $parameter, string $operator, array &$result): array {
178-
$value = $request->query->get($parameter);
179-
180-
if (!empty($timeQuery)) {
181-
try {
182-
$startTime = new \DateTimeImmutable($timeQuery);
183-
$submissionQuery->condition('created', $startTime->getTimestamp(), $operator);
184-
$result[$parameter] = $timeQuery->format(\DateTimeImmutable::ATOM);
185-
}
186-
catch (\Exception $e) {
187-
$errors = [
188-
'error' => [
189-
'message' => $this->t('Could not generate DateTime from :time', [':time' => $timeQuery]),
190-
],
191-
];
192-
}
181+
return new ModifiedResourceResponse($errors, Response::HTTP_INTERNAL_SERVER_ERROR);
193182
}
194183

195-
return $errors ?? [];
184+
return new ModifiedResourceResponse($result);
196185
}
197186

198187
}

0 commit comments

Comments
 (0)