Skip to content

Commit

Permalink
FORMS-984: Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jekuaitk committed Oct 24, 2023
1 parent 2cc6943 commit 0e26f29
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 47 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ adding query parameters to the URL:
| Name | Value | Example |
|-------------|----------------------|--------------|
| `starttime` | [PHP Date and Time Formats](https://www.php.net/manual/en/datetime.formats.php) | `yesterday` |
| `endtime` | PHP DateTime formats | `2023-10-23` |
| `endtime` | [PHP Date and Time Formats](https://www.php.net/manual/en/datetime.formats.php) | `2023-10-23` |

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

This example requests all submissions after on or after October 1st, 2023:
This example requests all submissions on or after October 1st, 2023:

Request:

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

## Custom access control
Expand Down
77 changes: 33 additions & 44 deletions src/Plugin/rest/resource/WebformAllFormSubmissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Url;
use Drupal\os2forms_rest_api\WebformHelper;
use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

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

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

if (isset($errors['error'])) {
return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST);
foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) {
$value = $requestQuery->get($param);

if (!empty($value)) {
try {
$dateTime = new \DateTimeImmutable($value);
$submissionQuery->condition('created', $dateTime->getTimestamp(), $operator);
$result[$param] = $value;
}
catch (\Exception $e) {
$errors = [
'error' => [
'message' => $this->t('Could not generate DateTime from :time', [':time' => $value]),
],
];

return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST);
}
}
}

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

$submissionData = [];

if (!empty($sids)) {
$submissions = $submissionEntityStorage->loadMultiple($sids);

foreach ($submissions as $submission) {
$url = Url::fromRoute(
// Generate submission URLs.
try {
$result['submissions'] = array_map(
static fn($submission) => Url::fromRoute(
'rest.webform_rest_submission.GET',
[
'webform_id' => $webform_id,
'uuid' => $submission->uuid(),
]
)
->setAbsolute()
->toString(TRUE)->getGeneratedUrl();
->toString(TRUE)->getGeneratedUrl(),
$submissionEntityStorage->loadMultiple($sids ?: [])
);
} catch (\Exception $e) {
$errors = [
'error' => [
'message' => $this->t('Could not generate submission URLs'),
],
];

$submissionData[$submission->id()] = $url;
}
return new ModifiedResourceResponse($errors, Response::HTTP_INTERNAL_SERVER_ERROR);
}

$result['submissions'] = $submissionData;

return new ModifiedResourceResponse($result);
}

/**
* Updates submission query with request query parameters.
*
* @phpstan-param array<string, mixed> $result
* @phpstan-return array<string, mixed>
*/
private function updateSubmissionQuery(Request $request, QueryInterface $submissionQuery, string $parameter, string $operator, array &$result): array {
$value = $request->query->get($parameter);

if (!empty($timeQuery)) {
try {
$startTime = new \DateTimeImmutable($timeQuery);
$submissionQuery->condition('created', $startTime->getTimestamp(), $operator);
$result[$parameter] = $timeQuery->format(\DateTimeImmutable::ATOM);
}
catch (\Exception $e) {
$errors = [
'error' => [
'message' => $this->t('Could not generate DateTime from :time', [':time' => $timeQuery]),
],
];
}
}

return $errors ?? [];
}

}

0 comments on commit 0e26f29

Please sign in to comment.