|
4 | 4 |
|
5 | 5 | use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; |
6 | 6 | use Drupal\Component\Plugin\Exception\PluginNotFoundException; |
7 | | -use Drupal\Core\Entity\Query\QueryInterface; |
8 | 7 | use Drupal\Core\Url; |
9 | 8 | use Drupal\os2forms_rest_api\WebformHelper; |
10 | 9 | use Drupal\rest\ModifiedResourceResponse; |
11 | 10 | use Drupal\rest\Plugin\ResourceBase; |
12 | 11 | use Symfony\Component\DependencyInjection\ContainerInterface; |
13 | | -use Symfony\Component\HttpFoundation\Request; |
14 | 12 | use Symfony\Component\HttpFoundation\Response; |
15 | 13 |
|
16 | 14 | /** |
@@ -131,68 +129,59 @@ public function get(string $webform_id): ModifiedResourceResponse { |
131 | 129 | $submissionQuery = $submissionEntityStorage->getQuery() |
132 | 130 | ->condition('webform_id', $webform_id); |
133 | 131 |
|
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; |
136 | 133 |
|
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 | + } |
139 | 152 | } |
140 | 153 | } |
141 | 154 |
|
142 | 155 | // Complete query. |
143 | 156 | $submissionQuery->accessCheck(FALSE); |
144 | 157 | $sids = $submissionQuery->execute(); |
145 | 158 |
|
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( |
153 | 163 | 'rest.webform_rest_submission.GET', |
154 | 164 | [ |
155 | 165 | 'webform_id' => $webform_id, |
156 | 166 | 'uuid' => $submission->uuid(), |
157 | 167 | ] |
158 | 168 | ) |
159 | 169 | ->setAbsolute() |
160 | | - ->toString(TRUE)->getGeneratedUrl(); |
161 | | - |
162 | | - $submissionData[$submission->id()] = $url; |
163 | | - } |
| 170 | + ->toString(TRUE)->getGeneratedUrl(), |
| 171 | + $submissionEntityStorage->loadMultiple($sids ?: []) |
| 172 | + ); |
164 | 173 | } |
| 174 | + catch (\Exception $e) { |
| 175 | + $errors = [ |
| 176 | + 'error' => [ |
| 177 | + 'message' => $this->t('Could not generate submission URLs'), |
| 178 | + ], |
| 179 | + ]; |
165 | 180 |
|
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); |
193 | 182 | } |
194 | 183 |
|
195 | | - return $errors ?? []; |
| 184 | + return new ModifiedResourceResponse($result); |
196 | 185 | } |
197 | 186 |
|
198 | 187 | } |
0 commit comments