Skip to content

Commit

Permalink
updating uploads helper
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic committed Jan 4, 2024
1 parent c2c54b3 commit f372b1a
Show file tree
Hide file tree
Showing 10 changed files with 1,267 additions and 923 deletions.
35 changes: 35 additions & 0 deletions src/Config/UtilsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,39 @@ class UtilsConfig
*/
public const DELIMITER = '---';

/**
* Status error const.
*
* @var string
*/
public const STATUS_ERROR = 'error';

/**
* Status success const.
*
* @var string
*/
public const STATUS_SUCCESS = 'success';

/**
* Status warning const.
*
* @var string
*/
public const STATUS_WARNING = 'warning';

public const SETTINGS_NAME_PREFIX = 'es-forms';

public const FILTER_SETTINGS_DATA = self::FILTER_PREFIX . '_settings_data';
public const FILTER_SETTINGS_NONE_TRANSLATABLE_NAMES = self::FILTER_PREFIX . '_settings_none_translatable_names';

/**
* API validator output key.
*
* @var string
*/
public const VALIDATOR_OUTPUT_KEY = 'validation';

/**
* Method that returns projects temp upload dir name.
*
Expand All @@ -65,4 +93,11 @@ public static function getTempUploadDir(): string
{
return "esforms-tmp";
}

/**
* Filter settings is debug active key.
*/
public const FILTER_SETTINGS_IS_DEBUG_ACTIVE = 'es_forms_settings_is_debug_active';

public const SETTINGS_DEBUG_DEVELOPER_MODE_KEY = 'developer-mode';
}
276 changes: 276 additions & 0 deletions src/Helpers/ApiHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
<?php

/**
* Class that holds all api helpers used in classes.
*
* @package EightshiftLibs\Helpers
*/

declare(strict_types=1);

namespace EightshiftForms\Helpers;

use EightshiftFormsUtils\Config\UtilsConfig;
use EightshiftFormsUtilsVendor\EightshiftLibs\Helpers\Components;

/**
* ApiHelper class.
*/
final class ApiHelper
{
/**
* Return API response array details.
*
* @param array<mixed> $response Response got from the API.
*
* @return array<string, mixed>
*/
/**
* Return API response array details.
*
* @param string $integration Integration name from settings.
* @param mixed $response API full reponse.
* @param string $url Url of the request.
* @param array<mixed> $params All params prepared for API.
* @param array<mixed> $files All files prepared for API.
* @param string $itemId List Id used for API (questions, form id, list id, item id).
* @param string $formId Internal form ID.
* @param boolean $isDisabled If integration is disabled.
* @param boolean $isCurl Used for some changed if native cURL is used.
*
* @return array<string, mixed>
*/
public static function getIntegrationApiReponseDetails(
string $integration,
$response,
string $url,
array $params = [],
array $files = [],
string $itemId = '',
string $formId = '',
bool $isDisabled = false,
bool $isCurl = false
): array {

// Do regular stuff if this is not and WP_Error.
if (!\is_wp_error($response)) {
if ($isCurl) {
$code = $response['status'] ?? 200;
$body = $response;
} else {
$code = $response['response']['code'] ?? 200;
$body = $response['body'] ?? '';

if (Components::isJson($body)) {
$body = \json_decode($body, true) ?? [];
}
}
} else {
// Mock response for WP_Error.
$code = 404;
$body = [
'error' => $response->get_error_message(),
];
$response = [];
}

return [
'integration' => Components::kebabToCamelCase($integration, '-'),
'params' => $params,
'files' => $files,
'response' => $response['response'] ?? [],
'code' => $code,
'body' => !\is_string($body) ? $body : [],
'url' => $url,
'itemId' => $itemId,
'formId' => $formId,
'isDisabled' => $isDisabled,
];
}

/**
* Return Integration API error response array - in combination with getIntegrationApiReponseDetails response.
*
* @param array<string, mixed> $details Details provided by getIntegrationApiReponseDetails method.
* @param string $msg Message to output.
* @param array<string, mixed> $additional Additional array details to attach to the success output.
*
* @return array<string, mixed>
*/
public static function getIntegrationApiErrorOutput(array $details, string $msg, array $additional = []): array
{
unset($details['status']);
unset($details['message']);

return \array_merge(
[
'status' => UtilsConfig::STATUS_ERROR,
'message' => $msg,
],
$details,
$additional
);
}

/**
* Return Integration API success response array - in combination with getIntegrationApiReponseDetails response.
*
* @param array<string, mixed> $details Details provided by getIntegrationApiReponseDetails method.
* @param array<string, mixed> $additional Additional array details to attach to the success output.
*
* @return array<string, mixed>
*/
public static function getIntegrationApiSuccessOutput(array $details, array $additional = []): array
{
$integration = $details['integration'] ?? '';

unset($details['status']);
unset($details['message']);

return \array_merge(
[
'status' => UtilsConfig::STATUS_SUCCESS,
'message' => "{$integration}Success",
],
$details,
$additional
);
}

/**
* Return Integration API final response array depending on the status - in combination with getIntegrationApiReponseDetails response.
*
* @param array<string, mixed> $details Details provided by getIntegrationApiReponseDetails method.
* @param string $msg Message to output.
* @param array<int, string> $additional Additional array details to attach to the success output.
*
* @return array<string, array<mixed>|int|string>
*/
public static function getIntegrationApiOutput(array $details, string $msg, array $additional = []): array
{
$status = $details['status'] ?? UtilsConfig::STATUS_ERROR;

$additionalOutput = [];

$additional = [
UtilsConfig::VALIDATOR_OUTPUT_KEY,
...$additional,
];

foreach ($additional as $value) {
if (isset($details[$value])) {
$additionalOutput[$value] = $details[$value];
}
}

if ($status === UtilsConfig::STATUS_SUCCESS) {
return self::getApiSuccessOutput(
$msg,
$additionalOutput,
$details
);
}

return self::getApiErrorOutput(
$msg,
$additionalOutput,
$details
);
}

/**
* Return API error response array.
*
* @param string $msg Msg for the user.
* @param array<string, mixed> $additional Additonal data to attach to response.
* @param array<string, mixed> $debug Debug options.
*
* @return array<string, array<mixed>|int|string>
*/
public static function getApiErrorOutput(string $msg, array $additional = [], array $debug = []): array
{
$output = [
'status' => UtilsConfig::STATUS_ERROR,
'code' => 400,
'message' => $msg,
];

if ($additional) {
$output['data'] = $additional;
}

if (Helper::isDeveloperMode() && $debug) {
$output['debug'] = $debug;
}

return $output;
}

/**
* Return API success response array - Generic.
*
* @param string $msg Msg for the user.
* @param array<int|string, mixed> $additional Additonal data to attach to response.
* @param array<string, mixed> $debug Debug options.
*
* @return array<string, array<mixed>|int|string>
*/
public static function getApiSuccessOutput(string $msg, array $additional = [], array $debug = []): array
{
$output = [
'status' => UtilsConfig::STATUS_SUCCESS,
'code' => 200,
'message' => $msg,
];

if ($additional) {
$output['data'] = $additional;
}

if (Helper::isDeveloperMode() && $debug) {
$output['debug'] = $debug;
}

return $output;
}

/**
* Return API warning response array - Generic.
*
* @param string $msg Msg for the user.
* @param array<int|string, mixed> $additional Additonal data to attach to response.
* @param array<string, mixed> $debug Debug options.
*
* @return array<string, array<mixed>|int|string>
*/
public static function getApiWarningOutput(string $msg, array $additional = [], array $debug = []): array
{
$output = [
'status' => UtilsConfig::STATUS_WARNING,
'code' => 200,
'message' => $msg,
];

if ($additional) {
$output['data'] = $additional;
}

if (Helper::isDeveloperMode() && $debug) {
$output['debug'] = $debug;
}

return $output;
}

/**
* Return API error response array for missing permissions.
*
* @return array<string, mixed>
*/
public static function getApiPermissionsErrorOutput(): array
{
return self::getApiErrorOutput(
\esc_html__('You don\'t have enough permissions to perform this action!', 'eightshift-forms'),
);
}
}
Loading

0 comments on commit f372b1a

Please sign in to comment.