Skip to content

Commit

Permalink
FORMS-984: Applied coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
jekuaitk committed Oct 20, 2023
1 parent 0070bf6 commit 872880a
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 133 deletions.
264 changes: 132 additions & 132 deletions src/Plugin/rest/resource/WebformAllFormSubmissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,141 +21,141 @@
*/
class WebformAllFormSubmissions extends ResourceBase {

/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
private $currentRequest;

/**
* The entity type manager object.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
private $entityTypeManager;

/**
* The webform helper.
*
* @var \Drupal\os2forms_rest_api\WebformHelper
*/
private $webformHelper;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);

$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->setCurrentRequest($container->get('request_stack')->getCurrentRequest());
$instance->webformHelper = $container->get('Drupal\os2forms_rest_api\WebformHelper');

return $instance;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
private $currentRequest;

/**
* The entity type manager object.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
private $entityTypeManager;

/**
* The webform helper.
*
* @var \Drupal\os2forms_rest_api\WebformHelper
*/
private $webformHelper;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {

Check failure on line 48 in src/Plugin/rest/resource/WebformAllFormSubmissions.php

View workflow job for this annotation

GitHub Actions / PHP - Code analysis (8.1, prefer-lowest)

Method Drupal\os2forms_rest_api\Plugin\rest\resource\WebformAllFormSubmissions::create() has parameter $configuration with no value type specified in iterable type array.

Check failure on line 48 in src/Plugin/rest/resource/WebformAllFormSubmissions.php

View workflow job for this annotation

GitHub Actions / PHP - Code analysis (8.1, prefer-stable)

Method Drupal\os2forms_rest_api\Plugin\rest\resource\WebformAllFormSubmissions::create() has parameter $configuration with no value type specified in iterable type array.
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);

$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->setCurrentRequest($container->get('request_stack')->getCurrentRequest());
$instance->webformHelper = $container->get('Drupal\os2forms_rest_api\WebformHelper');

return $instance;
}

/**
* Sets the current request.
*
* @param \Symfony\Component\HttpFoundation\Request $current_request
* The current request.
*
* @return $this
* Class.
*/
protected function setCurrentRequest(Request $current_request) {
$this->currentRequest = $current_request;
return $this;
}

/**
* Retrieve all submissions for a given webform id.
*
* @param string $webform_id
* Webform ID.
*
* @return \Drupal\rest\ModifiedResourceResponse
* HTTP response object containing webform submissions.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws HttpException in case of error.
*/
public function get(string $webform_id): ModifiedResourceResponse {
if (empty($webform_id)) {
$errors = [
'error' => [
'message' => 'Webform ID is required.',
],
];
return new ModifiedResourceResponse($errors, 400);
}

/**
* Sets the current request.
*
* @param \Symfony\Component\HttpFoundation\Request $current_request
* The current request.
*
* @return $this
* Class.
*/
protected function setCurrentRequest(Request $current_request) {
$this->currentRequest = $current_request;
return $this;
// Webform access check.
$webform = $this->webformHelper->getWebform($webform_id);

if (NULL === $webform) {
$errors = [
'error' => [
'message' => $this->t('Could not find webform with id :webform_id', [':webform_id' => $webform_id]),
],
];
return new ModifiedResourceResponse($errors, 400);
}

if (!$this->webformHelper->hasWebformAccess($webform, $this->webformHelper->getCurrentUser())) {
$errors = [
'error' => [
'message' => $this->t('Access denied'),
],
];
return new ModifiedResourceResponse($errors, 401);
}

$submissionData = [];

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

// Query for webform submissions with this webform_id.
$query = $this->entityTypeManager->getStorage('webform_submission')->getQuery()
->condition('webform_id', $webform_id);

$startTimestamp = $this->currentRequest->query->get('starttime');
if (is_numeric($startTimestamp)) {
$query->condition('created', $startTimestamp, '>=');
$result['starttime'] = $startTimestamp;
}

$endTimestamp = $this->currentRequest->query->get('endtime');
if (is_numeric($endTimestamp)) {
$query->condition('created', $endTimestamp, '<=');
$result['endtime'] = $endTimestamp;
}

/**
* Retrieve all submissions for a given webform id.
*
* @param string $webform_id
* Webform ID.
*
* @return \Drupal\rest\ModifiedResourceResponse
* HTTP response object containing webform submissions.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws HttpException in case of error.
*/
public function get(string $webform_id): ModifiedResourceResponse {
if (empty($webform_id)) {
$errors = [
'error' => [
'message' => 'Webform ID is required.',
],
];
return new ModifiedResourceResponse($errors, 400);
}

// Webform access check.
$webform = $this->webformHelper->getWebform($webform_id);

if (NULL === $webform) {
$errors = [
'error' => [
'message' => $this->t('Could not find webform with id :webform_id', [':webform_id' => $webform_id]),
],
];
return new ModifiedResourceResponse($errors, 400);
}

if (!$this->webformHelper->hasWebformAccess($webform, $this->webformHelper->getCurrentUser())) {
$errors = [
'error' => [
'message' => $this->t('Access denied'),
],
];
return new ModifiedResourceResponse($errors, 401);
}

$submissionData = [];

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

// Query for webform submissions with this webform_id.
$query = $this->entityTypeManager->getStorage('webform_submission')->getQuery()
->condition('webform_id', $webform_id);

$startTimestamp = $this->currentRequest->query->get('starttime');
if (is_numeric($startTimestamp)) {
$query->condition('created', $startTimestamp, '>=');
$result['starttime'] = $startTimestamp;
}

$endTimestamp = $this->currentRequest->query->get('endtime');
if (is_numeric($endTimestamp)) {
$query->condition('created', $endTimestamp, '<=');
$result['endtime'] = $endTimestamp;
}

$query->accessCheck(FALSE);
$sids = $query->execute();

foreach ($sids as $sid) {
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $this->entityTypeManager->getStorage('webform_submission')->load($sid);

$url = Url::fromRoute(
'rest.webform_rest_submission.GET',
[
'webform_id' => $webform_id,
'uuid' => $webform_submission->uuid(),
],
[
'absolute' => TRUE,
]
)->toString();

$submissionData[$sid] = $url;
}

$result['submissions'] = $submissionData;

return new ModifiedResourceResponse($result);
$query->accessCheck(FALSE);
$sids = $query->execute();

foreach ($sids as $sid) {
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $this->entityTypeManager->getStorage('webform_submission')->load($sid);

$url = Url::fromRoute(
'rest.webform_rest_submission.GET',
[
'webform_id' => $webform_id,
'uuid' => $webform_submission->uuid(),
],
[
'absolute' => TRUE,
]
)->toString();

$submissionData[$sid] = $url;
}

}
$result['submissions'] = $submissionData;

return new ModifiedResourceResponse($result);
}

}
2 changes: 1 addition & 1 deletion src/WebformHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public function fileDownload(string $uri) {
* The current user.
*/
public function getCurrentUser(): AccountProxyInterface {
return $this->currentUser;
return $this->currentUser;
}

}

0 comments on commit 872880a

Please sign in to comment.