Skip to content

Commit 88f7b53

Browse files
committed
FORMS-984: Code cleanup
1 parent 33f6fe9 commit 88f7b53

File tree

1 file changed

+81
-43
lines changed

1 file changed

+81
-43
lines changed

src/Plugin/rest/resource/WebformAllFormSubmissions.php

Lines changed: 81 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
namespace Drupal\os2forms_rest_api\Plugin\rest\resource;
44

5+
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
6+
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
7+
use Drupal\Core\Entity\Query\QueryInterface;
58
use Drupal\Core\Url;
9+
use Drupal\os2forms_rest_api\WebformHelper;
610
use Drupal\rest\ModifiedResourceResponse;
711
use Drupal\rest\Plugin\ResourceBase;
812
use Symfony\Component\DependencyInjection\ContainerInterface;
913
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpFoundation\Response;
1015

1116
/**
1217
* Creates a rest resource for retrieving webform submissions.
@@ -20,6 +25,14 @@
2025
* )
2126
*/
2227
class WebformAllFormSubmissions extends ResourceBase {
28+
/**
29+
* Allowed DateTime query parameters and their operation.
30+
*/
31+
private const ALLOWED_DATETIME_QUERY_PARAMS = [
32+
'starttime' => '>=',
33+
'endtime' => '<=',
34+
];
35+
2336
/**
2437
* The current request.
2538
*
@@ -50,22 +63,12 @@ public static function create(ContainerInterface $container, array $configuratio
5063
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
5164

5265
$instance->entityTypeManager = $container->get('entity_type.manager');
53-
$instance->setCurrentRequest($container->get('request_stack')->getCurrentRequest());
54-
$instance->webformHelper = $container->get('Drupal\os2forms_rest_api\WebformHelper');
66+
$instance->currentRequest = $container->get('request_stack')->getCurrentRequest();
67+
$instance->webformHelper = $container->get(WebformHelper::class);
5568

5669
return $instance;
5770
}
5871

59-
/**
60-
* Sets current request.
61-
*
62-
* @param \Symfony\Component\HttpFoundation\Request $currentRequest
63-
* The current request.
64-
*/
65-
protected function setCurrentRequest(Request $currentRequest): void {
66-
$this->currentRequest = $currentRequest;
67-
}
68-
6972
/**
7073
* Get submissions for a given webform.
7174
*
@@ -82,7 +85,7 @@ public function get(string $webform_id): ModifiedResourceResponse {
8285
'message' => 'Webform ID is required.',
8386
],
8487
];
85-
return new ModifiedResourceResponse($errors, 400);
88+
return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST);
8689
}
8790

8891
// Attempt finding webform.
@@ -95,7 +98,7 @@ public function get(string $webform_id): ModifiedResourceResponse {
9598
],
9699
];
97100

98-
return new ModifiedResourceResponse($errors, 400);
101+
return new ModifiedResourceResponse($errors, Response::HTTP_NOT_FOUND);
99102
}
100103

101104
// Webform access check.
@@ -106,53 +109,88 @@ public function get(string $webform_id): ModifiedResourceResponse {
106109
],
107110
];
108111

109-
return new ModifiedResourceResponse($errors, 401);
112+
return new ModifiedResourceResponse($errors, Response::HTTP_UNAUTHORIZED);
110113
}
111114

112115
$result = ['webform_id' => $webform_id];
113116

117+
try {
118+
$submissionEntityStorage = $this->entityTypeManager->getStorage('webform_submission');
119+
}
120+
catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
121+
$errors = [
122+
'error' => [
123+
'message' => $this->t('Could not load webform submission storage'),
124+
],
125+
];
126+
127+
return new ModifiedResourceResponse($errors, Response::HTTP_INTERNAL_SERVER_ERROR);
128+
}
129+
114130
// Query for webform submissions with this webform_id.
115-
$query = $this->entityTypeManager->getStorage('webform_submission')->getQuery()
131+
$submissionQuery = $submissionEntityStorage->getQuery()
116132
->condition('webform_id', $webform_id);
117133

118-
$startTimestamp = $this->currentRequest->query->get('starttime');
119-
if (is_numeric($startTimestamp)) {
120-
$query->condition('created', $startTimestamp, '>=');
121-
$result['starttime'] = $startTimestamp;
122-
}
134+
foreach (self::ALLOWED_DATETIME_QUERY_PARAMS as $param => $operator) {
135+
$errors = $this->updateSubmissionQuery($this->currentRequest, $submissionQuery, $param, $operator, $result);
123136

124-
$endTimestamp = $this->currentRequest->query->get('endtime');
125-
if (is_numeric($endTimestamp)) {
126-
$query->condition('created', $endTimestamp, '<=');
127-
$result['endtime'] = $endTimestamp;
137+
if (isset($errors['error'])) {
138+
return new ModifiedResourceResponse($errors, Response::HTTP_BAD_REQUEST);
139+
}
128140
}
129141

130-
$query->accessCheck(FALSE);
131-
$sids = $query->execute();
142+
// Complete query.
143+
$submissionQuery->accessCheck(FALSE);
144+
$sids = $submissionQuery->execute();
132145

133146
$submissionData = [];
134147

135-
foreach ($sids as $sid) {
136-
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
137-
$webform_submission = $this->entityTypeManager->getStorage('webform_submission')->load($sid);
138-
139-
$url = Url::fromRoute(
140-
'rest.webform_rest_submission.GET',
141-
[
142-
'webform_id' => $webform_id,
143-
'uuid' => $webform_submission->uuid(),
144-
],
145-
[
146-
'absolute' => TRUE,
147-
]
148-
)->toString();
149-
150-
$submissionData[$sid] = $url;
148+
if (!empty($sids)) {
149+
$submissions = $submissionEntityStorage->loadMultiple($sids);
150+
151+
foreach ($submissions as $submission) {
152+
$url = Url::fromRoute(
153+
'rest.webform_rest_submission.GET',
154+
[
155+
'webform_id' => $webform_id,
156+
'uuid' => $submission->uuid(),
157+
]
158+
)
159+
->setAbsolute()
160+
->toString(TRUE)->getGeneratedUrl();
161+
162+
$submissionData[$submission->id()] = $url;
163+
}
151164
}
152165

153166
$result['submissions'] = $submissionData;
154167

155168
return new ModifiedResourceResponse($result);
156169
}
157170

171+
/**
172+
* Updates submission query with request query parameters.
173+
*/
174+
private function updateSubmissionQuery(Request $request, QueryInterface $submissionQuery, string $parameter, string $operator, array &$result): array {
175+
// Handle starttime request query.
176+
$timeQuery = $request->query->get($parameter);
177+
178+
if (!empty($timeQuery)) {
179+
try {
180+
$startTime = new \DateTime($timeQuery);
181+
$submissionQuery->condition('created', $startTime->getTimestamp(), $operator);
182+
$result[$parameter] = $timeQuery;
183+
}
184+
catch (\Exception $e) {
185+
$errors = [
186+
'error' => [
187+
'message' => $this->t('Could not generate DateTime from :time', [':time' => $timeQuery]),
188+
],
189+
];
190+
}
191+
}
192+
193+
return $errors ?? [];
194+
}
195+
158196
}

0 commit comments

Comments
 (0)