You are here

protected function AppAnalyticsFormBase::validateQueryString in Apigee Edge 8

Validates the URL query string parameters.

Parameters

array $form: An associative array containing the structure of the form.

string $metric: The filter parameter.

string $since: The start date parameter.

string $until: The end date parameter.

string $environment: The environment parameter.

Return value

bool TRUE if the parameters are correctly set, else FALSE.

1 call to AppAnalyticsFormBase::validateQueryString()
AppAnalyticsFormBase::buildForm in src/Form/AppAnalyticsFormBase.php
Form constructor.

File

src/Form/AppAnalyticsFormBase.php, line 312

Class

AppAnalyticsFormBase
App analytics form builder for developer- and team apps.

Namespace

Drupal\apigee_edge\Form

Code

protected function validateQueryString(array $form, $metric, $since, $until, $environment) : bool {
  if ($metric === NULL || $since === NULL || $until === NULL || $environment === NULL) {
    return FALSE;
  }
  try {
    if (!array_key_exists($metric, $form['controls']['metrics']['#options'])) {
      $this
        ->messenger()
        ->addError($this
        ->t('Invalid parameter metric in the URL.'));
      return FALSE;
    }
    $since = DrupalDateTime::createFromTimestamp($since);
    $until = DrupalDateTime::createFromTimestamp($until);
    if ($since
      ->diff($until)->invert === 1) {
      $this
        ->messenger()
        ->addError($this
        ->t('The end date cannot be before the start date.'));
      return FALSE;
    }
    if ($since
      ->diff(new DrupalDateTime())->invert === 1) {
      $this
        ->messenger()
        ->addError($this
        ->t('Start date cannot be in future. The current local time of the Developer Portal: @time', [
        '@time' => new DrupalDateTime(),
      ]));
      return FALSE;
    }
    if (!in_array($environment, $this
      ->config('apigee_edge.common_app_settings')
      ->get('analytics_available_environments'))) {
      $this
        ->messenger()
        ->addError($this
        ->t('Invalid parameter environment in the URL.'));
      return FALSE;
    }
  } catch (\InvalidArgumentException $exception) {
    $this
      ->messenger()
      ->addError($this
      ->t('Invalid URL query parameters.'));
    return FALSE;
  }
  return TRUE;
}