Commit f4cee9bd authored by Eleo Basili's avatar Eleo Basili
Browse files

Issue #3276126 by eleonel: Fix small bugs creating the cookies.

parent c566a682
Loading
Loading
Loading
Loading
+113 −7
Original line number Diff line number Diff line
@@ -97,9 +97,111 @@ class MailchimpEnricherDatasource extends EnricherDatasourceBase implements Cont

  /**
   * {@inheritdoc}
   *
   * Take the incoming path and process it if necessary.
   *
   * Sample use case: converting /mypath/<sometoken>/return/to/path, as
   * drupal does not allow / is parameters, we would need to process this url
   * to encode it differently and then drupal can digest the new path.
   *
   * This is designed to be called from our
   * \Drupal\convivial_enricher\PathProcessor\InboundPathProcessor
   */
  public function processIncomingPath(&$path, $endpoint_path) {
    return $path;
    $this->endpoint_path = $endpoint_path;
    return $this->convertPathToDataEncodedString($path);
  }

  /**
   * Modify the incoming path/convert incoming path to something usable.
   *
   * @param string $path
   *   The incoming path string. eg: The /my/path portion of the incoming URL.
   *
   * @return bool
   *   TRUE if path was converted by this method, false if not.
   */
  private function convertPathToDataEncodedString(string &$path): string {
    if ($this->pathRequiresConversion($path)) {
      $token     = $this->getTokenFromPath($path);
      $return_to = $this->getReturnToPortionOfPath($path);

      $encoded_data_string = base64_encode("return_to={$return_to}&token={$token}");

      $path = '/' . $this->endpoint_path . "/data:$encoded_data_string";
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Get the token from the provided path string.
   *
   * @param string $path
   *   The incoming path string. eg: The /my/path portion of the incoming URL.
   *
   * @return string
   *   The token value calculated from the supplied path string.
   */
  private function getTokenFromPath(string $path) {
    $token = preg_replace('|^\/' . $this->endpoint_path . '\/(.*?)\/.*$|', '$1', $path);
    return $token;
  }

  /**
   * Get the return_to parameter encoded in the path.
   *
   * @param string $path
   *   The incoming path string. eg: The /my/path portion of the incoming URL.
   *
   * @return string
   *   The return_to value calculated from the supplied path string.
   */
  private function getReturnToPortionOfPath(string $path): string {
    $return_to = preg_replace('|^\/' . $this->endpoint_path . '\/.*?(\/.*)$|', '$1', $path);
    return $return_to;
  }

  /**
   * Determine if the format of this incoming path requires conversion.
   *
   * @param string $path
   *   The incoming path string. eg: The /my/path portion of the incoming URL.
   *
   * @return bool
   *   TRUE if path requires conversion to a drupal usable format.
   */
  private function pathRequiresConversion(string $path): bool {
    return $this->pathIsAnEndpointPath($path, $this->endpoint_path) && $this->pathDoesNotHaveLoadedData($path, $this->endpoint_path);
  }

  /**
   * Analyse this path to determine if it contains encoded data we can read.
   *
   * @param string $path
   *   The incoming path string. eg: The /my/path portion of the incoming URL.
   *
   * @return bool
   *   TRUE if path contains encoded data, FALSE if not.
   */
  private function pathDoesNotHaveLoadedData(string $path): bool {
    return strpos($path, "/$this->endpoint_path/data:") !== 0;
  }

  /**
   * Determine if the given path is our endpoint.
   *
   * Compare the path to the endpoint_path to determine if this is one of
   * our defined endpoints.
   *
   * @param string $path
   *   The incoming path string. eg: The /my/path portion of the incoming URL.
   *
   * @return bool
   *   TRUE if path is a processable endpoint.
   */
  private function pathIsAnEndpointPath(string $path): bool {
    return strpos($path, "/$this->endpoint_path/") === 0;
  }

  /**
@@ -118,13 +220,17 @@ class MailchimpEnricherDatasource extends EnricherDatasourceBase implements Cont
      $this->logger->warning('%type: @message in %function (line %line of %file).', $variables);
    }

    if (!empty((array) $this->listMember)) {
      $output += (array) $this->listMember;
    // Create the cookies of all the output fields.
    foreach ($this->listMember->members as $member) {
      $fields = (array) $member;

      foreach ($fields as $field_name => $field_value) {
        if (is_array($field_value) || is_object($field_value)) {
          $field_value = json_encode($field_value);
        }

    // Create the cookies of all the output fields.
    foreach ($output as $cookie_name => $cookie_value) {
      $return_cookies[] = $this->createCookie($cookie_name, $cookie_value);
        $return_cookies[] = $this->createCookie($field_name, $field_value);
      }
    }
    return $return_cookies;
  }